import { useNavigate } from 'react-router-dom';

import Stack, { StackProps } from '@material-hu/mui/Stack';

import HuBreadcrumbs from '@material-hu/components/design-system/Breadcrumbs';

import { BreadcrumbItem } from 'src/types/libraries';

import useGetBreadcrumbPath from '../../hooks/useGetBreadcrumbPath';
import { librariesRoutes } from '../../routes';

import { BreadcrumbsSkeleton } from './BreadcrumbsSkeleton';

export type BreadcrumbsProps = StackProps & {
  items: BreadcrumbItem[];
  loading?: boolean;
};

export const Breadcrumbs = ({
  items,
  loading = false,
  ...stackProps
}: BreadcrumbsProps) => {
  const navigate = useNavigate();
  const getBreadcrumbPath = useGetBreadcrumbPath();

  const handleClick = (to?: number) => () =>
    to !== undefined
      ? navigate(librariesRoutes.library(to))
      : navigate(librariesRoutes.base());

  const links = getBreadcrumbPath(items, handleClick);

  return (
    <Stack {...stackProps}>
      {loading && <BreadcrumbsSkeleton />}
      {!loading && <HuBreadcrumbs links={links} />}
    </Stack>
  );
};

export default Breadcrumbs;
