import facetGroups from "../../data/create/customRule/_facetGroups.json"; import type { CreateFlowState } from "../../app/(app)/create/types"; import { resolveMaturityChipId, resolveOrgChipId, resolveScaleChipId, resolveSizeChipId, } from "./communityChipIds"; import { TYPED_ORG_TYPE_MARKER } from "./deckTypes"; const FACET_GROUPS = ["size", "orgType", "scale", "maturity"] as const; type FacetGroupId = (typeof FACET_GROUPS)[number]; const CHIP_TO_VALUE_BY_GROUP: Record< FacetGroupId, Record > = (() => { const out: Record> = { size: {}, orgType: {}, scale: {}, maturity: {}, }; for (const group of FACET_GROUPS) { const block = (facetGroups as Record)[group]; if (block && typeof block === "object" && "values" in block) { const values = (block as { values: Record }) .values; for (const [valueId, entry] of Object.entries(values)) { out[group][entry.chipId] = valueId; } } } return out; })(); const STATE_KEY_BY_GROUP: Record = { size: "selectedCommunitySizeIds", orgType: "selectedOrganizationTypeIds", scale: "selectedScaleIds", maturity: "selectedMaturityIds", }; const RESOLVE_BY_GROUP: Record string> = { size: resolveSizeChipId, orgType: resolveOrgChipId, scale: resolveScaleChipId, maturity: resolveMaturityChipId, }; function readChipIds(state: CreateFlowState, group: FacetGroupId): string[] { const value = state[STATE_KEY_BY_GROUP[group]]; return Array.isArray(value) ? (value as string[]) : []; } function catalogOrgTypeId(chipId: string): string | null { const resolved = resolveOrgChipId(chipId); const valueId = CHIP_TO_VALUE_BY_GROUP.orgType[resolved] ?? CHIP_TO_VALUE_BY_GROUP.orgType[chipId]; if (!valueId || valueId === "enterprise") return null; return valueId; } /** Catalog kinds keep their order. A typed main kind is a leading marker. */ function appendOrgTypeFacets(params: URLSearchParams, state: CreateFlowState) { const chipIds = readChipIds(state, "orgType"); const catalogIds: string[] = []; const seen = new Set(); for (const chipId of chipIds) { const valueId = catalogOrgTypeId(chipId); if (!valueId || seen.has(valueId)) continue; seen.add(valueId); catalogIds.push(valueId); } const firstIsCatalog = chipIds.length > 0 && catalogOrgTypeId(chipIds[0]) !== null; if (!firstIsCatalog && catalogIds.length > 0) { params.append("facet.orgType", TYPED_ORG_TYPE_MARKER); } for (const valueId of catalogIds) { params.append("facet.orgType", valueId); } } /** * Build `facet.size=…&facet.orgType=…` query string from Create Community * chip selections. Shared by `/api/create-flow/methods` and * `GET /api/templates` ranking (CR-88). */ export function buildFacetQueryString(state: CreateFlowState): string { const params = new URLSearchParams(); for (const group of FACET_GROUPS) { if (group === "orgType") { appendOrgTypeFacets(params, state); continue; } const valuesById = CHIP_TO_VALUE_BY_GROUP[group]; const resolve = RESOLVE_BY_GROUP[group]; const seen = new Set(); for (const chipId of readChipIds(state, group)) { const valueId = valuesById[resolve(chipId)] ?? valuesById[chipId]; if (valueId === "enterprise") continue; if (!valueId || seen.has(valueId)) continue; seen.add(valueId); params.append(`facet.${group}`, valueId); } } return params.toString(); }