import type { Location, LocationListItem, LocationSite } from "@/domain/locations/types/location"; function readString(record: Record, ...keys: string[]): string { for (const key of keys) { const value = record[key]; if (typeof value === "string") { return value; } if (typeof value === "number") { return String(value); } } return ""; } function readId(record: Record): string | number { const value = record.id ?? record.Id; if (typeof value === "string" || typeof value === "number") { return value; } return ""; } function readOptionalId(record: Record): string | number { const value = record.accountId ?? record.AccountId; if (typeof value === "string" || typeof value === "number") { return value; } return ""; } export function formatLocationSiteLabel( site: Pick, ): string { const place = [site.city, site.state].filter(Boolean).join(", "); if (site.code && place) { return `${site.code} ยท ${place}`; } return site.code || place || ""; } /** Table cell label: `BK5 . Dallas, TX` (period separator). */ export function formatSiteTableLabel(params: { code?: string; city?: string; state?: string; fallbackLocation?: string; }): string { const code = params.code?.trim() ?? ""; const place = [params.city, params.state].filter(Boolean).join(", ") || (params.fallbackLocation?.trim() ?? ""); if (code && place) { return `${code} . ${place}`; } return code || place || ""; } export function formatSitePlace(city?: string, state?: string, fallbackLocation = ""): string { return [city, state].filter(Boolean).join(", ") || fallbackLocation.trim() || ""; } /** Wizard preview: `3811 Distribution Dr, Dallas, TX 70068` */ export function formatLocationAddressPreview( location: Pick, ): string { const street = location.address?.trim() ?? ""; const city = location.city?.trim() ?? ""; const stateZip = [location.state?.trim(), location.zipCode?.trim()].filter(Boolean).join(" "); const cityStateZip = [city, stateZip].filter(Boolean).join(", "); if (street && cityStateZip) { return `${street}, ${cityStateZip}`; } return street || cityStateZip || ""; } export function mapLocationSite(raw: unknown): LocationSite { const item = (typeof raw === "object" && raw !== null ? raw : {}) as Record; return { id: readId(item), code: readString(item, "code", "Code", "siteCode", "SiteCode"), city: readString(item, "city", "City"), state: readString(item, "state", "State"), }; } export function mapLocationSites(rawItems: unknown[]): LocationSite[] { return rawItems.map(mapLocationSite); } export function mapLocationListItem(raw: unknown): LocationListItem { const item = (typeof raw === "object" && raw !== null ? raw : {}) as Record; return { id: readId(item), name: readString(item, "name", "Name"), address: readString(item, "address", "Address"), city: readString(item, "city", "City"), state: readString(item, "state", "State"), zipCode: readString(item, "zipCode", "ZipCode", "Zip"), phone: readString(item, "phone", "Phone"), contact: readString(item, "contact", "Contact", "contactPerson", "ContactPerson"), contactEmail: readString(item, "contactEmail", "ContactEmail"), status: readString(item, "status", "Status"), accountId: readOptionalId(item), }; } export function mapLocationListItems(rawItems: unknown[]): LocationListItem[] { return rawItems.map(mapLocationListItem); } export function mapLocation(raw: unknown): Location { const item = (typeof raw === "object" && raw !== null ? raw : {}) as Record; const address = readString(item, "address", "Address") || [readString(item, "address1", "Address1"), readString(item, "address2", "Address2")] .filter(Boolean) .join(", ") || undefined; return { id: readId(item) || undefined, name: readString(item, "name", "Name") || undefined, address: address || undefined, city: readString(item, "city", "City") || undefined, state: readString(item, "state", "State") || undefined, zipCode: readString(item, "zipCode", "ZipCode", "Zip") || undefined, phone: readString(item, "phone", "Phone") || undefined, contact: readString(item, "contact", "Contact") || undefined, contactEmail: readString(item, "contactEmail", "ContactEmail") || undefined, status: readString(item, "status", "Status") || undefined, accountId: readOptionalId(item) || undefined, }; } export function mapLocationToBackend(location: unknown): Record { const item = (typeof location === "object" && location !== null ? location : {}) as Record< string, unknown >; const payload: Record = { name: item.name ?? "", address: item.address ?? "", city: item.city ?? "", state: item.state ?? "", zipCode: item.zipCode ?? "", phone: item.phone ?? "", contact: item.contact ?? "", contactEmail: item.contactEmail ?? "", status: item.status ?? "Active", accountId: item.accountId ?? "", }; const id = item.id ?? item.Id; if (typeof id === "string" || typeof id === "number") { payload.id = id; } return payload; }