// @ts-nocheck
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { PortalProvider } from '../../../portal';
import { BottomSheetModalProvider, BottomSheetModalInternalProvider } from '../../contexts';
import BottomSheetContainer from '../bottomSheetContainer';
import { WINDOW_HEIGHT } from '../../constants';
const BottomSheetModalProviderWrapper = _ref => {
  let {
    children
  } = _ref;
  //#region layout state
  const [containerHeight, setContainerHeight] = useState(WINDOW_HEIGHT);
  //#endregion

  //#region variables
  const sheetsQueueRef = useRef([]);
  //#endregion

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

  //#region private methods
  const handleMountSheet = 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 = 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 = 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 = 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 = 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 = useMemo(() => ({
    dismiss: handleDismiss,
    dismissAll: handleDismissAll
  }), [handleDismiss, handleDismissAll]);
  const internalContextVariables = useMemo(() => ({
    containerHeight,
    mountSheet: handleMountSheet,
    unmountSheet: handleUnmountSheet,
    willUnmountSheet: handleWillUnmountSheet
  }), [containerHeight, handleMountSheet, handleUnmountSheet, handleWillUnmountSheet]);
  //#endregion

  //#region renders
  return /*#__PURE__*/React.createElement(BottomSheetModalProvider, {
    value: externalContextVariables
  }, /*#__PURE__*/React.createElement(BottomSheetModalInternalProvider, {
    value: internalContextVariables
  }, /*#__PURE__*/React.createElement(BottomSheetContainer, {
    shouldMeasureHeight: true,
    onMeasureHeight: handleOnContainerMeasureHeight,
    children: null
  }), /*#__PURE__*/React.createElement(PortalProvider, null, children)));
  //#endregion
};

export default BottomSheetModalProviderWrapper;
//# sourceMappingURL=BottomSheetModalProvider.js.map