"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Side_exports = {};
__export(Side_exports, {
  SideWrapper: () => SideWrapper,
  default: () => Side_default
});
module.exports = __toCommonJS(Side_exports);
var import_react = __toESM(require("react"));
var import_react_transition_group = require("react-transition-group");
var import_Drawer = __toESM(require("../Drawer"));
var import_TransitionSlide = __toESM(require("../../transitions/TransitionSlide"));
var import_Provider = require("./Provider");
const SideWrapper = (props) => {
  var _a;
  const { state, setSide } = (0, import_Provider.useSide)();
  const [queue, setQueue] = (0, import_react.useState)([]);
  const [direction, setDirection] = (0, import_react.useState)("left");
  const [transitionsEnabled, setTransitionsEnabled] = (0, import_react.useState)(false);
  const nonce = (0, import_react.useRef)(0);
  const onClose = (0, import_react.useCallback)(() => setSide(), [setSide]);
  (0, import_react.useEffect)(() => {
    setQueue((q) => {
      if (!state.open || !state.Component)
        return [];
      return q.concat([{ ...state, key: nonce.current++ }]);
    });
  }, [state]);
  (0, import_react.useEffect)(() => {
    let timeout;
    if (queue.length > 1) {
      const [, ...rest] = queue;
      timeout = setTimeout(() => {
        setQueue(rest);
        setDirection("left");
      }, 0);
    }
    return () => {
      if (timeout)
        clearTimeout(timeout);
    };
  }, [queue]);
  const wrappedOnBack = (0, import_react.useCallback)(() => {
    var _a2;
    setDirection("right");
    const onBack = (_a2 = state == null ? void 0 : state.props) == null ? void 0 : _a2.onBack;
    onBack && onBack();
  }, [state == null ? void 0 : state.props]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_Drawer.default,
    {
      ...props,
      isOpen: !!state.open,
      onClose,
      onBack: ((_a = state == null ? void 0 : state.props) == null ? void 0 : _a.onBack) ? wrappedOnBack : void 0,
      setTransitionsEnabled,
      hideNavigation: false
    },
    /* @__PURE__ */ import_react.default.createElement(import_react_transition_group.TransitionGroup, { enter: transitionsEnabled, exit: transitionsEnabled, component: null }, queue.map(({ Component, props: props2, key }) => /* @__PURE__ */ import_react.default.createElement(import_TransitionSlide.default, { key, direction }, Component && /* @__PURE__ */ import_react.default.createElement(Component, { ...props2 }))))
  );
};
var Side_default = SideWrapper;
//# sourceMappingURL=index.js.map
