export interface ParsedPath {
  locale: string | null
  rest: string
}

export function parseLocalePath(pathname: string, locales: readonly string[]): ParsedPath {
  const parts = pathname.replace(/^\/+/, '').split('/')
  if (parts[0] && locales.includes(parts[0])) {
    return { locale: parts[0], rest: '/' + parts.slice(1).join('/') }
  }
  return { locale: null, rest: pathname }
}

export function buildLocalePath(locale: string, rest: string, defaultLocale: string): string {
  const clean = '/' + rest.replace(/^\/+/, '')
  if (locale === defaultLocale) return clean
  return '/' + locale + (clean === '/' ? '' : clean)
}
