import type { WorkOrderBoardCreatePayload } from "@/domain/work-orders/types/work-order-board"; import type { WorkOrderWizardDraft } from "@/domain/work-orders/types/work-order-wizard"; function trimOrUndefined(value: string | undefined): string | undefined { const trimmed = value?.trim(); return trimmed ? trimmed : undefined; } function normalizeExtraServices( primary: string | undefined, extras: string[], ): string[] | undefined { if (extras.length === 0) { return undefined; } const seen = new Set(); const primaryKey = primary?.toLowerCase() ?? ""; const out: string[] = []; for (const raw of extras) { const trimmed = raw.trim(); if (!trimmed) { continue; } const key = trimmed.toLowerCase(); if (key === primaryKey || seen.has(key)) { continue; } seen.add(key); out.push(trimmed); } return out.length > 0 ? out : undefined; } /** Mutates `payload` with primary/extra services, service notes, POC, tech phone, and vendor notes. */ export function assignWizardServicePocFields( payload: WorkOrderBoardCreatePayload, draft: WorkOrderWizardDraft, ): void { const primaryService = trimOrUndefined(draft.pm); if (primaryService) { payload.primaryService = primaryService; } const extraServices = normalizeExtraServices(primaryService, draft.extraServices); if (extraServices) { payload.extraServices = extraServices; } const serviceNotes = trimOrUndefined(draft.pmNote); if (serviceNotes) { payload.serviceNotes = serviceNotes; } const pocName = trimOrUndefined(draft.pocName); if (pocName) { payload.pocName = pocName; } const pocPhone = trimOrUndefined(draft.pocPhone); if (pocPhone) { payload.pocPhone = pocPhone; } const pocNotes = trimOrUndefined(draft.pocNotes); if (pocNotes) { payload.pocNotes = pocNotes; } const techPhone = trimOrUndefined(draft.techPhone); if (techPhone) { payload.techPhone = techPhone; } const vendorNotes = trimOrUndefined(draft.vendorNotes); if (vendorNotes) { payload.vendorNotes = vendorNotes; } }