"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SheetWrap = exports.SheetContent = void 0;

var _react = _interopRequireWildcard(require("react"));

var _native = _interopRequireDefault(require("../native"));

var _indexModule = _interopRequireDefault(require("./index.module.less"));

var _classnames = _interopRequireDefault(require("classnames"));

var _space = _interopRequireDefault(require("../space"));

var _switchVariable = _interopRequireDefault(require("../switch-variable"));

var _getCurrentPage = _interopRequireDefault(require("../get-current-page"));

var _configProvider = require("../config-provider");

var _wrap = _interopRequireDefault(require("./wrap"));

var _getPlatform = _interopRequireDefault(require("../get-platform"));

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

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 _getRequireWildcardCache(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; }

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; }

var SheetContent = /*#__PURE__*/(0, _react.createContext)(false);
exports.SheetContent = SheetContent;
var Sheet = /*#__PURE__*/(0, _react.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 = (0, _configProvider.useConfig)(),
      setIsShowSheetPage = _useConfig.setIsShowSheetPage;

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

      return (_nativeRef$current = nativeRef.current) === null || _nativeRef$current === void 0 ? void 0 : _nativeRef$current.setData({
        className: (0, _classnames.default)(_indexModule.default.sheet, className, visible && _indexModule.default.show)
      });
    }
  });
  var currentPage = (0, _getCurrentPage.default)();

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

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


        setIsShowSheetPage === null || setIsShowSheetPage === void 0 ? void 0 : setIsShowSheetPage(visible ? currentPage : '');
        sheetInstanceRef.current.setVisible(visible);
      }
    });
  });
  (0, _react.useEffect)(function () {
    return function () {
      //@ts-ignore
      setIsShowSheetPage === null || setIsShowSheetPage === void 0 ? void 0 : setIsShowSheetPage('');
    };
  }, [setIsShowSheetPage]);
  var content = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react.default.createElement(SheetContent.Provider, {
      value: true
    }, /*#__PURE__*/_react.default.createElement(_wrap.default, {
      visible: visible
    }, /*#__PURE__*/_react.default.createElement(_space.default, {
      className: (0, _classnames.default)(_indexModule.default.content, visible && _indexModule.default.showContent, contentCls, _indexModule.default[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.default !== 'native') {
          _setVisible(false);
        }

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

  var page = (0, _getCurrentPage.default)();
  var sheetRef = (0, _react.useRef)(null);

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

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

  (0, _react.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]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      promiseRef: promiseFn,
      sheetRef: sheetRef.current
    };
  }, [promiseFn]);
  return /*#__PURE__*/_react.default.createElement(Sheet, _objectSpread(_objectSpread({
    ref: sheetRef
  }, props), sheetProps), children);
});
exports.SheetWrap = SheetWrap;