import type { AdminGeoLocationNode } from "@/app/lib/types"; // Geo-location cascade builder for the member edit form (ported from // solvway-customer-portal's app/lib/geoPrefill.ts). The edit-initialize payload // mirrors the saved address traceKey (e.g. root/Malaysia/Kedah/Pendang): // `geoLocationLevel{i}` = selected id at depth i, `geoLocationLevel{i}List` = that // level's sibling options. Depth 0 is the root container (no list) and is never // shown — the first select starts at depth 1, whose options equal findFirstLevel. export interface GeoLevelData { options: AdminGeoLocationNode[]; selected: string; } const MAX_DEPTH = 10; export function buildGeoLevels( init: Record, firstLevel: AdminGeoLocationNode[], ): GeoLevelData[] { const levels: GeoLevelData[] = []; for (let i = 1; i <= MAX_DEPTH; i++) { const sel = init[`geoLocationLevel${i}`]; const list = init[`geoLocationLevel${i}List`]; if (sel === undefined && !Array.isArray(list)) break; const options = Array.isArray(list) && list.length > 0 ? (list as AdminGeoLocationNode[]) : i === 1 ? firstLevel : []; levels.push({ options, selected: sel !== undefined ? String(sel) : "" }); } if (levels.length === 0) { levels.push({ options: firstLevel, selected: "" }); } return levels; }