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

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

import useOrgchartTitle from 'src/pages/dashboard/orgChart/hooks/useOrgchartTitle';
import { orgChartRoutes } from 'src/pages/dashboard/orgChart/routes';
import { logOrgChartView } from 'src/utils/orgChart';

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

const { key } = itemsConfig.orgChart;

const PATH = orgChartRoutes.orgChart();

const NavOrgChartItem = (props: SidebarItemProps) => {
  const { isCollapsed, onClick, isActive: isActiveProp } = props;

  const orgchartTitle = useOrgchartTitle();

  const handleOnClick = () => {
    logOrgChartView('sidebar');
    onClick();
  };

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconSitemap,
    key: 'Org Chart',
  });

  const isActive = isActiveProp ?? getItemIsActive(PATH);

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

export default NavOrgChartItem;
