import { useQuery } from 'react-query';

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

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

import useCustomServerTranslation from 'src/hooks/useCustomServerTranslation';
import { groupsKeys } from 'src/pages/dashboard/groups/queries';
import { groupsRoutes } from 'src/pages/dashboard/groups/routes';
import { getGroupsNotifications } from 'src/services/groups';

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

const { key, titleTranslationKey } = itemsConfig.groups;

const MODULE = 'GROUPS';
const PATH = groupsRoutes.groups();

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

  const { data } = useQuery(
    groupsKeys.notifications.notificationRedBubblesCount(),
    () => getGroupsNotifications(),
  );

  const { groupNotifications = 0 } = data?.data || {};

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconUsersGroup,
    key: 'Groups',
  });

  const isActive = isActiveProp || getItemIsActive(PATH);

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

  return (
    <HuSidebarNavItem
      key={key}
      depth={0}
      title={title}
      icon={<Icon />}
      info={{
        notificationCount: groupNotifications,
      }}
      path={PATH}
      isCollapsed={isCollapsed}
      onClick={onClick}
      active={isActive}
      skipLinkBehavior
    />
  );
};

export default NavGroups;
