import { BreadcrumbItem } from './breadcrumb.component';

/** Route → breadcrumb label + parent. The app has a small fixed route set
    (see app.routes.ts), so a static map is enough — no per-page wiring.
    ponytail: static map, not a breadcrumb service; revisit if routes go dynamic. */
interface Crumb { label: string; parent?: string }

const ROUTES: Record<string, Crumb> = {
  '/dashboard': { label: $localize`:@@crumb.dashboard:Mijn overzicht` },
  '/registratie': { label: $localize`:@@crumb.registratie:Mijn gegevens`, parent: '/dashboard' },
  '/registreren': { label: $localize`:@@crumb.registreren:Inschrijven`, parent: '/dashboard' },
  '/herregistratie': { label: $localize`:@@crumb.herregistratie:Herregistratie`, parent: '/dashboard' },
  '/intake': { label: $localize`:@@crumb.intake:Herregistratie-intake`, parent: '/dashboard' },
  '/concepts': { label: $localize`:@@crumb.concepts:Functionele patronen`, parent: '/dashboard' },
};

/** Build the breadcrumb trail for a router url (query/fragment stripped).
    Returns [] for unknown routes (e.g. /login) so the bar can hide itself. */
export function trailFor(url: string): BreadcrumbItem[] {
  const path = url.split(/[?#]/)[0];
  const trail: BreadcrumbItem[] = [];
  let cursor: string | undefined = path;
  while (cursor) {
    const node: Crumb | undefined = ROUTES[cursor];
    if (!node) break;
    trail.unshift({ label: node.label, link: cursor });
    cursor = node.parent;
  }
  // The current (last) page is not a link.
  if (trail.length) delete trail[trail.length - 1].link;
  return trail;
}
