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 { D as Dialog } from '../Dialog-BOcBeB8v.js';
import { B as Button } from '../Button-BCWJgHDy.js';
import '../utils-BJnb9o5c.js';
import '../primitives/Dialog.js';
import '../button-CrEMzWKw.js';
import '../index-Bo3AAQJp.js';
import '../index-CfriMyrd.js';
import '../index-BtkPdosV.js';
import '../dialog--Qm4MZDo.js';
import 'react-i18next';
import '../x-_o2T3n6D.js';
import '../createLucideIcon-C8ycilSN.js';
import '../index-B7SdntLB.js';
import '../index-DNzunGHb.js';
import '../index-CiyxEyB0.js';
import '../index-zW4GjM5L.js';
import '../index-DOzu5J1s.js';
import '../index-Cor698lu.js';
import '../index-BNPynZWM.js';
import '../index-DhnfW8wQ.js';
import 'react-dom';
import '../index-CSUSJzOJ.js';
import '../index-Jr4soqx3.js';
import '../index-fV_U4ZJM.js';
import '../index-yFgkK_AM.js';
import '../renderIcon-C6twJSqH.js';
import '../primitives/Button.js';
import '../primitives/Spinner.js';
import '../loader-circle-DycHUAWN.js';
import '../primitives/Tooltip.js';
import '../tooltip-ChQ9xD1j.js';
import '../index-C3hByjk3.js';
import '../floating-ui.react-dom-D8_f_WWh.js';
import '../index-BYZaLNq1.js';
import '../index-CSggBaQF.js';

const useNavPrompt = ({
  shouldBlock = true
}) => {
  const [isBlocked, setIsBlocked] = useState(false);
  const history = useHistory();
  const unblockRef = useRef(void 0);
  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 void 0;
  }, [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 /* @__PURE__ */ jsxs(
    Dialog,
    {
      isOpen,
      onClose: onClose ?? (() => {
      }),
      closeButton: true,
      closeOnEsc: true,
      closeOnOutsideClick: true,
      initialFocusRef: submitButtonRef,
      size: "medium",
      "data-testid": "alert-box",
      children: [
        /* @__PURE__ */ jsx(Dialog.Header, { children: /* @__PURE__ */ jsx(Dialog.Title, { "data-testid": "alert-title", children: title }) }),
        /* @__PURE__ */ jsx(Dialog.Body, { children: /* @__PURE__ */ jsx(
          "p",
          {
            "data-testid": "alert-message",
            className: "text-sm text-muted-foreground",
            children: message
          }
        ) }),
        /* @__PURE__ */ jsxs(Dialog.Footer, { className: "flex items-center justify-end gap-2", children: [
          /* @__PURE__ */ jsx(
            Button,
            {
              "data-testid": "alert-cancel-button",
              label: cancelButtonLabel,
              variant: "destructive",
              onClick: onDiscardChanges
            }
          ),
          /* @__PURE__ */ 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 /* @__PURE__ */ jsx(
    BlockNavigationAlert,
    {
      isOpen: isBlocked,
      onClose: hidePrompt,
      onDiscardChanges: handleDiscardChanges,
      onSubmit: hidePrompt,
      ...otherProps
    }
  );
};
FormikBlockNavigation.displayName = "FormikBlockNavigation";

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