import { pbiStatusFromApi, pbiStatusToApi, storyStatusFromApi, taskStatusFromApi, } from '@/lib/task-status' import type { BacklogPbi, BacklogStory, BacklogTask, ProductBacklogSnapshot, TaskDetail, } from '@/stores/product-workspace/types' import type { SprintWorkspaceSnapshot, SprintWorkspaceStory, SprintWorkspaceTask, SprintWorkspaceTaskDetail, } from '@/stores/sprint-workspace/types' export function normalizePbiStatusForStore(status: string): BacklogPbi['status'] { const dbStatus = pbiStatusFromApi(status) return dbStatus ? pbiStatusToApi(dbStatus) : (status as BacklogPbi['status']) } export function normalizeStoryStatusForStore(status: string): string { return storyStatusFromApi(status) ?? status } export function normalizeTaskStatusForStore(status: string): string { return taskStatusFromApi(status) ?? status } export function normalizeBacklogPbi(pbi: T): T { const status = normalizePbiStatusForStore(pbi.status) return status === pbi.status ? pbi : { ...pbi, status } } export function normalizeBacklogStory(story: T): T { const status = normalizeStoryStatusForStore(story.status) return status === story.status ? story : { ...story, status } } export function normalizeBacklogTask(task: T): T { const status = normalizeTaskStatusForStore(task.status) return status === task.status ? task : { ...task, status } } export function normalizeSprintStory(story: T): T { const status = normalizeStoryStatusForStore(story.status) return status === story.status ? story : { ...story, status } } export function normalizeSprintTask( task: T, ): T { const status = normalizeTaskStatusForStore(task.status) return status === task.status ? task : { ...task, status } } export function normalizeProductBacklogSnapshot( snapshot: ProductBacklogSnapshot, ): ProductBacklogSnapshot { return { ...snapshot, pbis: snapshot.pbis.map(normalizeBacklogPbi), storiesByPbi: mapRecordLists(snapshot.storiesByPbi, normalizeBacklogStory), tasksByStory: mapRecordLists(snapshot.tasksByStory, normalizeBacklogTask), } } export function normalizeSprintWorkspaceSnapshot( snapshot: SprintWorkspaceSnapshot, ): SprintWorkspaceSnapshot { return { ...snapshot, stories: snapshot.stories.map(normalizeSprintStory), tasksByStory: mapRecordLists(snapshot.tasksByStory, normalizeSprintTask), } } function mapRecordLists(record: Record, normalize: (item: T) => T): Record { const next: Record = {} for (const [id, list] of Object.entries(record)) { next[id] = list.map(normalize) } return next }