"use strict";
const _extends = require("../../../../../@babel_runtime@7.19.0/node_modules/@babel/runtime/helpers/esm/extends.js");
const React = require("react");
const PropTypes = require("prop-types");
const constants = require("../../../../../focus-lock@0.11.3/node_modules/focus-lock/dist/es2015/constants.js");
const FocusGuard = require("./FocusGuard.js");
const medium = require("./medium.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const useMergeRef = require("../../../../../use-callback-ref@1.3.0_react@18.2.0/node_modules/use-callback-ref/dist/es2015/useMergeRef.js");
function _interopNamespace(e) {
  if (e && e.__esModule)
    return e;
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespace(React);
var emptyArray = [];
var FocusLock = /* @__PURE__ */ React__namespace.forwardRef(function FocusLockUI(props, parentRef) {
  var _extends2;
  var _React$useState = React__namespace.useState(), realObserved = _React$useState[0], setObserved = _React$useState[1];
  var observed = React__namespace.useRef();
  var isActive = React__namespace.useRef(false);
  var originalFocusedElement = React__namespace.useRef(null);
  var children = props.children, disabled = props.disabled, noFocusGuards = props.noFocusGuards, persistentFocus = props.persistentFocus, crossFrame = props.crossFrame, autoFocus = props.autoFocus, allowTextSelection = props.allowTextSelection, group = props.group, className = props.className, whiteList = props.whiteList, hasPositiveIndices = props.hasPositiveIndices, _props$shards = props.shards, shards = _props$shards === void 0 ? emptyArray : _props$shards, _props$as = props.as, Container = _props$as === void 0 ? "div" : _props$as, _props$lockProps = props.lockProps, containerProps = _props$lockProps === void 0 ? {} : _props$lockProps, SideCar = props.sideCar, shouldReturnFocus = props.returnFocus, focusOptions = props.focusOptions, onActivationCallback = props.onActivation, onDeactivationCallback = props.onDeactivation;
  var _React$useState2 = React__namespace.useState({}), id = _React$useState2[0];
  var onActivation = React__namespace.useCallback(function() {
    originalFocusedElement.current = originalFocusedElement.current || document && document.activeElement;
    if (observed.current && onActivationCallback) {
      onActivationCallback(observed.current);
    }
    isActive.current = true;
  }, [onActivationCallback]);
  var onDeactivation = React__namespace.useCallback(function() {
    isActive.current = false;
    if (onDeactivationCallback) {
      onDeactivationCallback(observed.current);
    }
  }, [onDeactivationCallback]);
  React.useEffect(function() {
    if (!disabled) {
      originalFocusedElement.current = null;
    }
  }, []);
  var returnFocus = React__namespace.useCallback(function(allowDefer) {
    var returnFocusTo = originalFocusedElement.current;
    if (returnFocusTo && returnFocusTo.focus) {
      var howToReturnFocus = typeof shouldReturnFocus === "function" ? shouldReturnFocus(returnFocusTo) : shouldReturnFocus;
      if (howToReturnFocus) {
        var returnFocusOptions = typeof howToReturnFocus === "object" ? howToReturnFocus : void 0;
        originalFocusedElement.current = null;
        if (allowDefer) {
          Promise.resolve().then(function() {
            return returnFocusTo.focus(returnFocusOptions);
          });
        } else {
          returnFocusTo.focus(returnFocusOptions);
        }
      }
    }
  }, [shouldReturnFocus]);
  var onFocus = React__namespace.useCallback(function(event) {
    if (isActive.current) {
      medium.mediumFocus.useMedium(event);
    }
  }, []);
  var onBlur = medium.mediumBlur.useMedium;
  var setObserveNode = React__namespace.useCallback(function(newObserved) {
    if (observed.current !== newObserved) {
      observed.current = newObserved;
      setObserved(newObserved);
    }
  }, []);
  if (process.env.NODE_ENV !== "production") {
    if (typeof allowTextSelection !== "undefined") {
      console.warn("React-Focus-Lock: allowTextSelection is deprecated and enabled by default");
    }
    React__namespace.useEffect(function() {
      if (!observed.current) {
        console.error("FocusLock: could not obtain ref to internal node");
      }
    }, []);
  }
  var lockProps = _extends((_extends2 = {}, _extends2[constants.FOCUS_DISABLED] = disabled && "disabled", _extends2[constants.FOCUS_GROUP] = group, _extends2), containerProps);
  var hasLeadingGuards = noFocusGuards !== true;
  var hasTailingGuards = hasLeadingGuards && noFocusGuards !== "tail";
  var mergedRef = useMergeRef.useMergeRefs([parentRef, setObserveNode]);
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
    children: [hasLeadingGuards && [
      /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        "data-focus-guard": true,
        tabIndex: disabled ? -1 : 0,
        style: FocusGuard.hiddenGuard
      }, "guard-first"),
      hasPositiveIndices ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        "data-focus-guard": true,
        tabIndex: disabled ? -1 : 1,
        style: FocusGuard.hiddenGuard
      }, "guard-nearest") : null
    ], !disabled && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(SideCar, {
      id,
      sideCar: medium.mediumSidecar,
      observed: realObserved,
      disabled,
      persistentFocus,
      crossFrame,
      autoFocus,
      whiteList,
      shards,
      onActivation,
      onDeactivation,
      returnFocus,
      focusOptions
    }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(Container, {
      ref: mergedRef,
      ...lockProps,
      className,
      onBlur,
      onFocus,
      children
    }), hasTailingGuards && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      "data-focus-guard": true,
      tabIndex: disabled ? -1 : 0,
      style: FocusGuard.hiddenGuard
    })]
  });
});
FocusLock.propTypes = process.env.NODE_ENV !== "production" ? {
  children: PropTypes.node,
  disabled: PropTypes.bool,
  returnFocus: PropTypes.oneOfType([PropTypes.bool, PropTypes.object, PropTypes.func]),
  focusOptions: PropTypes.object,
  noFocusGuards: PropTypes.bool,
  hasPositiveIndices: PropTypes.bool,
  allowTextSelection: PropTypes.bool,
  autoFocus: PropTypes.bool,
  persistentFocus: PropTypes.bool,
  crossFrame: PropTypes.bool,
  group: PropTypes.string,
  className: PropTypes.string,
  whiteList: PropTypes.func,
  shards: PropTypes.arrayOf(PropTypes.any),
  as: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]),
  lockProps: PropTypes.object,
  onActivation: PropTypes.func,
  onDeactivation: PropTypes.func,
  sideCar: PropTypes.any.isRequired
} : {};
FocusLock.defaultProps = {
  children: void 0,
  disabled: false,
  returnFocus: false,
  focusOptions: void 0,
  noFocusGuards: false,
  autoFocus: true,
  persistentFocus: false,
  crossFrame: true,
  hasPositiveIndices: void 0,
  allowTextSelection: void 0,
  group: void 0,
  className: void 0,
  whiteList: void 0,
  shards: void 0,
  as: "div",
  lockProps: {},
  onActivation: void 0,
  onDeactivation: void 0
};
const FocusLockUI2 = FocusLock;
module.exports = FocusLockUI2;
//# sourceMappingURL=Lock.js.map
