import { jsxs, jsx } from 'react/jsx-runtime';
import { useFormikContext } from 'formik';
import { useState, useRef, useCallback, useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { B as Button } from '../Button-DSvnInaL.js';
import { D as Dialog } from '../Dialog-BURSzxaP.js';
import '../utils-DdHUxIdC.js';
import '../renderIcon-tlvMyboj.js';
import '../primitives/Button.js';
import '../button-D6uWDFCs.js';
import '../index-D7Zy7P05.js';
import '../index-CfriMyrd.js';
import '../index-BtkPdosV.js';
import '../primitives/Spinner.js';
import 'react-i18next';
import '../loader-circle-DycHUAWN.js';
import '../createLucideIcon-C8ycilSN.js';
import '../primitives/Tooltip.js';
import '../tooltip-XkHLgxlU.js';
import '../index-DNzunGHb.js';
import '../index-CiyxEyB0.js';
import '../index-BNPynZWM.js';
import '../index-DhnfW8wQ.js';
import 'react-dom';
import '../index-CSUSJzOJ.js';
import '../index-zW4GjM5L.js';
import '../index-DOzu5J1s.js';
import '../index-C3hByjk3.js';
import '../floating-ui.react-dom-D8_f_WWh.js';
import '../index-BYZaLNq1.js';
import '../index-fV_U4ZJM.js';
import '../index-yFgkK_AM.js';
import '../index-Cor698lu.js';
import '../index-CSggBaQF.js';
import '../primitives/Dialog.js';
import '../dialog-DK71SlsS.js';
import '../x-_o2T3n6D.js';
import '../index-9HvIbmnI.js';
import '../index-B4_fVWDx.js';

const useNavPrompt = ({ shouldBlock = true, }) => {
    const [isBlocked, setIsBlocked] = useState(false);
    const history = useHistory();
    const unblockRef = useRef();
    const navRef = useRef(null);
    const handleNavigation = useCallback((action, transition) => {
        switch (action) {
            case "PUSH":
                history.push(transition);
                break;
            case "REPLACE":
                history.replace(transition);
                break;
            case "POP":
                history.goBack();
                break;
        }
    }, [history]);
    const continueNavigation = useCallback(() => {
        if (isBlocked && unblockRef.current) {
            unblockRef.current();
            setIsBlocked(false);
            if (navRef.current) {
                const { action, transition } = navRef.current;
                handleNavigation(action, transition);
            }
        }
    }, [isBlocked, handleNavigation]);
    const hidePrompt = useCallback(() => setIsBlocked(false), []);
    useEffect(() => {
        if (shouldBlock) {
            unblockRef.current = history.block((transition, action) => {
                setIsBlocked(true);
                navRef.current = { transition: transition.pathname, action };
                return false;
            });
            return () => {
                unblockRef.current?.();
            };
        }
        return undefined;
    }, [shouldBlock, history]);
    return { isBlocked, continueNavigation, hidePrompt };
};

const BlockNavigationAlert = ({ isOpen = false, onClose, onSubmit, onDiscardChanges, title = "You have unsaved changes", message = "Are you sure you want to leave? All unsaved changes will be lost.", cancelButtonLabel = "Discard and leave", submitButtonLabel = "Stay on this page", }) => {
    const submitButtonRef = useRef(null);
    return (jsxs(Dialog, { isOpen: isOpen, onClose: onClose ?? (() => { }), closeButton: true, closeOnEsc: true, closeOnOutsideClick: true, initialFocusRef: submitButtonRef, size: "medium", "data-testid": "alert-box", children: [jsx(Dialog.Header, { children: jsx(Dialog.Title, { "data-testid": "alert-title", children: title }) }), jsx(Dialog.Body, { children: jsx("p", { "data-testid": "alert-message", className: "text-sm text-muted-foreground", children: message }) }), jsxs(Dialog.Footer, { className: "flex items-center justify-end gap-2", children: [jsx(Button, { "data-testid": "alert-cancel-button", label: cancelButtonLabel, variant: "destructive", onClick: onDiscardChanges }), jsx(Button, { "data-testid": "alert-submit-button", label: submitButtonLabel, ref: submitButtonRef, onClick: onSubmit })] })] }));
};
BlockNavigationAlert.displayName = "BlockNavigationAlert";

const FormikBlockNavigation = ({ isDirty = false, ...otherProps }) => {
    const formikContext = useFormikContext();
    const shouldBlock = isDirty || (Boolean(formikContext) && Boolean(formikContext.dirty));
    const { isBlocked, continueNavigation, hidePrompt } = useNavPrompt({
        shouldBlock,
    });
    const handleDiscardChanges = () => {
        if (formikContext)
            formikContext.resetForm();
        hidePrompt();
        continueNavigation();
    };
    return (jsx(BlockNavigationAlert, { isOpen: isBlocked, onClose: hidePrompt, onDiscardChanges: handleDiscardChanges, onSubmit: hidePrompt, ...otherProps }));
};
FormikBlockNavigation.displayName = "FormikBlockNavigation";

export { FormikBlockNavigation };
//# sourceMappingURL=BlockNavigation.js.map
