"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 _await(value, then, direct) {
  if (direct) {
    return then ? then(value) : value;
  }

  if (!value || !value.then) {
    value = Promise.resolve(value);
  }

  return then ? value.then(then) : value;
}

function _async(f) {
  return function () {
    for (var args = [], i = 0; i < arguments.length; i++) {
      args[i] = arguments[i];
    }

    try {
      return Promise.resolve(f.apply(this, args));
    } catch (e) {
      return Promise.reject(e);
    }
  };
}

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 = _async(function (e) {
      var i = setTimeout(function () {
        return setOkLoading(true);
      });
      return _await(onConfirm(e), function (_onConfirm2) {
        if (_onConfirm2 !== false) {
          clearTimeout(i);
          destroy();
        } else {
          clearTimeout(i);
          setOkLoading(false);
        }
      });
    });

    var _onCancel = _async(function (e, clickOverlay) {
      if (clickOverlay) {
        destroy();
        return;
      }

      var i = setTimeout(function () {
        return setCancelLoading(true);
      });
      return _await(onCancel(e), function (_onCancel2) {
        if (_onCancel2 !== false) {
          clearTimeout(i);
          destroy();
        } else {
          clearTimeout(i);
          setCancelLoading(false);
        }
      });
    });

    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,
      _onConfirm3 = _props$onConfirm2 === void 0 ? _utils.noop : _props$onConfirm2;

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

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

Dialog.confirm = function (props) {
  var _props$onCancel2 = props.onCancel,
      _onCancel3 = _props$onCancel2 === void 0 ? _utils.noop : _props$onCancel2,
      _props$onConfirm3 = props.onConfirm,
      _onConfirm4 = _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) {
        _onCancel3(e);

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

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

var _default = Dialog;
exports.default = _default;