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

import DOMPurify from 'dompurify';
import { IconChevronRight } from '@material-hu/icons/tabler';
import Stack from '@material-hu/mui/Stack';
import { useTheme } from '@material-hu/mui/styles';
import Typography from '@material-hu/mui/Typography';

import HuCardContainer from '@material-hu/components/design-system/CardContainer';
import HuListItem from '@material-hu/components/design-system/List/components/ListItem';
import HuSkeleton from '@material-hu/components/design-system/Skeleton';

import { librariesRoutes } from 'src/pages/dashboard/libraries/routes';
import { IconTypes } from 'src/types/icons';
import { LibrarySearchListItem } from 'src/types/libraries';
import { useLokaliseTranslation } from 'src/utils/i18n';

import Breadcrumbs from './Breadcrumbs';

export type LibrarySearchItemProps = {
  library?: LibrarySearchListItem;
  loading?: boolean;
};

export const LibrarySearchItem = ({
  library,
  loading = false,
}: LibrarySearchItemProps) => {
  const { t } = useLokaliseTranslation('general');
  const navigate = useNavigate();
  const theme = useTheme();

  const handleSelect = () => {
    if (!library) return;
    navigate(librariesRoutes.library(library.id));
  };

  const breadcrumbItems = library
    ? [
        ...library.parentTree,
        {
          id: library.id,
          title: library.title,
        },
      ]
    : [];

  return (
    <HuCardContainer
      sx={{
        width: '100%',
        p: 3,
        '& > .MuiCardContent-root': {
          display: 'flex',
          flexDirection: 'column',
          gap: 3,
          p: 0,
        },
        '& .back-highlights': {
          backgroundColor: theme.palette.new.border.neutral.brand,
        },
      }}
    >
      {!loading && library && (
        <>
          <Stack>
            <HuListItem
              avatar={{
                src:
                  library.icon.type === IconTypes.IMAGE
                    ? library.icon.value
                    : undefined,
                text:
                  library.icon.type === IconTypes.EMOJI
                    ? library.icon.value
                    : undefined,
                alt: '',
              }}
              text={{
                title: (
                  <div
                    dangerouslySetInnerHTML={{
                      __html: DOMPurify.sanitize(
                        library.highlights.title[0] || library.title,
                        {
                          USE_PROFILES: { html: true },
                        },
                      ),
                    }}
                  />
                ),
              }}
              action={{
                onClick: handleSelect,
                'aria-label': `${t('select')} ${library.title}`,
                Icon: IconChevronRight,
              }}
              sx={{
                '& .MuiListItem-root': {
                  p: 0,
                },
                '& .MuiAvatar-root': {
                  '& .MuiAvatar-img': {
                    p: 1,
                  },
                  '& .MuiTypography-root': {
                    fontSize: '20px',
                  },
                },
              }}
            />
            <Breadcrumbs
              items={breadcrumbItems}
              sx={{
                ml: '48px',
              }}
            />
          </Stack>
          {library.textContent && (
            <Typography
              variant="globalS"
              sx={{
                display: '-webkit-box',
                WebkitLineClamp: 2,
                WebkitBoxOrient: 'vertical',
                maxHeight: '44px',
                overflow: 'hidden',
                whiteSpace: 'normal',
                overflowWrap: 'break-word',
              }}
              dangerouslySetInnerHTML={{
                __html: DOMPurify.sanitize(
                  library.highlights.textContent[0] || library.textContent,
                  {
                    USE_PROFILES: { html: true },
                  },
                ),
              }}
            />
          )}
        </>
      )}
      {loading && (
        <>
          <HuListItem loading />
          <HuSkeleton
            width="100%"
            height={44}
          />
        </>
      )}
    </HuCardContainer>
  );
};

export default LibrarySearchItem;
