import { _ as _asyncToGenerator, r as regenerator } from '../index-qSfOyGhk.js';
import { useEffect } from 'react';
import confirm from '../modal/confirm.js';
import '../typeof-CgC7Bq6K.js';
import 'react-dom';
import '../modal/confirm-modal.js';
import '../extends-C4cduGrv.js';
import '../objectWithoutProperties-TXwlfNxG.js';
import 'prop-types';
import 'classnames';
import '../button/index.js';
import '../defineProperty-CEd-kZQW.js';
import '@reach/visually-hidden';
import '../ck-icon/index.js';
import '../ck-icon/assets/icon-map/index.js';
import '../icon/index.js';
import '@fortawesome/react-fontawesome';
import '../icon/icons.js';
import '../icon/library.js';
import '@fortawesome/fontawesome-svg-core';
import '@fortawesome/pro-solid-svg-icons';
import '@fortawesome/pro-light-svg-icons';
import '@fortawesome/free-brands-svg-icons';
import '../slicedToArray-BB-AwwkK.js';
import '../floating-ui.react-cWPyyUrN.js';
import '../toConsumableArray-dKQYsfpV.js';
import '../button/button-group.js';
import '../portal/index.js';
import '../use-rerender-once/index.js';
import '../use-force-update/index.js';
import '../dialog.esm-C1hoFD2p.js';
import '../portal.esm-CuKGPwhL.js';
import '../utils.esm-CKyxlHOy.js';
import '../warning-s9zGJbnw.js';
import '../setPrototypeOf-Bchah3-D.js';
import '../auto-id.esm-Cly76abB.js';
import 'framer-motion';
import '../modal/action.js';

var hrefBlank = function hrefBlank(element) {
  var href = element.getAttribute("href");
  return !href || href === "#";
};

function useBeforeLeave() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$message = _ref.message,
      message = _ref$message === void 0 ? "" : _ref$message,
      _ref$title = _ref.title,
      title = _ref$title === void 0 ? "Unsaved Changes" : _ref$title,
      _ref$enabled = _ref.enabled,
      enabled = _ref$enabled === void 0 ? true : _ref$enabled,
      _ref$confirm = _ref.confirm,
      confirmText = _ref$confirm === void 0 ? "Leave without saving" : _ref$confirm,
      _ref$cancel = _ref.cancel,
      cancel = _ref$cancel === void 0 ? "Cancel" : _ref$cancel;

  useEffect(function () {
    var handleClick = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee(event) {
        var element, confirmation;
        return regenerator.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (enabled) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt("return");

              case 2:
                element = event.target;

                if (!(element.matches("a") && !element.dataset.confirmed)) {
                  _context.next = 15;
                  break;
                }

                if (!hrefBlank(element)) {
                  _context.next = 6;
                  break;
                }

                return _context.abrupt("return");

              case 6:
                if (!(element.target === "_blank")) {
                  _context.next = 8;
                  break;
                }

                return _context.abrupt("return");

              case 8:
                if (!element.isContentEditable) {
                  _context.next = 10;
                  break;
                }

                return _context.abrupt("return");

              case 10:
                // We always prevent the default action because the event listener is
                // synchronous, and determining if the user wants to navigate away is
                // asynchronous.
                event.preventDefault();
                _context.next = 13;
                return confirm(title, {
                  message: message,
                  confirm: confirmText,
                  cancel: cancel
                });

              case 13:
                confirmation = _context.sent;

                if (confirmation) {
                  // Once confirmed, set data-confirm so we can retrigger the click,
                  // but not prompt for confirmation next time.
                  element.dataset.confirmed = true;
                  element.click();
                }

              case 15:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function handleClick(_x) {
        return _ref2.apply(this, arguments);
      };
    }();

    document.addEventListener("click", handleClick);
    return function () {
      return document.removeEventListener("click", handleClick);
    };
  }, [message, title, enabled, confirmText, cancel]);
}

export { useBeforeLeave as default };
