"use strict";

exports.__esModule = true;
exports.SidePanelModal = void 0;
var _mobxReactLite = require("mobx-react-lite");
var _bexCore = require("@wix/bex-core");
var _react = require("@wix/bex-core/react");
var _SlidingModal = require("../SlidingModal");
var _react2 = _interopRequireWildcard(require("react"));
var _mobx = require("mobx");
var _SidePanelModalItem = require("./SidePanelModalItem");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/SidePanelModal/SidePanelModal.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); }
function _SidePanelModal(props) {
  const {
    top,
    left,
    state
  } = props;
  const {
    onSidePanelOpen,
    closeSidePanel,
    isPanelLayout,
    useRelativeSidePanel
  } = (0, _react.useWixPatternsContainer)();
  const pageState = (0, _react.usePageContext)();
  const {
    sidePanel,
    sidePanelComponents
  } = state;
  const {
    isOpen
  } = sidePanel;
  const width = (0, _react2.useMemo)(() => isPanelLayout ? '100%' : _bexCore.SidePanelWidth, [isPanelLayout]);
  const style = (0, _react2.useMemo)(() => ({
    top,
    ...(useRelativeSidePanel ? {
      right: isPanelLayout ? undefined : 0
    } : {
      left: isPanelLayout ? 0 : left != null ? left - _bexCore.SidePanelWidth : undefined
    }),
    bottom: 0,
    overflow: 'hidden',
    width
  }), [top, left, width]);
  (0, _react2.useEffect)(() => {
    return (0, _mobx.reaction)(() => sidePanel.isOpen, sidePanelOpen => {
      pageState.sidePanelOpen = sidePanelOpen;
    });
  }, []);
  (0, _react2.useEffect)(() => {
    const onGlobalSidePanelOpenSub = onSidePanelOpen == null ? void 0 : onSidePanelOpen(() => {
      state.closeSidePanel();
    });
    const disposers = [(0, _mobx.reaction)(() => sidePanel.isOpen, sidePanelShow => {
      if (sidePanelShow) {
        closeSidePanel == null || closeSidePanel();
      }
    })];
    return () => {
      onGlobalSidePanelOpenSub == null || onGlobalSidePanelOpenSub.remove();
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }, []);
  return /*#__PURE__*/_react2.default.createElement(_SlidingModal.SlidingModal, {
    dataHook: "cairo-side-panel-modal",
    transitionStatus: sidePanel.transition.status,
    style: style,
    appendTo: isPanelLayout || useRelativeSidePanel ? 'parent' : undefined,
    overlay: isPanelLayout,
    contentRef: el => {
      sidePanel.focus.modalElement = el;
    },
    onExited: () => {
      sidePanel.transition.setStatus('exited');
    },
    onEntered: () => {
      sidePanel.transition.setStatus('entered');
    },
    onEntering: () => {
      sidePanel.transition.setStatus('entering');
    },
    onExiting: () => {
      sidePanel.transition.setStatus('exiting');
    },
    width: _bexCore.SidePanelWidth,
    isOpen: isOpen,
    onRequestClose: () => {
      sidePanel.close();
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 88,
      columnNumber: 5
    }
  }, Array.from(sidePanelComponents.entries()).map(([key, ComponentType]) => {
    return /*#__PURE__*/_react2.default.createElement(_SidePanelModalItem.SidePanelModalItem, {
      key: key,
      id: key,
      width: width,
      state: state,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 117,
        columnNumber: 11
      }
    }, /*#__PURE__*/_react2.default.createElement(ComponentType, {
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 118,
        columnNumber: 13
      }
    }));
  }));
}
const SidePanelModal = exports.SidePanelModal = (0, _mobxReactLite.observer)(_SidePanelModal);
//# sourceMappingURL=SidePanelModal.js.map