{
  "version": 3,
  "sources": ["../../../src/Ref.ts"],
  "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\n// @import-as-namespace\n\nimport * as Option from 'effect/Option';\nimport type * as Schema from 'effect/Schema';\nimport * as SchemaAST from 'effect/SchemaAST';\n\nimport { type URI } from '@dxos/keys';\n\nimport type * as Entity from './Entity';\nimport type * as internal from './internal';\nimport * as refInternal from './internal/Ref';\nimport type * as JsonSchema from './JsonSchema';\nimport type * as Obj from './Obj';\n// eslint-disable-next-line @dxos/rules/import-as-namespace\nimport type * as TypeNs from './Type';\n\n/**\n * Instance type for a reference.\n *\n * Reference can point to any object or relation.\n * References are lazy loaded.\n *\n * `ref.dxn` is the DXN of the referenced object.\n *\n * @example\n * ```ts\n * const taskRef: Ref<Task> = Ref.make(task);\n *\n * await taskRef.load(); // Returns Promise<Task>\n * yield* Database.load(taskRef); // Effectful version.\n *\n * database.makeRef(dxn); // Create a ref from a DXN.\n * ```\n */\nexport type Ref<T> = refInternal.Ref<T>;\nexport type Unknown = refInternal.Ref<Obj.Unknown>;\n\n/**\n * Factory function to create a Ref schema for the given target schema.\n * Use this in schema definitions to declare reference fields.\n *\n * @example\n * ```ts\n * const Task = Schema.Struct({\n *   assignee: Ref.Ref(Person),  // Creates a Ref schema\n * }).pipe(Type.makeObject(DXN.make('com.example.type.task', '0.1.0')));\n * ```\n */\nexport const Ref: {\n  <S extends TypeNs.AnyObj | TypeNs.AnyRelation>(type: S): RefSchema<TypeNs.InstanceType<S> & Obj.Unknown>;\n\n  // `Type.Type` entities (the meta-schema kind) can be referenced too — e.g. a\n  // trigger that points to a stored function/workflow definition.\n  <T extends TypeNs.Type<any>>(type: T): RefSchema<TypeNs.InstanceType<T>>;\n\n  // Schema-side overload for the well-known \"any object\" / \"any relation\" schemas.\n  // Other raw `Schema.Schema` values are intentionally rejected — callers should\n  // pass a `Type.Type` entity instead.\n  <S extends internal.UnknownTypeSchema<any, any>>(schema: S): RefSchema<Schema.Schema.Type<S> & Obj.Unknown>;\n} = refInternal.Ref as any;\n\nexport const Array = refInternal.RefArray;\n\n/**\n * TypeScript type for a Ref schema.\n * This is the type of the SCHEMA itself, not the runtime ref instance.\n * For the instance type, use `Ref.Ref<T>` from the Ref module.\n *\n * @example\n * ```ts\n * // Schema type annotation (rarely needed, usually inferred):\n * const refSchema: Ref.RefSchema<typeof Task> = Ref.Ref(Task);\n *\n * // Instance type annotation (use Ref.Ref instead):\n * const refInstance: Ref.Ref<Task> = Ref.make(task);\n * ```\n */\n// TODO(dmaretskyi): Investigate if we can remove this type.\n//                   Post DX-836 it will become just `Schema.Schema<Ref.Ref<T>>`.\n//                   NOTE: This could be Type.Ref<T> instead, but since it going to be removed, it's better to keep it here, self-contained.\nexport interface RefSchema<T extends Entity.Unknown> extends refInternal.RefSchema<T> {}\n\n/**\n * Extract reference target.\n */\nexport type Target<R extends Unknown> = R extends refInternal.Ref<infer T> ? T : never;\n\n/**\n * Reference resolver.\n */\nexport type Resolver = refInternal.RefResolver;\n\nexport const isRef: (value: unknown) => value is Unknown = refInternal.Ref.isRef;\n\nexport const make = refInternal.Ref.make;\n\n// TODO(dmaretskyi): Consider just allowing `make` to accept URI.\nexport const fromURI = (uri: URI.URI): refInternal.Ref<any> => refInternal.Ref.fromURI(uri);\n\nexport const hasEntityId = refInternal.Ref.hasEntityId;\n\n// TODO(wittjosiah): Factor out?\nexport const isRefType = (ast: SchemaAST.AST): boolean => {\n  return SchemaAST.getAnnotation<JsonSchema.JsonSchema>(ast, SchemaAST.JSONSchemaAnnotationId).pipe(\n    Option.flatMap((jsonSchema) => ('$id' in jsonSchema ? Option.some(jsonSchema) : Option.none())),\n    Option.flatMap((jsonSchema) => {\n      const { typename } = refInternal.getSchemaReference(jsonSchema) ?? {};\n      return typename ? Option.some(true) : Option.some(false);\n    }),\n    Option.getOrElse(() => false),\n  );\n};\n"],
  "mappings": ";;;;;;;;;;;;AAAA;;;aAAAA;EAAA;;;;;;AAMA,YAAYC,YAAY;AAExB,YAAYC,eAAe;AA4CpB,IAAMC,OAWGA;AAET,IAAMC,QAAoBC;AA+B1B,IAAMC,QAA0DH,IAAIG;AAEpE,IAAMC,OAAmBJ,IAAII;AAG7B,IAAMC,UAAU,CAACC,QAAmDN,IAAIK,QAAQC,GAAAA;AAEhF,IAAMC,cAA0BP,IAAIO;AAGpC,IAAMC,YAAY,CAACC,QAAAA;AACxB,SAAiBC,wBAAqCD,KAAeE,gCAAsB,EAAEC,KACpFC,eAAQ,CAACC,eAAgB,SAASA,aAAoBC,YAAKD,UAAAA,IAAqBE,YAAI,CAAA,GACpFH,eAAQ,CAACC,eAAAA;AACd,UAAM,EAAEG,SAAQ,IAAiBC,mBAAmBJ,UAAAA,KAAe,CAAC;AACpE,WAAOG,WAAkBF,YAAK,IAAA,IAAeA,YAAK,KAAA;EACpD,CAAA,GACOI,iBAAU,MAAM,KAAA,CAAA;AAE3B;",
  "names": ["Ref", "Option", "SchemaAST", "Ref", "Array", "RefArray", "isRef", "make", "fromURI", "uri", "hasEntityId", "isRefType", "ast", "getAnnotation", "JSONSchemaAnnotationId", "pipe", "flatMap", "jsonSchema", "some", "none", "typename", "getSchemaReference", "getOrElse"]
}
