import { useTranslation } from 'react-i18next';

import {
  type GetDrawerConfiguration,
  useDrawerV2,
} from '../../../../../hooks/useDrawerV2';
import SelectInstanceContent, {
  type SelectInstanceDrawerProps,
} from '../components/SelectInstanceDrawerContent';

type DrawerProps = SelectInstanceDrawerProps & {
  onClose?: () => void;
};

const getSelectInstanceDrawerContent: GetDrawerConfiguration<DrawerProps> = ({
  closeDrawer,
  onClose,
  ...selectInstanceDrawerProps
}) => {
  const { t } = useTranslation('material_hu_only');

  const handleCloseDrawer = () => {
    closeDrawer();
    onClose?.();
  };

  return {
    children: <SelectInstanceContent {...selectInstanceDrawerProps} />,
    title: t('login.select_a_community'),
    onClose: handleCloseDrawer,
    secondaryButtonProps: {
      fullWidth: true,
      onClick: handleCloseDrawer,
      children: t('login.cancel'),
    },
  };
};

const useSelectIntanceDrawer = (props: DrawerProps) => {
  const {
    drawer: selectInstanceDrawer,
    showDrawer: showSelectInstanceDrawer,
    closeDrawer: closeSelectInstanceDrawer,
  } = useDrawerV2(args =>
    getSelectInstanceDrawerContent({
      ...args,
      ...props,
    }),
  );

  return {
    drawer: selectInstanceDrawer,
    showDrawer: showSelectInstanceDrawer,
    closeDrawer: closeSelectInstanceDrawer,
  };
};

export default useSelectIntanceDrawer;
