/** First word of the name, uppercased and stripped to alphanumerics — falls back to "CU" * so an empty/punctuation-only name still yields a usable base. Mirrors the vendor code * generator (lib/vendor-code.ts). */ function customerCodeBase(name: string): string { const firstWord = name.trim().split(/\s+/)[0] ?? "" const cleaned = firstWord.toUpperCase().replace(/[^A-Z0-9]/g, "") return cleaned.slice(0, 10) || "CU" } /** Appends a numeric suffix until the code doesn't collide with an existing one — the * backend enforces global uniqueness but has no generation of its own. */ export function generateCustomerCode(name: string, existingCodes: string[]): string { const base = `CU-${customerCodeBase(name)}` if (!existingCodes.includes(base)) return base let suffix = 2 while (existingCodes.includes(`${base}${suffix}`)) suffix += 1 return `${base}${suffix}` }