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

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

import useAuth from 'src/contexts/JWTContext';
import { Module } from 'src/types/modules';
import { useLokaliseTranslation } from 'src/utils/i18n';
import { hasAnyPermissions, ROUTE_PERMISSIONS } from 'src/utils/permissions';

import useGetSidebarIcon from '../hooks/useGetSidebarIcon';
import { type SidebarItemProps } from '../types';
import { getItemIsActive } from '../utils';

const EmployeeLifecycleItem = ({
  isCollapsed,
  onClick,
  isActive: isActiveProp,
}: SidebarItemProps) => {
  const { t } = useLokaliseTranslation('backoffice_only');
  const getIcon = useGetSidebarIcon();
  const { modules, permissions } = useAuth();
  const isActive = isActiveProp ?? getItemIsActive('employee-lifecycle');

  // The "New" badge only makes sense for communities that still have the legacy
  // Onboarding module enabled and for users who can manage it.
  const shouldShowNewBadge =
    Boolean(modules?.includes(Module.ONBOARDINGS)) &&
    hasAnyPermissions(permissions, ROUTE_PERMISSIONS.ONBOARDING);

  return (
    <HuSidebarNavItem
      key="employee-lifecycle"
      depth={0}
      title={t(`dashboard_sidebar.${Module.EMPLOYEE_LIFECYCLE}`)}
      icon={getIcon({ defaultIcon: IconDoor, key: 'Employee Lifecycle' })}
      path="employee-lifecycle"
      isCollapsed={isCollapsed}
      onClick={onClick}
      active={isActive}
      info={{ isNew: shouldShowNewBadge }}
    />
  );
};

export default EmployeeLifecycleItem;
