var _excluded = ['data', 'children', 'setOptions'],
  _excluded2 = ['maskClosable'];

function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}

function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}

function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly &&
      (symbols = symbols.filter(function(sym) {
        return Object.getOwnPropertyDescriptor(object, sym).enumerable;
      })),
      keys.push.apply(keys, symbols);
  }
  return keys;
}

function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2
      ? ownKeys(Object(source), !0).forEach(function(key) {
          _defineProperty(target, key, source[key]);
        })
      : Object.getOwnPropertyDescriptors
      ? Object.defineProperties(
          target,
          Object.getOwnPropertyDescriptors(source),
        )
      : ownKeys(Object(source)).forEach(function(key) {
          Object.defineProperty(
            target,
            key,
            Object.getOwnPropertyDescriptor(source, key),
          );
        });
  }
  return target;
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true,
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

function _slicedToArray(arr, i) {
  return (
    _arrayWithHoles(arr) ||
    _iterableToArrayLimit(arr, i) ||
    _unsupportedIterableToArray(arr, i) ||
    _nonIterableRest()
  );
}

function _nonIterableRest() {
  throw new TypeError(
    'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}

function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}

function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) {
    arr2[i] = arr[i];
  }
  return arr2;
}

function _iterableToArrayLimit(arr, i) {
  var _i =
    arr == null
      ? null
      : (typeof Symbol !== 'undefined' && arr[Symbol.iterator]) ||
        arr['@@iterator'];
  if (_i == null) return;
  var _arr = [];
  var _n = true;
  var _d = false;
  var _s, _e;
  try {
    for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
      _arr.push(_s.value);
      if (i && _arr.length === i) break;
    }
  } catch (err) {
    _d = true;
    _e = err;
  } finally {
    try {
      if (!_n && _i['return'] != null) _i['return']();
    } finally {
      if (_d) throw _e;
    }
  }
  return _arr;
}

function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}

