import { useFormContext } from 'react-hook-form';

import { GetDrawerConfiguration } from '@material-hu/hooks/useDrawerV2';

import HuFormSwitcher from '@material-hu/components/design-system/Switcher/form';

import { useLokaliseTranslation } from 'src/utils/i18n';
import { UserPermissions } from 'src/utils/permissions';

type KnowledgeLibrariesConfigurationProps = {
  handleSave: () => void;
  onClose: () => void;
  drawerOpen: boolean;
};

const getKnowledgeLibrariesConfiguration: GetDrawerConfiguration<
  KnowledgeLibrariesConfigurationProps
> = ({ drawerOpen, handleSave, onClose }) => {
  const { t } = useLokaliseTranslation('apps');
  const { formState } = useFormContext();

  return {
    children: (
      <HuFormSwitcher
        name={`capabilities.${UserPermissions.VIEW_TABBAR_KNOWLEDGE_LIBRARIES}`}
        switcherProps={{
          title: t('KNOWLEDGE_LIBRARIES_CONFIGURATION_TITLE'),
          description: t('KNOWLEDGE_LIBRARIES_CONFIGURATION_DESCRIPTION'),
        }}
      />
    ),
    open: drawerOpen,
    title: t('TITLE', {
      context: UserPermissions.VIEW_TABBAR_KNOWLEDGE_LIBRARIES,
    }),
    primaryButtonProps: {
      children: t('GENERAL:SAVE'),
      fullWidth: true,
      disabled: !formState.isDirty,
      onClick: () => {
        handleSave();
        onClose();
      },
    },
    secondaryButtonProps: {
      children: t('CANCEL'),
      fullWidth: true,
      onClick: onClose,
    },
    onClose,
  };
};

export default getKnowledgeLibrariesConfiguration;
