const KNOWN_PATHS = new Set([
    '/',
    '/contatti',
    '/contacts',
    '/403',
    '/404',
    '/500',
    '/errore'
])

const PATH_ALIASES = {
    '/assistenza': '/contatti',
    '/supporto': '/contatti',
    '/error': '/errore'
}

export const normalizePathname = (path) => {
    if (!path) return '/'
    const trimmed = String(path).trim()
    if (!trimmed) return '/'
    return trimmed.endsWith('/') && trimmed.length > 1 ? trimmed.slice(0, -1) : trimmed
}

export const resolvePathname = (path) => {
    const normalized = normalizePathname(path)
    const alias = PATH_ALIASES[normalized]
    if (alias) return alias
    // Non forziamo un "/404" per preservare l'URL inserito dall'utente.
    // Sarà getRoute() in App.svelte a mappare le route non conosciute sul componente 404
    return normalized
}

export const getErrorPath = (statusOrCode) => {
    const code = String(statusOrCode ?? '').trim()
    if (code === '403') return '/403'
    if (code === '404') return '/404'
    if (code === '500') return '/500'
    return '/errore'
}
