'use strict';

var jsxRuntime = require('react/jsx-runtime');
var formik = require('formik');
var React = require('react');
var reactRouterDom = require('react-router-dom');
var Button = require('../Button-BniJ4TT_.js');
var Dialog = require('../Dialog-CtI_yWsJ.js');
require('../utils-CTr7wn5d.js');
require('../renderIcon-fLF3odqg.js');
require('../primitives/Button.js');
require('../button--P7-oIJb.js');
require('../index-BZhTddX0.js');
require('../index-DuNgWCXZ.js');
require('../index-D-iDn9RI.js');
require('../primitives/Spinner.js');
require('react-i18next');
require('../loader-circle-Bw7zP2Gn.js');
require('../createLucideIcon-D0tRgV6l.js');
require('../primitives/Tooltip.js');
require('../tooltip-C-568jEL.js');
require('../index-COPkC3I5.js');
require('../index-CGUGhyIp.js');
require('../index-Bvu9MiFi.js');
require('../index-DMbj7vXd.js');
require('react-dom');
require('../index-lWVw05cs.js');
require('../index-BwAq9ba8.js');
require('../index-CCdG4z4E.js');
require('../index-C9ICrOhM.js');
require('../floating-ui.react-dom-B4Aw6O7R.js');
require('../index-Bf0WsHta.js');
require('../index-Df-Ffa3s.js');
require('../index-DcCSSgb7.js');
require('../index-CyrAgb4H.js');
require('../index-CB9xFokC.js');
require('../primitives/Dialog.js');
require('../dialog-CWSpiRjk.js');
require('../x-Brw3FJst.js');
require('../index-BLGrl3PF.js');
require('../index-ChSOMM8b.js');

const useNavPrompt = ({ shouldBlock = true, }) => {
    const [isBlocked, setIsBlocked] = React.useState(false);
    const history = reactRouterDom.useHistory();
    const unblockRef = React.useRef();
    const navRef = React.useRef(null);
    const handleNavigation = React.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 = React.useCallback(() => {
        if (isBlocked && unblockRef.current) {
            unblockRef.current();
            setIsBlocked(false);
            if (navRef.current) {
                const { action, transition } = navRef.current;
                handleNavigation(action, transition);
            }
        }
    }, [isBlocked, handleNavigation]);
    const hidePrompt = React.useCallback(() => setIsBlocked(false), []);
    React.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 = React.useRef(null);
    return (jsxRuntime.jsxs(Dialog.Dialog, { isOpen: isOpen, onClose: onClose ?? (() => { }), closeButton: true, closeOnEsc: true, closeOnOutsideClick: true, initialFocusRef: submitButtonRef, size: "medium", "data-testid": "alert-box", children: [jsxRuntime.jsx(Dialog.Dialog.Header, { children: jsxRuntime.jsx(Dialog.Dialog.Title, { "data-testid": "alert-title", children: title }) }), jsxRuntime.jsx(Dialog.Dialog.Body, { children: jsxRuntime.jsx("p", { "data-testid": "alert-message", className: "text-sm text-muted-foreground", children: message }) }), jsxRuntime.jsxs(Dialog.Dialog.Footer, { className: "flex items-center justify-end gap-2", children: [jsxRuntime.jsx(Button.Button, { "data-testid": "alert-cancel-button", label: cancelButtonLabel, variant: "destructive", onClick: onDiscardChanges }), jsxRuntime.jsx(Button.Button, { "data-testid": "alert-submit-button", label: submitButtonLabel, ref: submitButtonRef, onClick: onSubmit })] })] }));
};
BlockNavigationAlert.displayName = "BlockNavigationAlert";

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

exports.FormikBlockNavigation = FormikBlockNavigation;
//# sourceMappingURL=BlockNavigation.js.map
