import { IconHeartHandshake } from '@material-hu/icons/tabler';
import { List } from '@material-hu/mui';

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

import useCustomServerTranslation from 'src/hooks/useCustomServerTranslation';
import { pxRoutes } from 'src/pages/dashboard/PeopleExperience/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 { key, titleTranslationKey } = itemsConfig.peopleExperience;

const MODULE = 'PEOPLE_EXPERIENCE';

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

  const subItems = [
    {
      key: 'px-available-surveys',
      path: pxRoutes.collaboratorSurveysBase(),
      title: t('available_surveys'),
    },
    {
      key: 'px-team-results',
      path: pxRoutes.managerSurveysBase(),
      title: t('team_results'),
    },
  ];

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconHeartHandshake,
    key: 'People Experience',
  });

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

  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={isActiveProp ?? getSubItemIsActive(item.path)}
            onClick={onClick}
          />
        ))}
      </List>
    );
  };

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

export default NavPeopleExperienceItem;
