"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _portal = require("../../../portal");
var _contexts = require("../../contexts");
var _bottomSheetContainer = _interopRequireDefault(require("../bottomSheetContainer"));
var _constants = require("../../constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// @ts-nocheck

const BottomSheetModalProviderWrapper = _ref => {
  let {
    children
  } = _ref;
  //#region layout state
  const [containerHeight, setContainerHeight] = (0, _react.useState)(_constants.WINDOW_HEIGHT);
  //#endregion

  //#region variables
  const sheetsQueueRef = (0, _react.useRef)([]);
  //#endregion

  //#region callback
  const handleOnContainerMeasureHeight = (0, _react.useCallback)(height => {
    setContainerHeight(height);
  }, []);
  //#endregion

  //#region private methods
  const handleMountSheet = (0, _react.useCallback)((key, ref) => {
    var _sheetsQueueRef$curre;
    const _sheetsQueue = (_sheetsQueueRef$curre = sheetsQueueRef.current) === null || _sheetsQueueRef$curre === void 0 ? void 0 : _sheetsQueueRef$curre.slice();
    const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
    const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;

    /**
     * Exit the method, if sheet is already presented
     * and at the top.
     */
    if (sheetIndex !== -1 && sheetOnTop) {
      return;
    }

    /**
     * Minimize the current sheet if:
     * - it exists.
     * - it is not unmounting.
     */
    const currentMountedSheet = _sheetsQueue[_sheetsQueue.length - 1];
    if (currentMountedSheet && !currentMountedSheet.willUnmount) {
      try {
        var _currentMountedSheet$;
        (_currentMountedSheet$ = currentMountedSheet.ref.current) === null || _currentMountedSheet$ === void 0 ? void 0 : _currentMountedSheet$.minimize();
      } catch (e) {
        handleDismissAll();
      }
    }

    /**
     * Restore and remove incoming sheet from the queue,
     * if it was registered.
     */
    if (sheetIndex !== -1) {
      var _ref$current;
      _sheetsQueue.splice(sheetIndex, 1);
      (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.restore();
    }
    _sheetsQueue.push({
      key,
      ref,
      willUnmount: false
    });
    sheetsQueueRef.current = _sheetsQueue;
  }, []);
  const handleUnmountSheet = (0, _react.useCallback)(key => {
    var _sheetsQueueRef$curre2, _sheetsQueueRef$curre3;
    const _sheetsQueue = (_sheetsQueueRef$curre2 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre2 === void 0 ? void 0 : _sheetsQueueRef$curre2.slice();
    const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
    const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;

    /**
     * Here we remove the unmounted sheet and update
     * the sheets queue.
     */
    _sheetsQueue.splice(sheetIndex, 1);
    sheetsQueueRef.current = _sheetsQueue;

    /**
     * Here we try to restore previous sheet position if unmounted
     * sheet was on top. This is needed when user dismiss
     * the modal by panning down.
     */
    const hasMinimizedSheet = ((_sheetsQueueRef$curre3 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre3 === void 0 ? void 0 : _sheetsQueueRef$curre3.length) > 0;
    if (sheetOnTop && hasMinimizedSheet) {
      var _sheetsQueueRef$curre4, _sheetsQueueRef$curre5;
      (_sheetsQueueRef$curre4 = sheetsQueueRef.current[((_sheetsQueueRef$curre5 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre5 === void 0 ? void 0 : _sheetsQueueRef$curre5.length) - 1].ref.current) === null || _sheetsQueueRef$curre4 === void 0 ? void 0 : _sheetsQueueRef$curre4.restore();
    }
  }, []);
  const handleWillUnmountSheet = (0, _react.useCallback)(key => {
    var _sheetsQueueRef$curre6;
    const _sheetsQueue = (_sheetsQueueRef$curre6 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre6 === void 0 ? void 0 : _sheetsQueueRef$curre6.slice();
    const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
    const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;

    /**
     * Here we mark the sheet that will unmount,
     * so it won't be restored.
     */
    if (sheetIndex !== -1) {
      _sheetsQueue[sheetIndex].willUnmount = true;
    }

    /**
     * Here we try to restore previous sheet position,
     * This is needed when user dismiss the modal by fire the dismiss action.
     */
    const hasMinimizedSheet = _sheetsQueue.length > 1;
    if (sheetOnTop && hasMinimizedSheet) {
      var _sheetsQueue$ref$curr;
      (_sheetsQueue$ref$curr = _sheetsQueue[_sheetsQueue.length - 2].ref.current) === null || _sheetsQueue$ref$curr === void 0 ? void 0 : _sheetsQueue$ref$curr.restore();
    }
    sheetsQueueRef.current = _sheetsQueue;
  }, []);
  //#endregion

  //#region public methods
  const handleDismiss = (0, _react.useCallback)(key => {
    var _sheetsQueueRef$curre7;
    const sheetToBeDismissed = (_sheetsQueueRef$curre7 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre7 === void 0 ? void 0 : _sheetsQueueRef$curre7.find(item => item.key === key);
    if (sheetToBeDismissed) {
      var _sheetToBeDismissed$r;
      (_sheetToBeDismissed$r = sheetToBeDismissed.ref.current) === null || _sheetToBeDismissed$r === void 0 ? void 0 : _sheetToBeDismissed$r.dismiss(true);
    }
  }, []);
  const handleDismissAll = (0, _react.useCallback)(() => {
    var _sheetsQueueRef$curre8;
    (_sheetsQueueRef$curre8 = sheetsQueueRef.current) === null || _sheetsQueueRef$curre8 === void 0 ? void 0 : _sheetsQueueRef$curre8.map(item => {
      var _item$ref$current;
      (_item$ref$current = item.ref.current) === null || _item$ref$current === void 0 ? void 0 : _item$ref$current.dismiss(true);
    });
  }, []);
  //#endregion

  //#region context variables
  const externalContextVariables = (0, _react.useMemo)(() => ({
    dismiss: handleDismiss,
    dismissAll: handleDismissAll
  }), [handleDismiss, handleDismissAll]);
  const internalContextVariables = (0, _react.useMemo)(() => ({
    containerHeight,
    mountSheet: handleMountSheet,
    unmountSheet: handleUnmountSheet,
    willUnmountSheet: handleWillUnmountSheet
  }), [containerHeight, handleMountSheet, handleUnmountSheet, handleWillUnmountSheet]);
  //#endregion

  //#region renders
  return /*#__PURE__*/_react.default.createElement(_contexts.BottomSheetModalProvider, {
    value: externalContextVariables
  }, /*#__PURE__*/_react.default.createElement(_contexts.BottomSheetModalInternalProvider, {
    value: internalContextVariables
  }, /*#__PURE__*/_react.default.createElement(_bottomSheetContainer.default, {
    shouldMeasureHeight: true,
    onMeasureHeight: handleOnContainerMeasureHeight,
    children: null
  }), /*#__PURE__*/_react.default.createElement(_portal.PortalProvider, null, children)));
  //#endregion
};
var _default = BottomSheetModalProviderWrapper;
exports.default = _default;
//# sourceMappingURL=BottomSheetModalProvider.js.map