import { jsx as _jsx } from "react/jsx-runtime";
import { FormProvider, useForm, useWatch, } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useDrawerV2 } from '../../../../../hooks/useDrawerV2';
import CriteriaCollaboratorsReachFooter from './CriteriaCollaboratorsReachFooter';
import CriteriaDrawerContent from './CriteriaDrawerContent';
const useCriteriaDrawer = ({ defaultValues, collaboratorsReach, validate, }) => {
    const { t } = useTranslation('material_hu_only');
    const form = useForm({
        defaultValues,
    });
    const formValues = useWatch({ control: form.control });
    const isInvalid = validate ? !validate(formValues) : false;
    const { handleSubmit, reset } = form;
    const { drawer: criteriaDrawer, showDrawer: showCriteriaDrawer, closeDrawer: closeCriteriaDrawer, } = useDrawerV2(({ onClose, onConfirm, title, description, disabled = false, loading = false, children, ...args }) => {
        const closeDrawer = () => {
            onClose?.();
            closeCriteriaDrawer();
            reset();
        };
        const submit = handleSubmit(values => {
            onConfirm(values);
            closeCriteriaDrawer();
            reset();
        });
        return {
            ...args,
            component: 'form',
            onSubmit: submit,
            onClose: closeDrawer,
            title,
            primaryButtonProps: {
                children: t('audience.confirm'),
                type: 'submit',
                disabled: disabled || isInvalid,
                loading,
            },
            secondaryButtonProps: {
                children: t('audience.cancel'),
                onClick: closeDrawer,
                disabled,
            },
            footer: collaboratorsReach ? (_jsx(CriteriaCollaboratorsReachFooter, { collaboratorsReach: collaboratorsReach, onSelectedCollaboratorsDismiss: closeDrawer })) : undefined,
            children: (_jsx(CriteriaDrawerContent, { description: description, children: children })),
        };
    });
    return {
        criteriaDrawer: _jsx(FormProvider, { ...form, children: criteriaDrawer }),
        showCriteriaDrawer: showCriteriaDrawer,
        closeCriteriaDrawer: closeCriteriaDrawer,
        criteriaForm: form,
    };
};
export default useCriteriaDrawer;