import React, {
  forwardRef,
  useEffect,
  useImperativeHandle,
  useMemo,
  useRef,
  useState,
  createContext,
} from 'react';
import Native from '../native';
import styles from './index.module.less';
import classNames from 'classnames';
import Space from '../space';
import switchVariable from '../switch-variable';
import getCurrentPage from '../get-current-page';
import { useConfig } from '../config-provider';
import Wrap from './wrap';
import getPlatform from '../get-platform';
export var SheetContent = /*#__PURE__*/ createContext(false);
var Sheet = /*#__PURE__*/ forwardRef(function(_ref, ref) {
  var style = _ref.style,
    children = _ref.children,
    className = _ref.className,
    _ref$direction = _ref.direction,
    direction = _ref$direction === void 0 ? 'bottom' : _ref$direction,
    contentCls = _ref.contentCls,
    center = _ref.center,
    onClose = _ref.onClose,
    _ref$maskClosable = _ref.maskClosable,
    maskClosable = _ref$maskClosable === void 0 ? true : _ref$maskClosable;

  var _useConfig = useConfig(),
    setIsShowSheetPage = _useConfig.setIsShowSheetPage;

  var nativeRef = useRef(null);
  var sheetInstanceRef = useRef({
    setVisible: function setVisible(visible) {
      var _nativeRef$current;

      return (_nativeRef$current = nativeRef.current) === null ||
        _nativeRef$current === void 0
        ? void 0
        : _nativeRef$current.setData({
            className: classNames(
              styles.sheet,
              className,
              visible && styles.show,
            ),
          });
    },
  });
  var currentPage = getCurrentPage();

  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    _setVisible = _useState2[1];

  useImperativeHandle(ref, function() {
    return _objectSpread(
      _objectSpread({}, sheetInstanceRef.current),
      {},
      {
        setVisible: function setVisible(visible) {
          //@ts-ignore
          if (getPlatform !== 'native') {
            _setVisible(visible);
          } //@ts-ignore

          setIsShowSheetPage === null || setIsShowSheetPage === void 0
            ? void 0
            : setIsShowSheetPage(visible ? currentPage : '');
          sheetInstanceRef.current.setVisible(visible);
        },
      },
    );
  });
  useEffect(
    function() {
      return function() {
        //@ts-ignore
        setIsShowSheetPage === null || setIsShowSheetPage === void 0
          ? void 0
          : setIsShowSheetPage('');
      };
    },
    [setIsShowSheetPage],
  );
  var content = useMemo(
    function() {
      return /*#__PURE__*/ React.createElement(
        SheetContent.Provider,
        {
          value: true,
        },
        /*#__PURE__*/ React.createElement(
          Wrap,
          {
            visible: visible,
          },
          /*#__PURE__*/ React.createElement(
            Space,
            {
              className: classNames(
                styles.content,
                visible && styles.showContent,
                contentCls,
                styles[direction],
              ),
              style: style,
              'active-opacity': false,
              onTap: function onTap() {
                if (!maskClosable) {
                  return;
                } //@ts-ignore

                setIsShowSheetPage === null || setIsShowSheetPage === void 0
                  ? void 0
                  : setIsShowSheetPage(''); //@ts-ignore

                if (getPlatform !== 'native') {
                  _setVisible(false);
                }

                sheetInstanceRef.current.setVisible(false);
                onClose === null || onClose === void 0 ? void 0 : onClose();
              },
              justify: switchVariable({
                default: 'center',
                left: 'flex-start',
                right: 'flex-end',
              })(direction),
              alignItems: center
                ? 'center'
                : switchVariable({
                    bottom: 'flex-end',
                    default: 'flex-start',
                  })(direction),
            },
            /*#__PURE__*/ React.createElement(
              Space,
              {
                'active-opacity': false,
                onTap: function onTap(e) {
                  return e.stopPropagation();
                },
              },
              children,
            ),
          ),
        ),
      );
    },
    [
      center,
      children,
      contentCls,
      direction,
      maskClosable,
      onClose,
      setIsShowSheetPage,
      style,
      visible,
    ],
  );
  return useMemo(
    function() {
      return /*#__PURE__*/ React.createElement(
        Native,
        {
          ref: nativeRef,
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          reRender: false,
          initData: {
            className: classNames(styles.sheet, className),
          },
        },
        content,
      );
    },
    [className, content],
  );
});
export default Sheet;
export var SheetWrap = /*#__PURE__*/ forwardRef(function(_ref2, ref) {
  var data = _ref2.data,
    children = _ref2.children,
    setOptions = _ref2.setOptions,
    props = _objectWithoutProperties(_ref2, _excluded);

  var page = getCurrentPage();
  var sheetRef = useRef(null);

  var _useState3 = useState({
      resolve: function resolve() {},
      reject: function reject() {},
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    promiseFn = _useState4[0],
    setPromiseFn = _useState4[1];

  var _useState5 = useState({}),
    _useState6 = _slicedToArray(_useState5, 2),
    sheetProps = _useState6[0],
    setSheetProps = _useState6[1];

  useEffect(
    function() {
      var _sheetRef$current2;

      data[page] = {
        fn: function fn(_ref3) {
          var maskClosable = _ref3.maskClosable,
            options = _objectWithoutProperties(_ref3, _excluded2);

          return new Promise(function(resolve, reject) {
            var _sheetRef$current;

            setOptions(options);
            setSheetProps({
              maskClosable: maskClosable,
            });
            (_sheetRef$current = sheetRef.current) === null ||
            _sheetRef$current === void 0
              ? void 0
              : _sheetRef$current.setVisible(true);
            setPromiseFn({
              reject: reject,
              resolve: resolve,
            });
          });
        },
        setVisible:
          (_sheetRef$current2 = sheetRef.current) === null ||
          _sheetRef$current2 === void 0
            ? void 0
            : _sheetRef$current2.setVisible,
      };
    },
    [data, page, setOptions],
  );
  useImperativeHandle(
    ref,
    function() {
      return {
        promiseRef: promiseFn,
        sheetRef: sheetRef.current,
      };
    },
    [promiseFn],
  );
  return /*#__PURE__*/ React.createElement(
    Sheet,
    _objectSpread(
      _objectSpread(
        {
          ref: sheetRef,
        },
        props,
      ),
      sheetProps,
    ),
    children,
  );
});
