import type { CommunityStructureChipSnapshotRow, CreateFlowState, } from "../../app/(app)/create/types"; import communityStructure from "../../messages/en/create/community/communityStructure.json"; import { migrateChipSnapshot, resolveChipIdList, resolveMaturityChipId, resolveOrgChipId, resolveScaleChipId, resolveSizeChipId, } from "./communityChipIds"; import { migrateSavedDeckIds } from "./idAliases"; /** Legacy `currentStep` values mapped to the current `CreateFlowStep` id. */ const LEGACY_CREATE_FLOW_STEP_RENAMES: Readonly> = { "right-rail": "decision-approaches", }; function chipPresets( rows: readonly { id: string; label: string }[], ): { id: string; label: string }[] { return rows.map((row) => ({ id: row.id, label: row.label })); } function migrateCommunityChips(state: CreateFlowState): CreateFlowState { const snapshots = state.communityStructureChipSnapshots; const maturity = migrateChipSnapshot( snapshots?.maturity as CommunityStructureChipSnapshotRow[] | undefined, chipPresets(communityStructure.maturityOptions), resolveMaturityChipId, ); const organizationTypes = migrateChipSnapshot( snapshots?.organizationTypes, chipPresets(communityStructure.organizationTypes), resolveOrgChipId, ); const scale = migrateChipSnapshot( snapshots?.scale, chipPresets(communityStructure.scaleOptions), resolveScaleChipId, ); return { ...state, selectedCommunitySizeIds: resolveChipIdList( state.selectedCommunitySizeIds, resolveSizeChipId, ), selectedOrganizationTypeIds: resolveChipIdList( state.selectedOrganizationTypeIds, resolveOrgChipId, ), selectedScaleIds: resolveChipIdList(state.selectedScaleIds, resolveScaleChipId), selectedMaturityIds: resolveChipIdList( state.selectedMaturityIds, resolveMaturityChipId, ), ...(snapshots || maturity || organizationTypes || scale ? { communityStructureChipSnapshots: { ...snapshots, ...(organizationTypes ? { organizationTypes } : {}), ...(scale ? { scale } : {}), ...(maturity ? { maturity } : {}), }, } : {}), }; } /** * Normalizes parsed draft JSON before merging into create-flow context. * Renames deprecated step ids and remaps deck and chip ids from before the remake. */ export function migrateLegacyCreateFlowState( raw: Record | null | undefined, ): CreateFlowState { if (!raw || typeof raw !== "object") return {}; const step = raw.currentStep; let state = raw as CreateFlowState; if (typeof step === "string") { const next = LEGACY_CREATE_FLOW_STEP_RENAMES[step]; if (next !== undefined) { state = { ...state, currentStep: next } as CreateFlowState; } } return migrateCommunityChips(migrateSavedDeckIds(state)); }