{"version":3,"file":"column_overrided_recipe.cjs","names":["mergeSpecOverrides","isDataColumn","ColumnRecipe","createColumnOverriddenId","applySpecOverrides","rebrandLeafId"],"sources":["../../../src/columns/column_recipes/column_overrided_recipe.ts"],"sourcesContent":["import {\n  applySpecOverrides,\n  createColumnOverriddenId,\n  mergeSpecOverrides,\n  type ColumnOverriddenId,\n  type ColumnOverriddenKey,\n  type ColumnUniversalId,\n  type PColumnSpec,\n  type PObjectId,\n  type SpecOverrides,\n  type SpecQuery,\n} from \"@milaboratories/pl-model-common\";\nimport type { GlobalCfgRenderCtx } from \"../../render/internal\";\nimport { type DataColumnRecipe, type ColumnData, isDataColumn } from \"../data_column\";\nimport type { ColumnFieldStatus, ColumnResolutionStatus } from \"./types\";\nimport { ColumnRecipe } from \"./index\";\nimport { Column } from \"../column\";\nimport { rebrandLeafId } from \"./leaf_rebrand\";\n\n/**\n * A recipe whose id is not an Overridden wrap — valid `source` for Overridden.\n * Mirrors the spec-level invariant on {@link ColumnOverriddenKey.source}.\n */\ntype NonOverriddenColumn = Column<Exclude<ColumnUniversalId, ColumnOverriddenId>>;\n\n/**\n * Recipe wrapper that overlays {@link SpecOverrides} on top of any other\n * recipe. The id is `ColumnOverriddenKey { source: inner.id, specOverrides }`.\n *\n * Flat-merge invariant: `inner` is never another {@link ColumnOverriddenRecipe}.\n * Repeated overrides go through {@link wrap} / {@link withSpecs}, which merge\n * `specOverrides` at the current level and keep wrapper depth constant.\n *\n * This invariant matches the one enforced inside `unwrapOverrides` from\n * pl-model-common: it explicitly throws on a nested override-wrap.\n */\nexport class ColumnOverriddenRecipe implements ColumnRecipe<ColumnOverriddenId> {\n  /**\n   * Wrap any recipe with overrides. If `inner` is already a\n   * {@link ColumnOverriddenRecipe}, merges `overrides` into the existing ones\n   * and returns a new Overridden sharing the original `inner`. No\n   * `Overridden<Overridden<X>>` is produced.\n   */\n  static wrap(col: Column, overrides: SpecOverrides): ColumnOverriddenRecipe {\n    if (col instanceof ColumnOverriddenRecipe) {\n      return ColumnOverriddenRecipe.build(col.inner, mergeSpecOverrides(col.overrides, overrides));\n    }\n    // `inner` is not an Overridden wrap (the `instanceof` branch above handles\n    // that). Narrow the id union here so the constructor parameter stays\n    // honest with `ColumnOverriddenKey.source`.\n    return ColumnOverriddenRecipe.build(col as NonOverriddenColumn, overrides);\n  }\n\n  /**\n   * Pick the variant from the inner recipe: data-bearing inner →\n   * {@link DataColumnOverriddenRecipe}, otherwise the plain wrapper.\n   *\n   * The choice is **structural**, not a readiness check — it follows from\n   * which kind of recipe `inner` is, which in turn follows from the id alone\n   * (a bare `PObjectId` source is a leaf, anything else is not). So the same\n   * id always yields the same class, whether it was built via `withSpecs` or\n   * parsed from a string, and recipes stay value-objects that are equal iff\n   * their ids match.\n   *\n   * Re-deriving from `inner` on every build is also what keeps the merge path\n   * honest: `overridden.withSpecs(a).withSpecs(b)` cannot silently drop the\n   * data-bearing variant.\n   *\n   * Discriminated by method presence rather than `instanceof DataColumnImpl`\n   * to keep this module free of a runtime import cycle with `../data_column`.\n   * By the flat-merge invariant `inner` is never an Overridden, so the only\n   * recipe kind reaching here that carries `getData` is the leaf.\n   */\n  private static build(\n    inner: NonOverriddenColumn,\n    overrides: SpecOverrides,\n  ): ColumnOverriddenRecipe {\n    return isDataColumn(inner)\n      ? new DataColumnOverriddenRecipe(inner, overrides)\n      : new ColumnOverriddenRecipe(inner, overrides);\n  }\n\n  /**\n   * Static counterpart to {@link wrap}: returns the resolution status of a\n   * {@link ColumnOverriddenKey} without constructing the recipe. Overrides\n   * do not add references — status collapses to the source's status.\n   */\n  static getStatusByKey(\n    key: ColumnOverriddenKey,\n    opts: { ctx?: GlobalCfgRenderCtx } = {},\n  ): ColumnResolutionStatus {\n    return ColumnRecipe.getStatus(key.source, opts);\n  }\n\n  readonly id: ColumnOverriddenId;\n  private specCache?: { readonly value: PColumnSpec };\n  private queryCache?: { readonly value: SpecQuery };\n  private dataStatusCache?: { readonly value: ColumnFieldStatus };\n\n  protected constructor(\n    private readonly inner: NonOverriddenColumn,\n    private readonly overrides: SpecOverrides,\n  ) {\n    this.id = createColumnOverriddenId({\n      source: inner.id,\n      specOverrides: overrides,\n    });\n  }\n\n  /** Wrapped recipe — exposed so external walkers can descend. */\n  getInner(): Column {\n    return this.inner;\n  }\n\n  /**\n   * `inner` owns all references; overrides are a pure spec patch and do not\n   * introduce new column refs.\n   */\n  getReferencedIds(): PObjectId[] {\n    return this.inner.getReferencedIds();\n  }\n\n  getDataStatus(): ColumnFieldStatus {\n    if (this.dataStatusCache === undefined) {\n      this.dataStatusCache = { value: this.inner.getDataStatus() };\n    }\n    return this.dataStatusCache.value;\n  }\n\n  /**\n   * Take the inner spec and overlay our overrides. `applySpecOverrides`\n   * extracts the overrides from `this.id` itself.\n   */\n  getSpec(): PColumnSpec {\n    if (this.specCache === undefined) {\n      this.specCache = { value: applySpecOverrides(this.inner.getSpec(), this.id) };\n    }\n    return this.specCache.value;\n  }\n\n  /**\n   * Emit a client-side `specOverride` node wrapping the inner query.\n   *\n   * This is a structural-but-collapsible node: it carries the override\n   * patch through the query tree so consumers see the full recipe shape,\n   * but it is collapsed at the host boundary (`resolvePColumn`) before\n   * the query reaches pframe-engine. The engine never sees this node.\n   *\n   * Overrides do not change query topology — they are a pure spec patch.\n   */\n  getQuery(): SpecQuery {\n    if (this.queryCache === undefined) {\n      // Rebrand the inner column leaf to this.id (rich Overridden id) so\n      // filter/sort references emitted with this recipe's id match the\n      // leaf the engine sees. The specOverride node still wraps the leaf\n      // and is collapsed at the host boundary into a spec patch on the\n      // resolved PColumn.\n      this.queryCache = {\n        value: {\n          type: \"specOverride\",\n          input: rebrandLeafId(this.inner.getQuery(), this.inner.id, this.id),\n          override: this.overrides,\n        },\n      };\n    }\n    return this.queryCache.value;\n  }\n\n  /**\n   * Flat merge: new overrides merge with existing ones; `inner` is unchanged.\n   * The result is an Overridden of the same depth.\n   */\n  withSpecs(overrides: SpecOverrides): Column {\n    return ColumnOverriddenRecipe.wrap(this, overrides);\n  }\n}\n\n/**\n * The variant produced when the wrapped recipe is itself data-bearing.\n *\n * Overrides are a pure spec patch — they never reshape the data — so the inner\n * column's data stays consistent with this recipe's spec and is passed\n * straight through. Which variant you get is decided once, at construction:\n * see {@link build}.\n *\n * Not something callers name. Narrow with `hasReachableData(recipe)`, which\n * yields the {@link DataColumnRecipe} interface.\n */\nexport class DataColumnOverriddenRecipe\n  extends ColumnOverriddenRecipe\n  implements DataColumnRecipe<ColumnOverriddenId>\n{\n  /**\n   * Safe by construction: {@link build} only picks this class when the inner\n   * recipe exposes `getData`.\n   */\n  getData(): ColumnData {\n    return (this.getInner() as DataColumnRecipe).getData();\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoCA,IAAa,yBAAb,MAAa,uBAAmE;CAgE3D;CACA;;;;;;;CA1DnB,OAAO,KAAK,KAAa,WAAkD;EACzE,IAAI,eAAe,wBACjB,OAAO,uBAAuB,MAAM,IAAI,QAAA,GAAOA,gCAAAA,mBAAAA,CAAmB,IAAI,WAAW,SAAS,CAAC;EAK7F,OAAO,uBAAuB,MAAM,KAA4B,SAAS;CAC3E;;;;;;;;;;;;;;;;;;;;;CAsBA,OAAe,MACb,OACA,WACwB;EACxB,OAAOC,oBAAAA,aAAa,KAAK,IACrB,IAAI,2BAA2B,OAAO,SAAS,IAC/C,IAAI,uBAAuB,OAAO,SAAS;CACjD;;;;;;CAOA,OAAO,eACL,KACA,OAAqC,CAAC,GACd;EACxB,OAAOC,cAAAA,aAAa,UAAU,IAAI,QAAQ,IAAI;CAChD;CAEA;CACA;CACA;CACA;CAEA,YACE,OACA,WACA;EAFiB,KAAA,QAAA;EACA,KAAA,YAAA;EAEjB,KAAK,MAAA,GAAKC,gCAAAA,yBAAAA,CAAyB;GACjC,QAAQ,MAAM;GACd,eAAe;EACjB,CAAC;CACH;;CAGA,WAAmB;EACjB,OAAO,KAAK;CACd;;;;;CAMA,mBAAgC;EAC9B,OAAO,KAAK,MAAM,iBAAiB;CACrC;CAEA,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