import { FC } from 'react';

import { ICONS } from '@material-hu/components/design-system/Sidebar/constants';
import { IconsModules } from '@material-hu/components/design-system/Sidebar/constants';

import useAuth from 'src/contexts/JWTContext';
import { IconsStyle } from 'src/types/instance';

type GetIconProps = {
  defaultIcon: FC;
  key: IconsModules;
};
const useGetSidebarIcon = () => {
  const { instance } = useAuth();

  const getIcon = (props: GetIconProps) => {
    const { defaultIcon: DefaultIcon, key } = props;

    // Normalize underscores from iconsStyle
    const iconKey = instance?.iconsStyle?.replace('_', '').toLowerCase();

    let Icon = iconKey
      ? ICONS[iconKey as keyof typeof ICONS]?.[key]
      : undefined;

    if (instance?.iconsStyle === IconsStyle.ICONS || !Icon)
      return <DefaultIcon />;

    return <Icon />;
  };

  return getIcon;
};

export default useGetSidebarIcon;
