"use strict";

exports.__esModule = true;
exports.getAnchorParam = exports.FocusableWrapper = exports.FocusableContext = void 0;
var _react = _interopRequireWildcard(require("react"));
var _FocusableService = require("./FocusableService");
var _scrollAndFocus = require("./scrollAndFocus");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/utils/dist/cjs/FocusableWrapper/FocusableWrapper.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const getAnchorParam = () => {
  const searchParams = new URLSearchParams(window.location.search);
  return searchParams.get('anchor');
};
exports.getAnchorParam = getAnchorParam;
const FocusableContext = exports.FocusableContext = /*#__PURE__*/_react.default.createContext(null);
const FocusableWrapper = ({
  scrollableRef,
  getScrollableTrigger,
  getDoesItemExist,
  children
}) => {
  const focusableItemsService = (0, _react.useMemo)(() => new _FocusableService.FocusableItemsService(), []);
  const scrollIfNeeded = (0, _react.useCallback)(() => {
    const focusedItemName = getAnchorParam();
    scrollByItemName(focusedItemName);
  }, [scrollableRef]);
  const scrollByItemName = (0, _react.useCallback)(focusedItemName => {
    if (!focusedItemName) {
      return;
    }
    const containerRefByItem = focusableItemsService.getContainerRefByItem(focusedItemName);
    if (containerRefByItem) {
      const focusedItemRef = focusableItemsService.getItemRef(focusedItemName);
      const afterScroll = focusableItemsService.getItemAfterScroll(focusedItemName);
      return (0, _scrollAndFocus.scrollAndFocus)({
        scrollableRef,
        containerRef: containerRefByItem,
        focusedItemRef,
        afterScroll
      });
    }
    const containerRef = focusableItemsService.getContainerRefByName(focusedItemName);
    if (containerRef) {
      (0, _scrollAndFocus.scrollAndFocus)({
        scrollableRef,
        containerRef,
        focusedItemRef: containerRef
      });
    }
  }, [focusableItemsService, scrollableRef]);
  const doesItemExist = (0, _react.useCallback)(() => {
    const focusedItemName = getAnchorParam();
    if (!focusedItemName) {
      return false;
    }
    return Boolean(focusableItemsService.getContainerRefByItem(focusedItemName));
  }, [focusableItemsService]);
  (0, _react.useEffect)(() => {
    getScrollableTrigger(scrollIfNeeded);
    getDoesItemExist(doesItemExist);
  }, [getScrollableTrigger, getDoesItemExist, scrollIfNeeded, doesItemExist]);
  const contextValue = (0, _react.useMemo)(() => ({
    focusable: {
      registerScrollableItem: scrollableItem => {
        focusableItemsService.addContainer(scrollableItem);
        focusableItemsService.addItem(scrollableItem);
      }
    }
  }), [focusableItemsService]);
  return /*#__PURE__*/_react.default.createElement(FocusableContext.Provider, {
    value: contextValue,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 100,
      columnNumber: 5
    }
  }, children);
};
exports.FocusableWrapper = FocusableWrapper;
//# sourceMappingURL=FocusableWrapper.js.map