{"version":3,"file":"data_column.cjs","names":["ColumnOverriddenRecipe","ColumnRegistry","getCtxProviders","isNil","createGlobalPObjectId","isPColumnSpec","isPlRef","isPColumn","LRUCache"],"sources":["../../src/columns/data_column.ts"],"sourcesContent":["import {\n  ColumnRegistry,\n  createGlobalPObjectId,\n  isPColumn,\n  isPColumnSpec,\n  isPlRef,\n  PlRef,\n  PObjectId,\n  type LeafEntry,\n  type PColumn,\n  type PColumnSpec,\n  type PObjectSpec,\n  type SpecOverrides,\n  type SpecQuery,\n} from \"@milaboratories/pl-model-common\";\nimport type { GlobalCfgRenderCtx, PColumnDataUniversal } from \"../render/internal\";\nimport { getCtxProviders } from \"./column_providers\";\nimport { isNil } from \"es-toolkit\";\nimport { LRUCache } from \"lru-cache\";\nimport { TreeNodeAccessor } from \"../render\";\nimport type {\n  ColumnFieldStatus,\n  ColumnRecipe,\n  ColumnRecipeId,\n  ColumnResolutionStatus,\n} from \"./column_recipes/types\";\nimport { ColumnOverriddenRecipe } from \"./column_recipes/column_overrided_recipe\";\n\nexport type DataColumnId = PObjectId;\nexport type ColumnData = undefined | PColumnDataUniversal;\nexport type { ColumnFieldStatus, ColumnResolutionStatus } from \"./column_recipes/types\";\n\n/**\n * A {@link ColumnRecipe} that can be read directly in the sandbox: its\n * {@link getData} is guaranteed to be consistent with {@link getSpec}.\n *\n * This is a *capability*, not a class. Only two recipe shapes carry it — a\n * bare leaf and a spec-override over a bare leaf — because those are the only\n * ones whose data still matches their spec. An axis-filtered recipe drops axes\n * from its spec while the underlying data keeps them, and a discovered recipe\n * only materialises after a join; neither can be read here, so neither\n * implements this interface.\n *\n * Narrow to it with `hasReachableData(recipe)` rather than testing for a concrete\n * class — which recipe classes exist is an implementation detail behind\n * `recipe.id`.\n */\nexport interface DataColumnRecipe<\n  ID extends ColumnRecipeId = ColumnRecipeId,\n> extends ColumnRecipe<ID> {\n  /** Column data, consistent with {@link ColumnRecipe.getSpec}. */\n  getData(): ColumnData;\n}\n\n/**\n * The leaf-recipe building block: a {@link DataColumnRecipe} whose `id` is a bare\n * {@link PObjectId} and whose readers are bound to a single tree-accessor\n * leaf. Layered encodings (Overridden / Discovered / Filtered) are reified\n * through their dedicated recipe classes and reference leaf columns by id.\n *\n * Internal — consumers hold the {@link DataColumnRecipe} interface and construct\n * through the {@link DataColumnRecipe} dispatcher.\n */\nexport class DataColumnImpl implements DataColumnRecipe<PObjectId> {\n  private specCache?: { readonly value: PColumnSpec };\n  private dataCache?: { readonly value: ColumnData };\n  private dataStatusCache?: { readonly value: ColumnFieldStatus };\n\n  private constructor(\n    public readonly id: PObjectId,\n    private readonly options: {\n      getSpec: () => PColumnSpec;\n      getData: () => ColumnData;\n      getDataStatus: () => ColumnFieldStatus;\n    },\n  ) {}\n\n  /** A leaf-recipe references exactly one column — its own id. */\n  getReferencedIds(): PObjectId[] {\n    return [this.id];\n  }\n\n  getSpec(): PColumnSpec {\n    if (this.specCache === undefined) {\n      this.specCache = { value: this.options.getSpec() };\n    }\n    return this.specCache.value;\n  }\n\n  /** Leaf-shaped — points straight at `this.id`. */\n  getQuery(): SpecQuery {\n    return { type: \"column\", column: this.id } as SpecQuery;\n  }\n\n  getData(): ColumnData {\n    if (this.dataCache === undefined) this.dataCache = { value: this.options.getData() };\n    return this.dataCache.value;\n  }\n\n  getDataStatus(): ColumnFieldStatus {\n    if (this.dataStatusCache === undefined) {\n      this.dataStatusCache = { value: this.options.getDataStatus() };\n    }\n    return this.dataStatusCache.value;\n  }\n\n  /**\n   * Overlay overrides → produces a {@link ColumnOverriddenRecipe} wrapping\n   * this leaf. The leaf itself stays bare (id remains a plain PObjectId);\n   * layering lives entirely in the recipe wrappers.\n   */\n  withSpecs(overrides: SpecOverrides): ColumnRecipe {\n    return ColumnOverriddenRecipe.wrap(this, overrides);\n  }\n\n  /**\n   * Resolve via the ambient {@link ColumnRegistry}. Spec is resolved eagerly:\n   * returns `undefined` unless the id names a column whose spec is readable in\n   * this ctx — while it is resolving, when it is absent, and when the id names\n   * an export that is not a column (a `kind: \"File\"` object, for example).\n   * Data and dataStatus stay lazy.\n   */\n  static fromId(\n    id: PObjectId,\n    { ctx }: { ctx?: GlobalCfgRenderCtx } = {},\n  ): undefined | DataColumnRecipe<PObjectId> {\n    const leaf = new ColumnRegistry(getCtxProviders({ ctx })).resolve(id);\n    if (isNil(leaf)) return undefined;\n    return DataColumnImpl.fromAccessor(leaf);\n  }\n\n  /** {@link PlRef} wrapper over {@link fromId}. */\n  static fromPlRef(ref: PlRef): undefined | DataColumnRecipe<PObjectId> {\n    return DataColumnImpl.fromId(createGlobalPObjectId(ref.blockId, ref.name));\n  }\n\n  /**\n   * Bind directly to an accessor-backed {@link LeafEntry} — no registry.\n   * Returns `undefined` unless the leaf's spec is readable and is a column\n   * spec: while the spec is resolving, when it is absent, and for an export\n   * that is not a column.\n   */\n  static fromAccessor(entry: LeafEntry<TreeNodeAccessor>): undefined | DataColumnRecipe<PObjectId> {\n    const spec = readSpec(entry);\n    if (spec === undefined || !isPColumnSpec(spec)) return undefined;\n    return new DataColumnImpl(entry.id, {\n      getSpec: () => spec,\n      getData: () => readDataAccessor(entry),\n      getDataStatus: () => readDataStatus(entry),\n    });\n  }\n\n  /**\n   * Wrap a materialised {@link PColumn}. If the input is already a\n   * {@link DataColumnRecipe} leaf it is returned as-is.\n   */\n  static fromColumn(\n    column: PColumn<ColumnData> | DataColumnRecipe<PObjectId>,\n  ): DataColumnRecipe<PObjectId> {\n    if (column instanceof DataColumnImpl) return column;\n    // `DataColumnRecipe` is an interface, so `instanceof` above does not remove it\n    // from the union. The only implementation carrying a bare `PObjectId` is\n    // `DataColumnImpl` (wrappers expose `ColumnUniversalId`), so anything\n    // reaching here is a materialised `PColumn`.\n    const pColumn = column as PColumn<ColumnData>;\n    return new DataColumnImpl(pColumn.id, {\n      getSpec: () => pColumn.spec,\n      getData: () => pColumn.data,\n      getDataStatus: () => \"present\",\n    });\n  }\n\n  /**\n   * Distinguishes `present` / `resolving` / `absent` for a {@link PObjectId}\n   * in the active render ctx. Falls back to the registry's `isFinal()`\n   * when the id has no entry — only then we can say `absent` instead of\n   * `resolving`. The status tells only whether the spec is readable: for an\n   * export that is not a column it is `present`, and the factories return\n   * `undefined`.\n   */\n  static getStatusById(\n    id: PObjectId,\n    { ctx }: { ctx?: GlobalCfgRenderCtx } = {},\n  ): ColumnResolutionStatus {\n    const registry = new ColumnRegistry(getCtxProviders({ ctx }));\n    const leaf = registry.resolve(id);\n    if (isNil(leaf)) return registry.isFinal() ? \"absent\" : \"resolving\";\n    return getLeafEntryStatus(leaf);\n  }\n\n  /** {@link PlRef} wrapper over {@link getStatusById}. */\n  static getStatusByPlRef(\n    ref: PlRef,\n    opts: { ctx?: GlobalCfgRenderCtx } = {},\n  ): ColumnResolutionStatus {\n    return DataColumnImpl.getStatusById(createGlobalPObjectId(ref.blockId, ref.name), opts);\n  }\n\n  /** No registry — reads straight off the entry's accessor. */\n  static getStatusByAccessor(entry: LeafEntry<TreeNodeAccessor>): ColumnResolutionStatus {\n    return getLeafEntryStatus(entry);\n  }\n}\n\n/** Anything the {@link DataColumnRecipe} dispatcher can build a leaf from. */\nexport type DataColumnSource =\n  | PObjectId\n  | PlRef\n  | LeafEntry<TreeNodeAccessor>\n  | PColumn<ColumnData>\n  | DataColumnRecipe<PObjectId>;\n\n/**\n * Unified dispatcher — picks the right `DataColumnImpl.fromX` by source\n * shape. For ambiguous inputs callers can still use the explicit factories\n * (also attached as properties: `DataColumn.fromId`, `.fromPlRef`,\n * `.fromAccessor`, `.fromColumn`).\n */\nfunction DataColumnDispatch(\n  source: DataColumnSource,\n  opts: { ctx?: GlobalCfgRenderCtx } = {},\n): undefined | DataColumnRecipe<PObjectId> {\n  if (typeof source === \"string\") return DataColumnImpl.fromId(source, opts);\n  if (source instanceof DataColumnImpl) return source;\n  // `DataColumnRecipe` is an interface — `instanceof` above cannot remove it from\n  // the union, and only `DataColumnImpl` implements it with a bare id.\n  const rest = source as LeafEntry<TreeNodeAccessor> | PlRef | PColumn<ColumnData>;\n  if (\"accessor\" in rest) return DataColumnImpl.fromAccessor(rest);\n  if (isPlRef(rest)) return DataColumnImpl.fromPlRef(rest);\n  if (isPColumn(rest)) return DataColumnImpl.fromColumn(rest);\n  throw new Error(\"DataColumn: unknown source shape\");\n}\n\n/**\n * Polymorphic counterpart to {@link DataColumnDispatch}: returns the\n * {@link ColumnResolutionStatus} for any factory input without constructing\n * the recipe. For already-materialised sources ({@link PColumn} value,\n * existing {@link DataColumnRecipe}) status is `present` by construction.\n */\nfunction DataColumnGetStatus(\n  source: DataColumnSource,\n  opts: { ctx?: GlobalCfgRenderCtx } = {},\n): ColumnResolutionStatus {\n  if (typeof source === \"string\") return DataColumnImpl.getStatusById(source, opts);\n  if (source instanceof DataColumnImpl) return \"present\";\n  const rest = source as LeafEntry<TreeNodeAccessor> | PlRef | PColumn<ColumnData>;\n  if (\"accessor\" in rest) return DataColumnImpl.getStatusByAccessor(rest);\n  if (isPlRef(rest)) return DataColumnImpl.getStatusByPlRef(rest, opts);\n  if (isPColumn(rest)) return \"present\";\n  throw new Error(\"DataColumn.getStatus: unknown source shape\");\n}\n\nexport const DataColumn = Object.assign(DataColumnDispatch, {\n  fromId: DataColumnImpl.fromId,\n  fromPlRef: DataColumnImpl.fromPlRef,\n  fromAccessor: DataColumnImpl.fromAccessor,\n  fromColumn: DataColumnImpl.fromColumn,\n  getStatus: DataColumnGetStatus,\n  getStatusById: DataColumnImpl.getStatusById,\n  getStatusByPlRef: DataColumnImpl.getStatusByPlRef,\n  getStatusByAccessor: DataColumnImpl.getStatusByAccessor,\n});\n\n/**\n * Type-guard narrowing to a bare leaf — a {@link DataColumnRecipe} whose `id` is a\n * plain {@link PObjectId}.\n */\nexport function isDataColumn(value: unknown): value is DataColumnRecipe<PObjectId> {\n  return value instanceof DataColumnImpl;\n}\n\nconst readSpecAccessor = memoizeByEntry(\n  ({ accessor, name }: LeafEntry<TreeNodeAccessor>): undefined | TreeNodeAccessor =>\n    accessor.traverse({ field: `${name}.spec`, assertFieldType: \"Input\", ignoreError: true }),\n);\n\n/** The leaf's spec, or `undefined` until the spec resource has data. */\nconst readSpec = memoizeByEntry((entry: LeafEntry<TreeNodeAccessor>): undefined | PObjectSpec => {\n  const spec = readSpecAccessor(entry);\n  if (isNil(spec) || !spec.hasData()) return undefined;\n  return spec.getDataAsJson<PObjectSpec>();\n});\n\n/**\n * Per-entry counterpart to {@link readDataStatus}: tells whether the leaf's\n * **spec** can be read in this ctx, and — for the negative cases —\n * distinguishes `resolving` from `absent` via `getInputsLocked()`.\n *\n *  - spec field not yet on the entry's accessor + inputs locked → `absent`\n *  - spec field not yet on the entry's accessor + still resolving → `resolving`\n *  - spec resource present but bytes not yet written → `resolving`\n *    (transient — the spec resource is connected, just unfilled)\n *  - spec resource present and `hasData()` → `present`\n */\nfunction getLeafEntryStatus(entry: LeafEntry<TreeNodeAccessor>): ColumnResolutionStatus {\n  const spec = readSpecAccessor(entry);\n  if (isNil(spec)) return entry.accessor.getInputsLocked() ? \"absent\" : \"resolving\";\n  if (!spec.hasData()) return \"resolving\";\n  return \"present\";\n}\n\nconst readDataAccessor = memoizeByEntry(\n  ({ accessor, name }: LeafEntry<TreeNodeAccessor>): undefined | TreeNodeAccessor =>\n    accessor.traverse({ field: `${name}.data`, assertFieldType: \"Input\", ignoreError: true }),\n);\n\nconst readDataStatus = memoizeByEntry(\n  ({ accessor, name }: LeafEntry<TreeNodeAccessor>): ColumnFieldStatus => {\n    if (accessor.listInputFields().includes(`${name}.data`)) return \"present\";\n    return accessor.getInputsLocked() ? \"absent\" : \"resolving\";\n  },\n);\n\nfunction memoizeByEntry<R>(\n  fn: (entry: LeafEntry<TreeNodeAccessor>) => R,\n): (entry: LeafEntry<TreeNodeAccessor>) => R {\n  const cache = new LRUCache<string, { value: R }>({ max: 1000 });\n  return (entry) => {\n    const key = `${entry.accessor.handle}:${entry.name}`;\n    let hit = cache.get(key);\n    if (!hit) cache.set(key, (hit = { value: fn(entry) }));\n    return hit.value;\n  };\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