{"version":3,"file":"columns.cjs","names":["PColumnCollection","getNormalizedAxesList","getAxisId","canonicalizeJson","enrichCompatible","isLinkerColumn","getAvailableWithLinkersAxes","matchAxisId","isLabelColumn","ColumnsCollection","extractPObjectId"],"sources":["../../src/pframe_utils/columns.ts"],"sourcesContent":["import type { PColumn, PColumnSpec, PFrameDef, PObjectId } from \"@milaboratories/pl-model-common\";\nimport {\n  getNormalizedAxesList,\n  getAxisId,\n  canonicalizeJson,\n  isLinkerColumn,\n  matchAxisId,\n  isLabelColumn,\n  extractPObjectId,\n} from \"@milaboratories/pl-model-common\";\nimport type { AxesVault } from \"./axes\";\nimport { enrichCompatible, getAvailableWithLinkersAxes } from \"./axes\";\nimport type { RenderCtxBase, PColumnDataUniversal } from \"../render\";\nimport { PColumnCollection } from \"../render\";\nimport { ColumnsCollection } from \"../columns\";\n\nexport function getAllRelatedColumns<A, U>(\n  ctx: RenderCtxBase<A, U>,\n  predicate: (spec: PColumnSpec) => boolean,\n): PFrameDef<PColumn<undefined | PColumnDataUniversal>> {\n  // if current block doesn't produce own columns then use all columns from result pool\n  const columns = new PColumnCollection();\n  columns.addColumnProvider(ctx.resultPool);\n  const allColumns =\n    columns.getColumns(predicate, {\n      dontWaitAllData: true,\n      overrideLabelAnnotation: false,\n    }) ?? [];\n\n  const allAxes: AxesVault = new Map(\n    allColumns\n      .flatMap((column) => getNormalizedAxesList(column.spec.axesSpec))\n      .map((axisSpec) => {\n        const axisId = getAxisId(axisSpec);\n        return [canonicalizeJson(axisId), axisSpec];\n      }),\n  );\n\n  // additional columns are duplicates with extra fields in domains for compatibility if there are ones with partial match\n  const extendedColumns = enrichCompatible(allAxes, allColumns);\n\n  return extendedColumns;\n}\n\nexport function getRelatedColumns<A, U>(\n  ctx: RenderCtxBase<A, U>,\n  {\n    columns: rootColumns,\n    predicate,\n  }: {\n    columns: PColumn<undefined | PColumnDataUniversal>[];\n    predicate: (spec: PColumnSpec) => boolean;\n  },\n): PFrameDef<PColumn<undefined | PColumnDataUniversal>> {\n  // if current block has its own columns then take from result pool only compatible with them\n  const columns = new PColumnCollection();\n  columns.addColumnProvider(ctx.resultPool);\n  columns.addColumns(rootColumns);\n\n  // all possible axes from block columns\n  const blockAxes: AxesVault = new Map();\n  // axes from block columns and compatible result pool columns\n  const allAxes: AxesVault = new Map();\n  for (const c of rootColumns) {\n    for (const spec of getNormalizedAxesList(c.spec.axesSpec)) {\n      const aid = getAxisId(spec);\n      blockAxes.set(canonicalizeJson(aid), spec);\n      allAxes.set(canonicalizeJson(aid), spec);\n    }\n  }\n\n  // all linker columns always go to pFrame - even it's impossible to use some of them they all are hidden\n  const linkerColumns =\n    columns.getColumns((spec) => predicate(spec) && isLinkerColumn(spec), {\n      dontWaitAllData: true,\n    }) ?? [];\n  const availableWithLinkersAxes = getAvailableWithLinkersAxes(linkerColumns, blockAxes);\n\n  // all possible axes from connected linkers\n  for (const item of availableWithLinkersAxes) {\n    blockAxes.set(...item);\n    allAxes.set(...item);\n  }\n\n  const blockAxesArr = Array.from(blockAxes.values());\n  // all compatible with block columns but without label columns\n  let compatibleWithoutLabels = (\n    columns.getColumns(\n      (spec) =>\n        predicate(spec) &&\n        spec.axesSpec.some((axisSpec) => {\n          const axisId = getAxisId(axisSpec);\n          return blockAxesArr.some((selectorAxisSpec) =>\n            matchAxisId(getAxisId(selectorAxisSpec), axisId),\n          );\n        }),\n      { dontWaitAllData: true, overrideLabelAnnotation: false },\n    ) ?? []\n  ).filter((column) => !isLabelColumn(column.spec));\n\n  // extend axes set for label columns request\n  for (const c of compatibleWithoutLabels) {\n    for (const spec of getNormalizedAxesList(c.spec.axesSpec)) {\n      const aid = getAxisId(spec);\n      allAxes.set(canonicalizeJson(aid), spec);\n    }\n  }\n\n  const allAxesArr = Array.from(allAxes.values());\n  // extend allowed columns - add columns thad doesn't have axes from block, but have all axes in 'allAxes' list (that means all axes from linkers or from 'hanging' of other selected columns)\n  compatibleWithoutLabels = (\n    columns.getColumns(\n      (spec) =>\n        predicate(spec) &&\n        spec.axesSpec.every((axisSpec) => {\n          const axisId = getAxisId(axisSpec);\n          return allAxesArr.some((selectorAxisSpec) =>\n            matchAxisId(getAxisId(selectorAxisSpec), axisId),\n          );\n        }),\n      { dontWaitAllData: true, overrideLabelAnnotation: false },\n    ) ?? []\n  ).filter((column) => !isLabelColumn(column.spec));\n\n  // label columns must be compatible with full set of axes - block axes and axes from compatible columns from result pool\n  const compatibleLabels = (\n    columns.getColumns(\n      (spec) =>\n        predicate(spec) &&\n        spec.axesSpec.some((axisSpec) => {\n          const axisId = getAxisId(axisSpec);\n          return allAxesArr.some((selectorAxisSpec) =>\n            matchAxisId(getAxisId(selectorAxisSpec), axisId),\n          );\n        }),\n      { dontWaitAllData: true, overrideLabelAnnotation: false },\n    ) ?? []\n  ).filter((column) => isLabelColumn(column.spec));\n\n  const compatible = [...compatibleWithoutLabels, ...compatibleLabels];\n\n  // Drop result-pool columns that share an axis with the block but cannot join it.\n  const integrableIds = discoverIntegrableColumnIds(ctx, rootColumns);\n  const rootIds = new Set(rootColumns.map((c) => c.id));\n  const connected = compatible.filter(\n    (c) => rootIds.has(c.id) || isLinkerColumn(c.spec) || (integrableIds?.has(c.id) ?? true),\n  );\n\n  // additional columns are duplicates with extra fields in domains for compatibility if there are ones with partial match\n  const extendedColumns = enrichCompatible(blockAxes, connected);\n\n  return extendedColumns;\n}\n\n/** Column ids that discovery confirms integrate with the block, or `undefined` when it can't decide. */\nfunction discoverIntegrableColumnIds<A, U>(\n  ctx: RenderCtxBase<A, U>,\n  rootColumns: PColumn<undefined | PColumnDataUniversal>[],\n): Set<PObjectId> | undefined {\n  const anchorSpecs = Array.from(\n    new Map(rootColumns.map((c) => [canonicalizeJson(c.spec), c.spec])).values(),\n  );\n  if (anchorSpecs.length === 0) return undefined;\n\n  try {\n    const columns = ColumnsCollection(undefined, { ctx: ctx.ctx })\n      .discover({\n        mode: \"enrichment\",\n        maxHops: 4,\n        anchors: Object.fromEntries(anchorSpecs.map((spec, i) => [`anchor_${i}`, spec])),\n      })\n      .getColumns();\n    return new Set(columns.map((c) => extractPObjectId(c.id)));\n  } catch {\n    return undefined;\n  }\n}\n"],"mappings":";;;;;;;AAgBA,SAAgB,qBACd,KACA,WACsD;CAEtD,MAAM,UAAU,IAAIA,0BAAAA,kBAAkB;CACtC,QAAQ,kBAAkB,IAAI,UAAU;CACxC,MAAM,aACJ,QAAQ,WAAW,WAAW;EAC5B,iBAAiB;EACjB,yBAAyB;CAC3B,CAAC,KAAK,CAAC;CAET,MAAM,UAAqB,IAAI,IAC7B,WACG,SAAS,YAAA,GAAWC,gCAAAA,sBAAAA,CAAsB,OAAO,KAAK,QAAQ,CAAC,CAAC,CAChE,KAAK,aAAa;EACjB,MAAM,UAAA,GAASC,gCAAAA,UAAAA,CAAU,QAAQ;EACjC,OAAO,EAAA,GAACC,gCAAAA,iBAAAA,CAAiB,MAAM,GAAG,QAAQ;CAC5C,CAAC,CACL;CAKA,OAFwBC,aAAAA,iBAAiB,SAAS,UAE7B;AACvB;AAEA,SAAgB,kBACd,KACA,EACE,SAAS,aACT,aAKoD;CAEtD,MAAM,UAAU,IAAIJ,0BAAAA,kBAAkB;CACtC,QAAQ,kBAAkB,IAAI,UAAU;CACxC,QAAQ,WAAW,WAAW;CAG9B,MAAM,4BAAuB,IAAI,IAAI;CAErC,MAAM,0BAAqB,IAAI,IAAI;CACnC,KAAK,MAAM,KAAK,aACd,KAAK,MAAM,SAAA,GAAQC,gCAAAA,sBAAAA,CAAsB,EAAE,KAAK,QAAQ,GAAG;EACzD,MAAM,OAAA,GAAMC,gCAAAA,UAAAA,CAAU,IAAI;EAC1B,UAAU,KAAA,GAAIC,gCAAAA,iBAAAA,CAAiB,GAAG,GAAG,IAAI;EACzC,QAAQ,KAAA,GAAIA,gCAAAA,iBAAAA,CAAiB,GAAG,GAAG,IAAI;CACzC;CAIF,MAAM,gBACJ,QAAQ,YAAY,SAAS,UAAU,IAAI,MAAA,GAAKE,gCAAAA,eAAAA,CAAe,IA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