import { FC } from 'react';

import { IconZoomExclamation } from '@material-hu/icons/tabler';

import StateCard from '@material-hu/components/composed-components/StateCard';

import { ORGANIZER_LIMIT } from 'src/constants/events';
import { Event } from 'src/types/events';
import { useLokaliseTranslation } from 'src/utils/i18n';

import InfiniteList from 'src/components/list/InfiniteList';

import { useEventOrganizers } from '../hooks/useEventOrganizers';

import { OrganizerListItem } from './OrganizerListItem';

type OrganizersListDrawerProps = {
  event: Event;
};

const OrganizersListDrawer: FC<OrganizersListDrawerProps> = props => {
  const { event } = props;
  const { t } = useLokaliseTranslation('events');

  const { creator, displayOrganizers, restOrganizerDataProps } =
    useEventOrganizers(event, ORGANIZER_LIMIT.DRAWER_DISPLAY);

  return (
    <>
      <InfiniteList
        isSuccess={restOrganizerDataProps?.isSuccess}
        hasNextPage={restOrganizerDataProps?.hasNextPage}
        fetchNextPage={restOrganizerDataProps?.fetchNextPage}
        isFetchingNextPage={restOrganizerDataProps?.isFetchingNextPage}
        isLoading={restOrganizerDataProps?.isLoading}
        isEmpty={
          restOrganizerDataProps?.isSuccess && !displayOrganizers?.length
        }
        showNoResultsMessage={false}
      >
        {displayOrganizers?.map(organizer => (
          <OrganizerListItem
            key={organizer.id}
            organizer={organizer}
            event={event}
            creatorId={creator?.id}
            slotProps={{
              container: {
                sx: {
                  p: 1.5,
                },
              },
            }}
          />
        ))}
      </InfiniteList>
      {displayOrganizers?.length === 0 &&
        !restOrganizerDataProps?.isLoading && (
          <StateCard
            slotProps={{
              avatar: { Icon: IconZoomExclamation },
              title: {
                title: t('no_users_found_title'),
                description: t('no_users_found_description'),
              },
            }}
            cardContained={false}
          />
        )}
    </>
  );
};

export default OrganizersListDrawer;
