import { NavigationBreadcrumbsItem } from '@af/digi-ng/_navigation/navigation-breadcrumbs';
import { mapPathToPageName } from './map-path-to-page-name.util';

export function mapPathsToBreadcrumbs(
  paths: string[],
  startBreadcrumb?: NavigationBreadcrumbsItem
): NavigationBreadcrumbsItem[] {
  const breadcrumbs = [
    ...(startBreadcrumb ? [startBreadcrumb] : []),
    ...paths.map((path, index) => ({
      text: mapPathToPageName(path),
      routerLink: `/${paths.slice(0, index + 1).join('/')}`,
    })),
  ];

  if (isEmployeeCardRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 1].text = 'Personalkonto';
  } else if (isEmployeeEditRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 2] = {
      text: 'Hantera personalkonton',
      routerLink: '/administration/personal',
    };
    breadcrumbs[breadcrumbs.length - 1].text = 'Redigera personalkonto';
  } else if (isParticipantCardRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 1].text = 'Deltagarinformation';
  }
  return breadcrumbs;
}

function isEmployeeCardRoute(paths: string[]): boolean {
  return paths.length === 3 && paths[1] === 'personal';
}

function isEmployeeEditRoute(paths: string[]): boolean {
  return paths.length === 3 && paths[1] === 'redigera-personalkonto';
}

function isParticipantCardRoute(paths: string[]): boolean {
  return paths.length === 2 && paths[0] === 'deltagare';
}
