import _extends from "../../../../../@babel_runtime@7.19.0/node_modules/@babel/runtime/helpers/esm/extends.js";
import * as React from "react";
import { useEffect } from "react";
import { node, bool, oneOfType, object, func, string, arrayOf, any } from "prop-types";
import { FOCUS_DISABLED, FOCUS_GROUP } from "../../../../../focus-lock@0.11.3/node_modules/focus-lock/dist/es2015/constants.js";
import { hiddenGuard } from "./FocusGuard.js";
import { mediumFocus, mediumBlur, mediumSidecar } from "./medium.js";
import { jsxs, jsx, Fragment } from "../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
import { useMergeRefs } from "../../../../../use-callback-ref@1.3.0_react@18.2.0/node_modules/use-callback-ref/dist/es2015/useMergeRef.js";
var emptyArray = [];
var FocusLock = /* @__PURE__ */ React.forwardRef(function FocusLockUI(props, parentRef) {
  var _extends2;
  var _React$useState = React.useState(), realObserved = _React$useState[0], setObserved = _React$useState[1];
  var observed = React.useRef();
  var isActive = React.useRef(false);
  var originalFocusedElement = React.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.useState({}), id = _React$useState2[0];
  var onActivation = React.useCallback(function() {
    originalFocusedElement.current = originalFocusedElement.current || document && document.activeElement;
    if (observed.current && onActivationCallback) {
      onActivationCallback(observed.current);
    }
    isActive.current = true;
  }, [onActivationCallback]);
  var onDeactivation = React.useCallback(function() {
    isActive.current = false;
    if (onDeactivationCallback) {
      onDeactivationCallback(observed.current);
    }
  }, [onDeactivationCallback]);
  useEffect(function() {
    if (!disabled) {
      originalFocusedElement.current = null;
    }
  }, []);
  var returnFocus = React.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.useCallback(function(event) {
    if (isActive.current) {
      mediumFocus.useMedium(event);
    }
  }, []);
  var onBlur = mediumBlur.useMedium;
  var setObserveNode = React.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.useEffect(function() {
      if (!observed.current) {
        console.error("FocusLock: could not obtain ref to internal node");
      }
    }, []);
  }
  var lockProps = _extends((_extends2 = {}, _extends2[FOCUS_DISABLED] = disabled && "disabled", _extends2[FOCUS_GROUP] = group, _extends2), containerProps);
  var hasLeadingGuards = noFocusGuards !== true;
  var hasTailingGuards = hasLeadingGuards && noFocusGuards !== "tail";
  var mergedRef = useMergeRefs([parentRef, setObserveNode]);
  return /* @__PURE__ */ jsxs(Fragment, {
    children: [hasLeadingGuards && [
      /* @__PURE__ */ jsx("div", {
        "data-focus-guard": true,
        tabIndex: disabled ? -1 : 0,
        style: hiddenGuard
      }, "guard-first"),
      hasPositiveIndices ? /* @__PURE__ */ jsx("div", {
        "data-focus-guard": true,
        tabIndex: disabled ? -1 : 1,
        style: hiddenGuard
      }, "guard-nearest") : null
    ], !disabled && /* @__PURE__ */ jsx(SideCar, {
      id,
      sideCar: mediumSidecar,
      observed: realObserved,
      disabled,
      persistentFocus,
      crossFrame,
      autoFocus,
      whiteList,
      shards,
      onActivation,
      onDeactivation,
      returnFocus,
      focusOptions
    }), /* @__PURE__ */ jsx(Container, {
      ref: mergedRef,
      ...lockProps,
      className,
      onBlur,
      onFocus,
      children
    }), hasTailingGuards && /* @__PURE__ */ jsx("div", {
      "data-focus-guard": true,
      tabIndex: disabled ? -1 : 0,
      style: hiddenGuard
    })]
  });
});
FocusLock.propTypes = process.env.NODE_ENV !== "production" ? {
  children: node,
  disabled: bool,
  returnFocus: oneOfType([bool, object, func]),
  focusOptions: object,
  noFocusGuards: bool,
  hasPositiveIndices: bool,
  allowTextSelection: bool,
  autoFocus: bool,
  persistentFocus: bool,
  crossFrame: bool,
  group: string,
  className: string,
  whiteList: func,
  shards: arrayOf(any),
  as: oneOfType([string, func, object]),
  lockProps: object,
  onActivation: func,
  onDeactivation: func,
  sideCar: 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;
export {
  FocusLockUI2 as default
};
//# sourceMappingURL=Lock.js.map
