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

export function mapPathsToBreadcrumbs(
  paths: string[],
  startBreadcrumb?: NavigationBreadcrumbsItem
): NavigationBreadcrumbsItem[] {
  let breadcrumbs = [
    ...(startBreadcrumb ? [startBreadcrumb] : []),
    ...paths.map((path, index) => ({
      text: mapPathToPageName(path).replace(/-/g, ' '),
      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 (isDeltagareCardRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 1].text = 'Deltagarinformation';
  } else if (isDeltagareReportingFormRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 2].text = 'Deltagarinformation';
    breadcrumbs[breadcrumbs.length - 1].text = `Skapa ${DELTAGARE_REPORTING_ROUTES[paths[paths.length - 1]] as string}`;
  } else if (isDeltagareReportingRoute(paths)) {
    breadcrumbs = breadcrumbs.slice(0, -1);
    breadcrumbs[breadcrumbs.length - 2].text = 'Deltagarinformation';
  } else if (isExportRoute(paths)) {
    breadcrumbs[breadcrumbs.length - 1].text = `Exportera ${paths[paths.length - 1]}`;
  }

  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 isDeltagareCardRoute(paths: string[]): boolean {
  return paths.length === 2 && paths[0] === 'deltagare';
}

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

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

function isExportRoute(paths: string[]): boolean {
  return paths[0] === 'exporter';
}
