import { useTranslation } from 'react-i18next';

import { useDrawerV2 } from '@hooks/useDrawerV2';
import { type DrawerCloseReason } from '@src/components/layers/Drawers/types';

import SelectedCollaboratorsDrawerContent from './content';
import { type SelectedCollaboratorsDrawerProps } from './types';

const useSelectedCollaboratorsDrawer = () => {
  const { t } = useTranslation('material_hu_only');

  const {
    drawer: selectedCollaboratorsDrawer,
    showDrawer: showSelectedCollaboratorsDrawer,
    closeDrawer: closeSelectedCollaboratorsDrawer,
  } = useDrawerV2<SelectedCollaboratorsDrawerProps>(
    ({ onClose, onDismiss, service, queryKey, totalCount, ...args }) => {
      const closeDrawer = (reason?: DrawerCloseReason) => {
        onClose?.(reason);
        closeSelectedCollaboratorsDrawer();
      };

      const handleClose = (reason?: DrawerCloseReason) => {
        closeDrawer(reason);

        if (reason === 'dismissButton') {
          onDismiss?.();
        }
      };

      return {
        ...args,
        onClose: handleClose,
        hasBackButton: true,
        title: t('audience.selected_collaborators_drawer_title'),
        children: (
          <SelectedCollaboratorsDrawerContent
            totalCount={totalCount}
            service={service}
            queryKey={queryKey}
          />
        ),
      };
    },
  );

  return {
    selectedCollaboratorsDrawer,
    showSelectedCollaboratorsDrawer,
    closeSelectedCollaboratorsDrawer,
  };
};

export default useSelectedCollaboratorsDrawer;
