{"version":3,"file":"columns_collection.cjs","names":["getService","ColumnRecipe","isNil","TreeNodeAccessor","isColumnProvider","getCfgRenderCtx","MainAccessorName","StagingAccessorName"],"sources":["../../src/columns/columns_collection.ts"],"sourcesContent":["import type {\n  CollectionHandle,\n  ColumnUniversalId,\n  ColumnsCollectionDriverModel,\n  ColumnsDiscoverOptions,\n  ColumnsFilterOptions,\n  DiscoverRootsOptions,\n  SerializedColumnsSource,\n} from \"@milaboratories/pl-model-common\";\nimport type { GlobalCfgRenderCtx } from \"../render/internal\";\nimport { MainAccessorName, StagingAccessorName } from \"../render/internal\";\nimport type { ColumnsSource } from \"./column_providers\";\nimport { isColumnProvider } from \"./column_providers\";\nimport { TreeNodeAccessor } from \"../render/accessor\";\nimport { getService } from \"../services/get_services\";\nimport { getCfgRenderCtx } from \"../internal\";\nimport { ColumnRecipe } from \"./column_recipes\";\nimport { isNil } from \"es-toolkit\";\n\nexport interface ColumnsCollectionDeps {\n  /** Render ctx used both for service resolution and for default ctx sources. */\n  readonly ctx?: GlobalCfgRenderCtx;\n  /** Override the resolved `columnsCollection` service — primarily for tests. */\n  readonly driver?: ColumnsCollectionDriverModel;\n}\n\n/**\n * Shorthand literals accepted inside `ColumnsCollection`'s `sources`\n * array, expanded against the active render ctx:\n *\n * - `\"result_pool\"`  – fan-out into the host's upstream-block result pool.\n * - `\"current_block\"` – main outputs + prerun (staging) accessors of the\n *   current block, when present.\n */\nexport type ColumnsSourceShorthand = \"result_pool\" | \"current_block\";\n\n/**\n * Build a {@link ColumnsCollection} from sandbox-side source descriptors.\n * Resolves the `columnsCollection` driver (either from `deps.driver` or via\n * `getService(\"columnsCollection\")`) and asks the host to mint a fresh\n * handle covering the supplied sources. Pass `sources === undefined` to\n * fall back to the active render ctx (main outputs + prerun + result pool).\n *\n * `sources` accepts {@link ColumnsSourceShorthand} string literals\n * (`\"result_pool\"`, `\"current_block\"`) for the common ctx-derived sources.\n */\nexport function ColumnsCollection(\n  sources?: (ColumnsCollection | ColumnsSource | ColumnsSourceShorthand)[],\n  deps?: ColumnsCollectionDeps,\n): ColumnsCollection {\n  const driver = deps?.driver ?? getService(\"columnsCollection\", { ctx: deps?.ctx });\n  const serialized: SerializedColumnsSource[] = sources\n    ? sources.flatMap((s) => toSerializedSources(s, deps?.ctx))\n    : defaultCtxSources(deps?.ctx);\n  return new ColumnsCollectionImpl(driver.create(serialized), driver);\n}\n\nexport function isColumnsCollection(value: unknown): value is ColumnsCollection {\n  return value instanceof ColumnsCollectionImpl;\n}\n\n/**\n * A root found by {@link ColumnsCollectionImpl.discoverRoots}: `leaf` is the\n * requested column exactly as `discover({ anchors: { r: root } })` would\n * return it — carrying the linker `path` and qualifications — so the table is\n * built from `root` as the anchor plus `leaf`.\n */\nexport interface DiscoveredRoot {\n  /** The column the leaf hangs off. */\n  readonly root: ColumnRecipe;\n  /** The requested column, discovered from the root. */\n  readonly leaf: ColumnRecipe;\n  /** Linker columns from the root to the leaf; empty when they join directly. */\n  readonly path: ColumnRecipe[];\n}\n\n/**\n * Sandbox proxy over the host-side `ColumnsCollection` driver. The driver\n * owns all column-set state; this object only carries an opaque\n * {@link CollectionHandle} plus a reference to the driver service.\n *\n * Construct via {@link ColumnsCollection} — the bare constructor is a\n * trivial `(handle, driver)` pair so that `addSource` / `discover` / `filter`\n * can rebind a freshly-minted handle without going through source\n * serialisation again.\n *\n * Every method returning a `ColumnsCollection` mints a fresh handle on the\n * host — the host VM bridge pins it to the active render ctx, so the sandbox\n * never has to manage refcounts.\n */\nexport class ColumnsCollectionImpl {\n  constructor(\n    public readonly handle: CollectionHandle,\n    private readonly driver: ColumnsCollectionDriverModel,\n  ) {}\n\n  isEmpty(): boolean {\n    return this.driver.isEmpty(this.handle);\n  }\n\n  isFinal(): boolean {\n    return this.driver.isFinal(this.handle);\n  }\n\n  getColumnIds(): ColumnUniversalId[] {\n    return this.driver.getColumns(this.handle);\n  }\n\n  getColumns(): ColumnRecipe[] {\n    return this.getColumnIds()\n      .map((id) => ColumnRecipe(id))\n      .filter((c): c is ColumnRecipe => !isNil(c));\n  }\n\n  addSource(source: ColumnsSource | ColumnsCollection): ColumnsCollection {\n    return new ColumnsCollectionImpl(\n      this.driver.addSource(this.handle, toSerializedSources(source)),\n      this.driver,\n    );\n  }\n\n  discover(options: ColumnsDiscoverOptions): ColumnsCollection {\n    return new ColumnsCollectionImpl(this.driver.discover(this.handle, options), this.driver);\n  }\n\n  filter(options: ColumnsFilterOptions): ColumnsCollection {\n    return new ColumnsCollectionImpl(this.driver.filter(this.handle, options), this.driver);\n  }\n\n  /**\n   * Finds the roots the given columns hang off: the columns that, used as\n   * `anchors` of {@link discover}, would reach them. One entry per root, leaf\n   * and linker path; entries whose columns are not yet resolvable are skipped.\n   */\n  discoverRoots(options: DiscoverRootsOptions): DiscoveredRoot[] {\n    return this.driver.discoverRoots(this.handle, options).flatMap((ids) => {\n      const root = ColumnRecipe(ids.root);\n      const leaf = ColumnRecipe(ids.leaf);\n      const path = ids.path\n        .map((id) => ColumnRecipe(id))\n        .filter((c): c is ColumnRecipe => !isNil(c));\n      if (isNil(root) || isNil(leaf) || path.length !== ids.path.length) return [];\n      return [{ root, leaf, path }];\n    });\n  }\n}\n\n/** Public type alias — value of the same name is the factory function above. */\nexport type ColumnsCollection = ColumnsCollectionImpl;\n\nfunction toSerializedSources(\n  source: ColumnsCollection | ColumnsSource | ColumnsSourceShorthand,\n  ctx?: GlobalCfgRenderCtx,\n): SerializedColumnsSource[] {\n  if (source === \"result_pool\") {\n    return [{ kind: \"result_pool\" }];\n  }\n  if (source === \"current_block\") {\n    return currentBlockSources(ctx);\n  }\n  if (source instanceof ColumnsCollectionImpl) {\n    return [{ kind: \"collection\", handle: source.handle }];\n  }\n  if (source instanceof TreeNodeAccessor) {\n    return [{ kind: \"accessor\", accessor: source.handle, path: source.resolvePath }];\n  }\n  if (isColumnArray(source)) {\n    return [{ kind: \"ids\", ids: source.columns.map((c) => c.id), isFinal: source.isFinal }];\n  }\n  if (isColumnProvider(source)) {\n    return [{ kind: \"ids\", ids: source.getColumns().map((c) => c.id), isFinal: source.isFinal() }];\n  }\n  throw new Error(\"ColumnsCollection: unrecognised ColumnsSource shape\");\n}\n\nfunction isColumnArray(source: unknown): source is {\n  columns: ReadonlyArray<{ id: ColumnUniversalId; spec?: unknown; data?: unknown }>;\n  isFinal: boolean;\n} {\n  if (typeof source !== \"object\" || source === null) return false;\n  const s = source as { columns?: unknown; isFinal?: unknown };\n  return Array.isArray(s.columns) && typeof s.isFinal === \"boolean\";\n}\n\nfunction currentBlockSources(ctx?: GlobalCfgRenderCtx): SerializedColumnsSource[] {\n  const renderCtx = ctx ?? getCfgRenderCtx();\n  const sources: SerializedColumnsSource[] = [];\n\n  const outputs = renderCtx.getAccessorHandleByName(MainAccessorName);\n  if (outputs !== undefined) {\n    sources.push({ kind: \"accessor\", accessor: outputs, path: [MainAccessorName] });\n  }\n  const prerun = renderCtx.getAccessorHandleByName(StagingAccessorName);\n  if (prerun !== undefined) {\n    sources.push({ kind: \"accessor\", accessor: prerun, path: [StagingAccessorName] });\n  }\n\n  return sources;\n}\n\nfunction defaultCtxSources(ctx?: GlobalCfgRenderCtx): SerializedColumnsSource[] {\n  return [...currentBlockSources(ctx), { kind: \"result_pool\" }];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA8CA,SAAgB,kBACd,SACA,MACmB;CACnB,MAAM,SAAS,MAAM,UAAUA,qBAAAA,WAAW,qBAAqB,EAAE,KAAK,MAAM,IAAI,CAAC;CACjF,MAAM,aAAwC,UAC1C,QAAQ,SAAS,MAAM,oBAAoB,GAAG,MAAM,GAAG,CAAC,IACxD,kBAAkB,MAAM,GAAG;CAC/B,OAAO,IAAI,sBAAsB,OAAO,OAAO,UAAU,GAAG,MAAM;AACpE;AAEA,SAAgB,oBAAoB,OAA4C;CAC9E,OAAO,iBAAiB;AAC1B;;;;;;;;;;;;;;;AA+BA,IAAa,wBAAb,MAAa,sBAAsB;CAEf;CACC;CAFnB,YACE,QACA,QACA;E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