// This should not be imported – Remove after migration to Lokalise
import { IconMessage } from '@material-hu/icons/tabler';
import List from '@material-hu/mui/List';

import HuSidebarNavItem from '@material-hu/components/design-system/Sidebar/components/NavItem';

import useCustomServerTranslation from 'src/hooks/useCustomServerTranslation';
import { draftsForReviewRoutes } from 'src/pages/dashboard/communication/DraftsForReview/routes';
import { useLokaliseTranslation } from 'src/utils/i18n';

import itemsConfig from '../constants';
import useGetSidebarIcon from '../hooks/useSidebarIcon';
import { SidebarItemProps } from '../types';
import { getItemIsActive, getSubItemIsActive } from '../utils';

const PATH = draftsForReviewRoutes.drafts();
export const NAMESPACE = 'COMMUNICATION';

const { key, titleTranslationKey } = itemsConfig.communications;

const NavCommunicationItem = (props: SidebarItemProps) => {
  const { isCollapsed, onClick, isActive: isActiveProp } = props;
  const { t } = useLokaliseTranslation('communication');

  const title = useCustomServerTranslation({
    module: NAMESPACE,
    defaultTranslationKey: titleTranslationKey,
    namespace: 'dashboard_sidebar_app',
  });

  const subItems = [
    {
      key: 'drafts',
      path: draftsForReviewRoutes.drafts(),
      title: t('draft_for_review'),
    },
  ];

  const isActive =
    isActiveProp ?? subItems.some(item => getItemIsActive(item.path));

  const renderSubItems = () => {
    if (isCollapsed) return null;

    return (
      <List
        disablePadding
        sx={{
          '& > *:not(:last-child)': {
            mb: 0,
          },
        }}
      >
        {subItems.map((item, index) => (
          <HuSidebarNavItem
            key={item.key}
            title={item.title}
            path={item.path}
            depth={1}
            isLastChild={index === subItems.length - 1}
            isCollapsed={isCollapsed}
            active={getSubItemIsActive(item.path)}
            onClick={onClick}
          />
        ))}
      </List>
    );
  };

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconMessage,
    key: 'Communication',
  });

  return (
    <HuSidebarNavItem
      key={key}
      depth={0}
      title={title}
      icon={<Icon />}
      path={PATH}
      isCollapsed={isCollapsed}
      active={isActive}
    >
      {renderSubItems()}
    </HuSidebarNavItem>
  );
};

export default NavCommunicationItem;
