// Winter-shaped URLs (config_list recordUrl, config_form redirects) mapped // onto the SPA's D-10 routes. The strings come from plugin YAML, so they are // never used verbatim: only the current controller's list, create and record // routes can come out (research Gap 8, T-10-20). // // // -> list // ///create -> create // ///update/:id -> record (:id substituted) // anything else, or another controller -> list import { controllerPath, parseControllerId } from './controllerRoutes' const DIGITS = /^\d+$/ export function mapWinterUrl(url: string | null | undefined, controllerId: string, id?: string | number | null): string { const params = parseControllerId(controllerId) const base = controllerPath(controllerId) if (!params || !base) { return '/' } const raw = (url ?? '').trim().split(/[?#]/, 1)[0] ?? '' const segments = raw.split('/').filter((segment) => segment !== '') const owner = [params.vendor, params.plugin, params.controller] const ownsUrl = owner.every((segment, index) => segments[index]?.toLowerCase() === segment.toLowerCase()) if (!ownsUrl) { return base } const rest = segments.slice(owner.length) if (rest.length === 0) { return base } if (rest.length === 1 && rest[0] === 'create') { return `${base}/create` } if (rest.length === 2 && rest[0] === 'update') { const target = rest[1] === ':id' ? String(id ?? '') : (rest[1] ?? '') return DIGITS.test(target) ? `${base}/${target}` : base } return base }