import type { AxisId } from "@milaboratories/pl-model-common"; import { canonicalizeAxisId, getAxisId, PColumnName } from "@milaboratories/pl-model-common"; import type { ColumnsSource } from "./column_providers"; import { ColumnsCollection, isColumnsCollection } from "./columns_collection"; import type { GlobalCfgRenderCtx } from "../render/internal"; import { TreeNodeAccessor } from "../render"; import { hasReachableData } from "./utils"; const RT_JSON = "PColumnData/Json"; const RT_JSON_PARTITIONED = "PColumnData/JsonPartitioned"; /** * Build an `axisValuesLabels` callback by discovering `pl7.app/label` columns * from the given source, eagerly materialising their value→label maps, and * keying them by canonical axis id. * * Modern replacement for `ctx.resultPool.findLabels` — uses the new * column-access mechanism (filtered {@link ColumnsCollection}) instead of * walking the raw result pool. * * Pair with {@link splitByAxes} (or any consumer expecting the * `(axisId) => Record` shape). * * Skips: * - recipes whose data is not directly readable (see `hasReachableData`); * - label columns whose `axesSpec.length !== 1`; * - label columns whose data resource type isn't `PColumnData/Json` / * `PColumnData/JsonPartitioned`. */ export function deriveAxisValuesLabels( source?: ColumnsCollection | (ColumnsCollection | ColumnsSource)[], opts?: { ctx?: GlobalCfgRenderCtx }, ): (axisId: AxisId) => Record | undefined { const collection = source === undefined ? ColumnsCollection(undefined, opts) : isColumnsCollection(source) ? source : ColumnsCollection(source, opts); const labelCols = collection .filter({ include: { name: [{ type: "exact", value: PColumnName.Label }] } }) .getColumns(); const byAxis = labelCols.reduce>>((map, col) => { if (!hasReachableData(col)) return map; const spec = col.getSpec(); if (spec.axesSpec.length !== 1) return map; const data = col.getData(); if (!(data instanceof TreeNodeAccessor)) return map; const labelMap = readLabelMap(data); if (!labelMap) return map; map.set(canonicalizeAxisId(getAxisId(spec.axesSpec[0])), labelMap); return map; }, new Map()); return (axisId) => byAxis.get(canonicalizeAxisId(axisId)); } function readLabelMap(acc: TreeNodeAccessor): undefined | Record { const rt = acc.resourceType.name; if (rt === RT_JSON) { const json = acc.getDataAsJson<{ data?: Record }>(); return json?.data ? parseLabelKeys(json.data) : undefined; } if (rt === RT_JSON_PARTITIONED) { return acc.listInputFields().reduce>((merged, partKey) => { const part = acc.resolve({ field: partKey, assertFieldType: "Input", ignoreError: true, }); if (!part) return merged; const json = part.getDataAsJson<{ data?: Record }>(); if (!json?.data) return merged; return Object.assign(merged, parseLabelKeys(json.data)); }, {}); } return undefined; } function parseLabelKeys(raw: Record): Record { return Object.entries(raw).reduce>((acc, [k, v]) => { try { const parsed = JSON.parse(k); acc[Array.isArray(parsed) ? parsed[0] : parsed] = v; } catch { acc[k] = v; } return acc; }, {}); }