"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _utils = require("../utils");
var _Focuser = require("../Overlay/Focuser");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const canSkip = node => node instanceof HTMLElement && node.dataset && node.dataset.skipAutoFocus;
const createFocusStrategy = checkSkipAutoFocusRef => {
  const elements = [];
  return {
    elements,
    collect(container) {
      let reset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      if (reset) {
        this.elements = [];
      }
      for (let i = 0; i < container.children.length; i++) {
        const element = container.children[i];
        if (checkSkipAutoFocusRef.current) {
          if (canSkip(element)) {
            continue;
          }
        }
        this.elements.push(element);
        this.collect(element);
      }
    },
    getFirst(container) {
      this.collect(container, true);
      return this.elements[0];
    },
    getLast(container) {
      this.collect(container, true);
      return this.elements[this.elements.length - 1];
    },
    getNext(_container, element) {
      if (element) {
        const index = this.elements.indexOf(element);
        if (index !== -1) {
          return this.elements[index + 1];
        }
      }
      return undefined;
    },
    getPrev(_container, element) {
      if (element) {
        const index = this.elements.indexOf(element);
        if (index !== -1) {
          return this.elements[index - 1];
        }
      }
      return undefined;
    }
  };
};
const FocusTrap = (_ref, ref) => {
  let {
    children,
    onFocus,
    ...props
  } = _ref;
  const rootRef = (0, _react.useRef)(null);
  const trapBeforeRef = (0, _react.useRef)(null);
  const trapAfterRef = (0, _react.useRef)(null);
  const checkSkipAutoFocusRef = (0, _react.useRef)(false);
  const isFitForFocus = node => {
    if (node === trapBeforeRef.current || node === trapAfterRef.current) {
      return false;
    }
    return (0, _Focuser.isFocusable)(node);
  };
  const {
    focusFirst,
    focusLast
  } = (0, _react.useMemo)(() => {
    const strategy = createFocusStrategy(checkSkipAutoFocusRef);
    return (0, _utils.createFocusManager)(rootRef, isFitForFocus, strategy);
  }, []);
  const handleFocus = (0, _utils.wrapFunction)(onFocus, e => {
    if (!rootRef.current) {
      return;
    }
    switch (true) {
      case e.target === trapBeforeRef.current:
        if (rootRef.current !== e.relatedTarget && e.relatedTarget instanceof HTMLElement && rootRef.current.contains(e.relatedTarget)) {
          focusLast();
        } else {
          focusFirst();
        }
        break;
      case e.target === trapAfterRef.current:
        if (rootRef.current !== e.relatedTarget && e.relatedTarget instanceof HTMLElement && rootRef.current.contains(e.relatedTarget)) {
          focusFirst();
        } else {
          focusLast();
        }
        break;
    }
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    onFocus: handleFocus,
    ref: (0, _utils.mergeRefs)(rootRef, ref),
    tabIndex: -1,
    ...props,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      tabIndex: 0,
      ref: trapBeforeRef
    }), children, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      tabIndex: 0,
      ref: trapAfterRef
    })]
  });
};
FocusTrap.displayName = 'FocusTrap';
var _default = exports.default = /*#__PURE__*/(0, _react.forwardRef)(FocusTrap);