"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _icons = require("@react-vant/icons");

var _utils = require("../utils");

var _Dialog = _interopRequireDefault(require("./Dialog"));

var _getContainer = require("../utils/dom/getContainer");

require("./style/index.css");

var _excluded = ["onClosed", "onCancel", "onConfirm", "onClose", "cancelProps", "confirmProps"];

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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

var Dialog = _Dialog.default; // 可返回用于销毁此弹窗的方法

Dialog.show = function (props) {
  var defaultOptions = {
    overlay: true,
    closeable: false,
    closeIcon: /*#__PURE__*/_react.default.createElement(_icons.Cross, null),
    lockScroll: true,
    transition: 'rv-dialog-bounce',
    showConfirmButton: true,
    showCancelButton: false,
    closeOnClickOverlay: false
  };

  var onClosed = props.onClosed,
      _props$onCancel = props.onCancel,
      onCancel = _props$onCancel === void 0 ? _utils.noop : _props$onCancel,
      _props$onConfirm = props.onConfirm,
      onConfirm = _props$onConfirm === void 0 ? _utils.noop : _props$onConfirm,
      _props$onClose = props.onClose,
      onClose = _props$onClose === void 0 ? _utils.noop : _props$onClose,
      cancelProps = props.cancelProps,
      confirmProps = props.confirmProps,
      restProps = _objectWithoutPropertiesLoose(props, _excluded);

  var userContainer = (0, _getContainer.resolveContainer)(props.teleport);
  var container = document.createElement('div');
  userContainer.appendChild(container);
  var destroy = _utils.noop;

  var TempDialog = function TempDialog() {
    var _useState = (0, _react.useState)(false),
        visible = _useState[0],
        setVisible = _useState[1];

    var _useState2 = (0, _react.useState)(false),
        cancelLoading = _useState2[0],
        setCancelLoading = _useState2[1];

    var _useState3 = (0, _react.useState)(false),
        okLoading = _useState3[0],
        setOkLoading = _useState3[1];

    (0, _react.useEffect)(function () {
      setVisible(true);
    }, []);

    destroy = function destroy() {
      setVisible(false);
      if (onClose) onClose();
    };

    var _afterClose = function _afterClose() {
      if (onClosed) {
        onClosed();
      }

      var unmountResult = _reactDom.default.unmountComponentAtNode(container);

      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    };

    var _onConfirm = /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(e) {
        var i;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                i = setTimeout(function () {
                  return setOkLoading(true);
                });
                _context.next = 3;
                return onConfirm(e);

              case 3:
                _context.t0 = _context.sent;

                if (!(_context.t0 !== false)) {
                  _context.next = 9;
                  break;
                }

                clearTimeout(i);
                destroy();
                _context.next = 11;
                break;

              case 9:
                clearTimeout(i);
                setOkLoading(false);

              case 11:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function _onConfirm(_x) {
        return _ref.apply(this, arguments);
      };
    }();

    var _onCancel = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(e, clickOverlay) {
        var i;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                if (!clickOverlay) {
                  _context2.next = 3;
                  break;
                }

                destroy();
                return _context2.abrupt("return");

              case 3:
                i = setTimeout(function () {
                  return setCancelLoading(true);
                });
                _context2.next = 6;
                return onCancel(e);

              case 6:
                _context2.t0 = _context2.sent;

                if (!(_context2.t0 !== false)) {
                  _context2.next = 12;
                  break;
                }

                clearTimeout(i);
                destroy();
                _context2.next = 14;
                break;

              case 12:
                clearTimeout(i);
                setCancelLoading(false);

              case 14:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      return function _onCancel(_x2, _x3) {
        return _ref2.apply(this, arguments);
      };
    }();

    return /*#__PURE__*/_react.default.createElement(_Dialog.default, _extends({}, defaultOptions, restProps, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      cancelProps: _extends({
        loading: cancelLoading
      }, cancelProps),
      confirmProps: _extends({
        loading: okLoading
      }, confirmProps),
      onClose: destroy,
      onCancel: _onCancel,
      onConfirm: _onConfirm,
      onClosed: _afterClose
    }));
  };

  _reactDom.default.render( /*#__PURE__*/_react.default.createElement(TempDialog, null), container);

  return destroy;
}; // 可使用 async/await 的方式


Dialog.alert = function (props) {
  var _props$onConfirm2 = props.onConfirm,
      _onConfirm2 = _props$onConfirm2 === void 0 ? _utils.noop : _props$onConfirm2;

  return new Promise(function (resolve) {
    Dialog.show(_extends({}, props, {
      onConfirm: function onConfirm(e) {
        _onConfirm2(e);

        resolve(e);
      }
    }));
  });
};

Dialog.confirm = function (props) {
  var _props$onCancel2 = props.onCancel,
      _onCancel2 = _props$onCancel2 === void 0 ? _utils.noop : _props$onCancel2,
      _props$onConfirm3 = props.onConfirm,
      _onConfirm3 = _props$onConfirm3 === void 0 ? _utils.noop : _props$onConfirm3;

  return new Promise(function (resolve, reject) {
    Dialog.show(_extends({
      // 强制显示 OK Btn
      showCancelButton: true
    }, props, {
      onCancel: function onCancel(e) {
        _onCancel2(e);

        reject();
      },
      onConfirm: function onConfirm(e) {
        _onConfirm3(e);

        resolve(true);
      }
    }));
  });
};

var _default = Dialog;
exports.default = _default;