{
  "version": 3,
  "sources": ["../../../src/Entity.ts"],
  "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\n// @import-as-namespace\n\nimport * as Schema from 'effect/Schema';\n\nimport type { ForeignKey } from '@dxos/echo-protocol';\nimport type { EntityId, URI } from '@dxos/keys';\n\nimport * as internal from './internal';\nimport * as objInternal from './internal/Obj';\nimport type * as Ref from './Ref';\nimport type * as Relation from './Relation';\nimport type * as Tag from './Tag';\nimport * as Type from './Type';\n\n// Re-export KindId and SnapshotKindId from internal.\nexport const KindId = internal.KindId;\nexport type KindId = typeof internal.KindId;\nexport const SnapshotKindId = internal.SnapshotKindId;\nexport type SnapshotKindId = typeof internal.SnapshotKindId;\n\n// NOTE: Relation does not extend Obj so that, for example, we can prevent Relations from being used as source and target objects.\n//  However, we generally refer to Obj and Relation instances as \"objects\",\n//  and many API methods accept both Obj.Unknown and Relation.Unknown (i.e., Entity.Unknown) instances.\n\nexport const Kind = internal.EntityKind;\nexport type Kind = internal.EntityKind;\nexport const KindSchema = internal.EntityKindSchema;\n\n/**\n * Assigns a kind to an Object or Relation instance.\n * NOTE: Needed to make `isRelation` and `isObject` checks work.\n */\nexport interface OfKind<K extends Kind> {\n  readonly [KindId]: K;\n  readonly id: EntityId;\n}\n\n/**\n * Assigns a snapshot kind to an Object or Relation snapshot.\n */\nexport interface SnapshotOfKind<K extends Kind> {\n  readonly [SnapshotKindId]: K;\n  readonly id: EntityId;\n}\n\n/**\n * Obj or Relation with a specific set of properties.\n */\nexport type Entity<Props> = OfKind<Kind> & Props;\n\n/**\n * Unknown Obj or Relation (reactive).\n */\nexport interface Unknown extends OfKind<Kind> {}\n\n/**\n * Effect Schema for any ECHO entity (object or relation).\n *\n * Kind-agnostic counterpart to `Obj.Unknown` / `Relation.Unknown` — validates\n * the structural shape (id + properties) without constraining `[KindId]`. Used\n * in operation input schemas that accept any entity flavour (e.g.\n * `Schema.Array(Entity.Unknown)`).\n *\n * The cast bridges the runtime structural schema to the branded `Unknown` type:\n * `[KindId]` is a symbol brand that can't be expressed in a runtime `Struct`,\n * so the entity guarantee is carried at the type level only (same approach as\n * `Obj.Unknown` / `Relation.Unknown`). Unlike those, this is kind-agnostic so it\n * isn't an `UnknownTypeSchema<_, K>` (there's no single `K`) and carries no\n * `TypeAnnotation`.\n */\nexport const Unknown: Schema.Schema<Unknown> = Schema.Struct({\n  id: Schema.String,\n}).pipe(\n  Schema.extend(Schema.Record({ key: Schema.String, value: Schema.Unknown })),\n) as unknown as Schema.Schema<Unknown>;\n\n/**\n * Snapshot of an Obj or Relation.\n * Branded with SnapshotKindId instead of KindId.\n */\nexport interface Snapshot extends SnapshotOfKind<Kind> {}\n\n/**\n * Object with arbitrary properties.\n *\n * NOTE: Due to how typescript works, this type is not assignable to a specific schema type.\n * In that case, use `Obj.instanceOf` to check if an object is of a specific type.\n *\n * This type is very permissive and allows accessing any property on the object.\n * We should move to Obj.Unknown that is not permissive and requires explicit instanceof checks..\n */\nexport interface Any extends OfKind<Kind> {\n  [key: string]: unknown;\n}\n\n/**\n * Returns all properties of an object or relation except for the id and kind.\n */\nexport type Properties<T> = Omit<T, 'id' | KindId | Relation.Source | Relation.Target>;\n\n/**\n * Check if a value is an ECHO entity (object or relation).\n * Returns `false` for snapshots.\n */\nexport const isEntity: (value: unknown) => value is Unknown = internal.isEntity;\n\n/**\n * Test if a value is an instance of a given object or relation type.\n *\n * Kind-agnostic counterpart to `Obj.instanceOf` / `Relation.instanceOf` —\n * use this when the caller's input type is `Type.AnyObj | Type.AnyRelation`.\n *\n * @example\n * ```ts\n * // Caller doesn't know whether `type` is object- or relation-kind.\n * const matches = <T extends Type.AnyObj | Type.AnyRelation>(type: T, value: unknown) =>\n *   Entity.instanceOf(type, value);\n * ```\n */\nexport const instanceOf: {\n  <S extends Type.AnyEntity>(schema: S): (value: unknown) => value is Type.InstanceType<S>;\n  <S extends Type.AnyEntity>(schema: S, value: unknown): value is Type.InstanceType<S>;\n} = ((...args: [schema: Type.AnyEntity, value?: unknown]) => {\n  if (args.length === 1) {\n    return (entity: unknown) => internal.isInstanceOf(args[0], entity);\n  }\n  return internal.isInstanceOf(args[0], args[1]);\n}) as any;\n\n/**\n * Check if a value is an ECHO entity snapshot.\n * Returns `false` for entities.\n */\nexport const isSnapshot = (value: unknown): value is Snapshot => {\n  if (typeof value !== 'object' || value === null) {\n    return false;\n  }\n  return (value as any)[SnapshotKindId] !== undefined;\n};\n\n// TODO(dmaretskyi): Type introspection -- move to kind.\nexport const getKind = internal.getEntityKind;\n\n/**\n * Property that accesses metadata for an entity.\n */\nexport const Meta: unique symbol = internal.MetaId as any;\n\n/**\n * Property that accesses metadata for an entity.\n */\nexport type Meta = typeof Meta;\n\n//\n// Entity-level functions that work on any entity (object or relation).\n// Use these when you don't know or care about the specific entity kind.\n// For kind-specific functions, use Obj.* or Relation.*.\n//\n\n/**\n * JSON representation of an entity.\n */\nexport type JSON = internal.ObjectJSON;\n\n/**\n * Whether the entity is a type-kind entity (a `Type.Type` produced by\n * `Type.makeObject` / `Type.makeRelation`, or a persisted schema). Type entities\n * carry their identity (typename/version) on themselves rather than referencing a\n * separate type, so the accessors below route them through the `Type.*` module.\n */\nconst isTypeEntity = (entity: unknown): boolean => internal.getEntityKindBrand(entity) === internal.EntityKind.Type;\n\n/**\n * Any value the read accessors operate on: a reactive entity or a snapshot.\n * Type entities (`Type.AnyEntity`) are also accepted — they're first-class\n * entities, and `Unknown`'s kind-agnostic brand already subsumes them.\n */\nexport type AnyInput = Unknown | Snapshot;\n\n/**\n * Get the canonical URI of an entity (object, relation, or type). Returns `URI.URI` —\n * an `EID` for object/relation instances and persisted types, or a typename\n * `DXN` for static type entities; narrow with `EID.parse(uri)` or\n * `DXN.tryMake(uri)` at the point of use.\n *\n * @param options.prefer - Controls the URI form (see {@link internal.GetURIOptions}).\n */\nexport const getURI = (entity: AnyInput, options?: internal.GetURIOptions): URI.URI =>\n  isTypeEntity(entity) ? Type.getURI(entity as Type.AnyEntity) : internal.getUri(entity as Unknown, options);\n\n/**\n * Get the DXN of an entity's type. For object/relation instances this is the URI\n * of the type they were created from; for a type entity it is the URI of the\n * meta-type ({@link Type.Type}, `dxn:org.dxos.type.schema:0.1.0`).\n */\nexport const getTypeURI = (entity: AnyInput): URI.URI | undefined =>\n  isTypeEntity(entity) ? Type.getURI(Type.Type) : internal.getTypeURI(entity as Unknown);\n\n/**\n * Get the type entity (`Type.AnyEntity`) the instance was created from.\n *\n * Returns `undefined` when the entity's type isn't registered in this runtime\n * (e.g. a freshly deserialized snapshot whose type entity hasn't been wired\n * up yet, or an entity loaded from storage before its schema is known). To\n * get the Effect Schema from the returned entity, use `Type.getSchema(...)`.\n *\n * For a type entity, returns the meta-type {@link Type.Type} (a type entity's\n * type is \"Type\").\n */\nexport const getType = (entity: AnyInput): Type.AnyEntity | undefined =>\n  isTypeEntity(entity) ? Type.Type : (internal.getType(entity) as Type.AnyEntity | undefined);\n\n/**\n * Get the typename of an entity's type. For object/relation instances this is the\n * typename of the type they were created from; for a type entity it is the type's\n * own typename (e.g. `com.example.type.person`).\n */\nexport const getTypename = (entity: AnyInput): string | undefined =>\n  isTypeEntity(entity) ? Type.getTypename(entity as Type.AnyEntity) : internal.getTypename(entity as Unknown);\n\n/**\n * Get the database an entity belongs to.\n */\nexport const getDatabase = (entity: Unknown | Snapshot): any | undefined => internal.getDatabase(entity);\n\n/**\n * Get the metadata for an entity.\n * Returns mutable meta when passed a mutable entity (inside change callback).\n * Returns read-only meta when passed a regular entity or snapshot.\n */\n// TODO(wittjosiah): When passed a Snapshot, should return a snapshot of meta, not the live meta proxy.\nexport function getMeta(entity: Mutable<Unknown>): internal.EntityMeta;\nexport function getMeta(entity: Unknown | Snapshot): internal.ReadonlyMeta;\nexport function getMeta(entity: Unknown | Snapshot | Mutable<Unknown>): internal.EntityMeta | internal.ReadonlyMeta {\n  return internal.getMetaChecked(entity);\n}\n\n/**\n * Get foreign keys for an entity from the specified source.\n */\nexport const getKeys = (entity: Unknown | Snapshot, source: string): ForeignKey[] => internal.getKeys(entity, source);\n\n/**\n * Check if an entity is deleted.\n */\nexport const isDeleted = (entity: Unknown | Snapshot): boolean => internal.isDeleted(entity);\n\n/**\n * Get the label of an entity.\n *\n * @param options.fallback `'typename'` returns the entity's typename when no\n *   label is set (e.g. `org.dxos.type.table`).\n */\nexport const getLabel = (entity: Unknown | Snapshot, options?: internal.GetLabelOptions): string | undefined =>\n  internal.getLabel(entity, options);\n\n/**\n * Set the label of an entity.\n * Must be called within an `Entity.update` / `Obj.update` / `Relation.update` callback.\n */\nexport const setLabel = (entity: Mutable<Unknown>, label: string): void => internal.setLabel(entity, label);\n\n/**\n * Get the description of an entity.\n */\nexport const getDescription = (entity: Unknown | Snapshot): string | undefined => internal.getDescription(entity);\n\n/**\n * Get the icon annotation for an entity (object or relation), resolved via its type-level\n * `IconAnnotation`. Returns the full `{ icon, hue }` annotation so callers can use both\n * the phosphor icon name and the suggested colour.\n */\nexport const getIcon = (entity: Unknown | Snapshot): internal.IconAnnotation | undefined => internal.getIcon(entity);\n\n/**\n * Convert an entity to its JSON representation.\n */\nexport const toJSON = (entity: Unknown | Snapshot): JSON => internal.objectToJSON(entity);\n\n/**\n * Subscribe to changes on an entity (object or relation).\n * @returns Unsubscribe function.\n */\nexport const subscribe = (entity: Unknown, callback: () => void): (() => void) => {\n  return internal.subscribe(entity, callback);\n};\n\n//\n// Change\n//\n\n/**\n * Used to provide a mutable view of an entity within `Entity.update`.\n */\nexport type Mutable<T> = internal.Mutable<T>;\n\n/**\n * Perform mutations on an entity (object or relation) within a change context.\n *\n * Entities are read-only by default. Mutations are batched and notifications fire\n * when the callback completes. Direct mutations outside of `Entity.update` will throw\n * at runtime.\n *\n * @param entity - The echo entity (object or relation) to mutate.\n * @param callback - Receives a mutable view of the entity. All mutations must occur here.\n *\n * @example\n * ```typescript\n * // Mutate within Entity.update\n * Entity.update(entity, (obj) => {\n *   obj.name = 'Updated';\n *   obj.count = 42;\n * });\n *\n * // Direct mutation throws\n * entity.name = 'Bob'; // Error: Cannot modify outside Entity.update()\n * ```\n *\n * Note: For type-specific operations, prefer `Obj.update` or `Relation.update`.\n */\nexport const update = <T extends Unknown>(entity: T, callback: internal.ChangeCallback<T>): void => {\n  internal.change(entity, callback);\n};\n\n/**\n * Add a tag to an entity.\n * Must be called within an `Entity.update`, `Obj.update`, or `Relation.update` callback.\n */\nexport const addTag = (entity: Mutable<Unknown>, tag: Ref.Ref<Tag.Tag>): void => internal.addTag(entity, tag);\n\n/**\n * Remove a tag from an entity.\n * Must be called within an `Entity.update`, `Obj.update`, or `Relation.update` callback.\n */\nexport const removeTag = (entity: Mutable<Unknown>, tag: Ref.Ref<Tag.Tag>): void => internal.removeTag(entity, tag);\n\n//\n// Atoms\n//\n\nexport const atom = objInternal.makeEntity;\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;gBAAAA;EAAA;;wBAAAC;EAAA,eAAAC;EAAA,cAAAC;EAAA;qBAAAC;EAAA,sBAAAC;EAAA,eAAAC;EAAA,eAAAC;EAAA;kBAAAC;EAAA;iBAAAC;EAAA,kBAAAC;EAAA,mBAAAC;EAAA,cAAAC;EAAA;mBAAAC;EAAA,gBAAAC;EAAA;mBAAAC;EAAA,gBAAAC;EAAA,iBAAAC;EAAA;;;AAMA,YAAYC,YAAY;AAajB,IAAMC,UAAkBA;AAExB,IAAMC,kBAA0BA;AAOhC,IAAMC,OAAgBC;AAEtB,IAAMC,aAAsBC;AA4C5B,IAAMC,WAAyCC,cAAO;EAC3DC,IAAWC;AACb,CAAA,EAAGC,KACMC,cAAcC,cAAO;EAAEC,KAAYJ;EAAQK,OAAcR;AAAQ,CAAA,CAAA,CAAA;AA+BnE,IAAMS,YAA0DA;AAehE,IAAMC,aAGR,IAAIC,SAAAA;AACP,MAAIA,KAAKC,WAAW,GAAG;AACrB,WAAO,CAACC,WAA6BC,aAAaH,KAAK,CAAA,GAAIE,MAAAA;EAC7D;AACA,SAAgBC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AAAA;AAM3E,IAAM8B,aAAY,CAAC9B,QAAiB+B,aAAAA;AACzC,SAAgBD,UAAU9B,QAAQ+B,QAAAA;AACpC;AAmCO,IAAMC,SAAS,CAAoBhC,QAAW+B,aAAAA;AACnDE,EAASC,OAAOlC,QAAQ+B,QAAAA;AAC1B;AAMO,IAAMI,UAAS,CAACnC,QAA0BoC,QAAyCD,OAAOnC,QAAQoC,GAAAA;AAMlG,IAAMC,aAAY,CAACrC,QAA0BoC,QAAyCC,UAAUrC,QAAQoC,GAAAA;AAMxG,IAAME,OAAmBC;",
  "names": ["KindId", "SnapshotKindId", "Unknown", "addTag", "getDatabase", "getDescription", "getIcon", "getKeys", "getLabel", "getType", "getTypeURI", "getTypename", "getURI", "isDeleted", "isEntity", "removeTag", "setLabel", "subscribe", "Schema", "KindId", "SnapshotKindId", "Kind", "EntityKind", "KindSchema", "EntityKindSchema", "Unknown", "Struct", "id", "String", "pipe", "extend", "Record", "key", "value", "isEntity", "instanceOf", "args", "length", "entity", "isInstanceOf", "isSnapshot", "undefined", "getKind", "getEntityKind", "Meta", "MetaId", "isTypeEntity", "getEntityKindBrand", "Type", "getURI", "options", "getUri", "getTypeURI", "getType", "getTypename", "getDatabase", "getMeta", "getMetaChecked", "getKeys", "source", "isDeleted", "getLabel", "setLabel", "label", "getDescription", "getIcon", "toJSON", "objectToJSON", "subscribe", "callback", "update", "internal", "change", "addTag", "tag", "removeTag", "atom", "makeEntity"]
}
