import type { Category } from "../../app/components/cards/Rule"; import type { ChipOption } from "../../app/components/controls/MultiSelect/MultiSelect.types"; import type { CommunityRuleSection } from "../../app/components/type/CommunityRule/CommunityRule.types"; import coreValuesMessages from "../../messages/en/create/customRule/coreValues.json"; import type { TemplateFacetGroupKey } from "./customRuleFacets"; import { readMethodPresetsForFacetGroup, templateCategoryToFacetGroupKey, } from "./customRuleFacets"; import { isDocumentEntry } from "./documentEntryGuards"; import { templateChipsForSlug, type TemplateChipSet } from "./templatePath"; export type { TemplateFacetGroupKey } from "./customRuleFacets"; function isDocumentSection(x: unknown): x is CommunityRuleSection { if (!x || typeof x !== "object") return false; const o = x as Record; if (typeof o.categoryName !== "string") return false; if (!Array.isArray(o.entries)) return false; return o.entries.every(isDocumentEntry); } /** * Normalize a section `categoryName` (as it appears in a template's `body`) * to the custom-rule facet-group key. Returns `null` for unknown categories. * Keys are matched case- and punctuation-insensitively so variations like * "Decision making" / "Decision-making" resolve to the same group. */ export function templateCategoryToGroupKey( categoryName: string, ): TemplateFacetGroupKey | null { return templateCategoryToFacetGroupKey(categoryName); } /** * Detail for a single chip rendered on a template review — includes the raw * entry fields plus the facet-group key so a click can open the matching * read-only modal (chip `label` is used to look up the preset method inside * the group). */ export interface TemplateChipDetail { chipId: string; chipLabel: string; categoryName: string; groupKey: TemplateFacetGroupKey | null; body: string; } /** * Maps API template `body` (published-rule document shape) to Rule category * rows **plus** a chipId → detail lookup for wiring chip clicks to the * read-only detail modal. */ export function templateBodyToReviewData(body: unknown): { categories: Category[]; chipDetailsByChipId: Record; } { const empty = { categories: [] as Category[], chipDetailsByChipId: {} }; if (!body || typeof body !== "object") return empty; const sections = (body as Record).sections; if (!Array.isArray(sections)) return empty; const categories: Category[] = []; const chipDetailsByChipId: Record = {}; for (const raw of sections) { if (!isDocumentSection(raw)) continue; const groupKey = templateCategoryToGroupKey(raw.categoryName); const chipOptions: ChipOption[] = raw.entries.map((e, i) => { const chipId = `${raw.categoryName}-${i}`; chipDetailsByChipId[chipId] = { chipId, chipLabel: e.title, categoryName: raw.categoryName, groupKey, body: e.body, }; return { id: chipId, label: e.title, state: "unselected", }; }); categories.push({ name: raw.categoryName, chipOptions, }); } return { categories, chipDetailsByChipId }; } /** * Backwards-compatible wrapper kept so existing consumers can still grab just * the rows when they don't need chip-click wiring. */ export function templateBodyToCategories(body: unknown): Category[] { return templateBodyToReviewData(body).categories; } type ReviewData = ReturnType; const CHIP_REVIEW_GROUPS: readonly { groupKey: TemplateFacetGroupKey; categoryName: string; idsOf: (_chips: TemplateChipSet) => readonly string[]; }[] = [ { groupKey: "coreValues", categoryName: "Values", idsOf: (chips) => chips.coreValues, }, { groupKey: "communication", categoryName: "Communication", idsOf: (chips) => chips.communication, }, { groupKey: "membership", categoryName: "Membership", idsOf: (chips) => chips.membership, }, { groupKey: "decisionApproaches", categoryName: "Decision-making", idsOf: (chips) => chips.decisionApproaches, }, { groupKey: "conflictManagement", categoryName: "Conflict management", idsOf: (chips) => chips.conflictManagement, }, ]; function coreValueLabelById(id: string): string | null { const values = (coreValuesMessages as { values?: unknown }).values; if (!Array.isArray(values)) return null; for (const raw of values) { if (!raw || typeof raw !== "object") continue; const row = raw as { id?: unknown; label?: unknown }; if (row.id === id && typeof row.label === "string" && row.label.trim()) { return row.label; } } return null; } /** Same order as `coreValuesChipsSnapshot` / final review: catalog list, then leftovers. */ function orderedCoreValueIds(ids: readonly string[]): string[] { const wanted = new Set(ids); const out: string[] = []; const values = (coreValuesMessages as { values?: unknown }).values; if (Array.isArray(values)) { for (const raw of values) { if (!raw || typeof raw !== "object") continue; const id = (raw as { id?: unknown }).id; if (typeof id === "string" && wanted.delete(id)) out.push(id); } } for (const id of ids) { if (wanted.has(id)) out.push(id); } return out; } function methodLabelById( groupKey: Exclude, id: string, ): string | null { const row = readMethodPresetsForFacetGroup(groupKey).find( (method) => method.id === id, ); return row?.label ?? null; } function templateChipsToReviewData(slug: string): ReviewData | null { const chips = templateChipsForSlug(slug); if (!chips) return null; const categories: Category[] = []; const chipDetailsByChipId: ReviewData["chipDetailsByChipId"] = {}; for (const group of CHIP_REVIEW_GROUPS) { const chipOptions: ChipOption[] = []; const ids = group.groupKey === "coreValues" ? orderedCoreValueIds(group.idsOf(chips)) : group.idsOf(chips); for (const id of ids) { const chipLabel = group.groupKey === "coreValues" ? coreValueLabelById(id) : methodLabelById(group.groupKey, id); if (!chipLabel) continue; chipDetailsByChipId[id] = { chipId: id, chipLabel, categoryName: group.categoryName, groupKey: group.groupKey, body: "", }; chipOptions.push({ id, label: chipLabel, state: "unselected", }); } if (chipOptions.length === 0) continue; categories.push({ name: group.categoryName, chipOptions, }); } return { categories, chipDetailsByChipId }; } /** * Detail-page chips. Prefer the remade catalog ids in templateChips.json so * the list matches final review; fall back to API body titles for unknown slugs. */ export function templateReviewDataForSlug( slug: string, body: unknown, ): ReviewData { return templateChipsToReviewData(slug) ?? templateBodyToReviewData(body); } /** * Summary line under tag rows: prefer API description; else first entry bodies (short). */ export function templateSummaryFromBody( description: string | null | undefined, body: unknown, ): string { const d = typeof description === "string" ? description.trim() : ""; if (d.length > 0) return d; if (!body || typeof body !== "object") return ""; const sections = (body as Record).sections; if (!Array.isArray(sections)) return ""; for (const s of sections) { if (!isDocumentSection(s)) continue; const first = s.entries[0]; if (isDocumentEntry(first)) { const main = first.body.trim(); if (main.length > 0) return main; const fromBlock = first.blocks?.[0]?.body?.trim(); if (fromBlock && fromBlock.length > 0) return fromBlock; } } return ""; }