import type { CreateFlowState } from "../../app/(app)/create/types"; import type { DeckId } from "./deckTypes"; import templateChipsFile from "../../data/templates/templateChips.json"; export type TemplatePathMode = "customize" | "useWithoutChanges"; export type TemplatePath = { slug: string; mode: TemplatePathMode; }; export type TemplateChipSet = Record; /** * Persistent template-path pin. Kept for the rest of the wizard so decks skip * the engine, final review shows identity fields, and Back knows whether to * return to the template or to the conflict deck. */ export function templatePathFromState( state: Pick, ): TemplatePath | null { const pending = state.pendingTemplateAction; if (!pending) return null; const slug = pending.slug.trim(); if (!slug) return null; if (pending.mode !== "customize" && pending.mode !== "useWithoutChanges") { return null; } return { slug, mode: pending.mode }; } export function isTemplateCreatePath( state: Pick, ): boolean { return templatePathFromState(state) !== null; } export function templateChipsForSlug(slug: string): TemplateChipSet | null { const templates = ( templateChipsFile as { templates: Record } ).templates; const chips = templates[slug]; return chips ?? null; } export function templateCardIdsForDeck( slug: string, deck: DeckId, ): string[] { const chips = templateChipsForSlug(slug); if (!chips) return []; return [...chips[deck]]; } export function templateValueIdsForSlug(slug: string): string[] { const chips = templateChipsForSlug(slug); if (!chips) return []; return [...chips.coreValues]; } export function templateChipCounts(slug: string): { values: number; cards: number; } | null { const chips = templateChipsForSlug(slug); if (!chips) return null; return { values: chips.coreValues.length, cards: chips.communication.length + chips.membership.length + chips.decisionApproaches.length + chips.conflictManagement.length, }; } /** Put `firstIds` at the head (stable), then the remaining items in their original order. */ export function orderIdsFirst( items: readonly T[], firstIds: readonly string[], ): T[] { const byId = new Map(items.map((item) => [item.id, item] as const)); const seen = new Set(); const head: T[] = []; for (const id of firstIds) { const item = byId.get(id); if (!item || seen.has(id)) continue; seen.add(id); head.push(item); } return [...head, ...items.filter((item) => !seen.has(item.id))]; }