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

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

import AllDrawer from '../components/AllSelectionDrawer';
import {
  SegmentationCondition,
  type SegmentationConditionAll,
  type UseCommonAssignmentSelectionProps,
} from '../types';

const ALL_SELECTION_FORM_ID = 'all-selection-drawer';

const useAllSelection = ({
  onConfirm,
  disabled: modalActionDisabled,
}: UseCommonAssignmentSelectionProps) => {
  const { t } = useLokaliseTranslation('audience');

  const handleAllSelectionDrawerConfirm = () => {
    const requestData: SegmentationConditionAll = {
      type: SegmentationCondition.ALL,
    };
    onConfirm(requestData);
  };

  const {
    drawer: allSelectionDrawer,
    closeDrawer: closeAllSelectionDrawer,
    showDrawer: showAllSelectionDrawer,
  } = useDrawerV2(args => ({
    children: (
      <AllDrawer
        formId={ALL_SELECTION_FORM_ID}
        onConfirm={handleAllSelectionDrawerConfirm}
        disabled={!!modalActionDisabled}
        value={{}}
      />
    ),
    title: t('audience:all_users_title'),
    onClose: () => args.closeDrawer(),
    disableEscapeKeyDown: true,
    primaryButtonProps: {
      variant: 'primary',
      type: 'submit',
      form: ALL_SELECTION_FORM_ID,
      fullWidth: true,
      disabled: !!modalActionDisabled,
      children: t('general:apply'),
    },
    secondaryButtonProps: {
      variant: 'text',
      type: 'reset',
      form: ALL_SELECTION_FORM_ID,
      onClick: () => args.closeDrawer(),
      fullWidth: true,
      children: t('general:cancel'),
    },
  }));

  return {
    allSelectionDrawer,
    closeAllSelectionDrawer,
    showAllSelectionDrawer,
  };
};

export default useAllSelection;
