"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _icons() {
  const data = require("@react-vant/icons");

  _icons = function () {
    return data;
  };

  return data;
}

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

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

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

var _render = require("../utils/dom/render");

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

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

Dialog.show = props => {
  const defaultOptions = {
    overlay: true,
    closeable: false,
    closeIcon: (0, _jsxRuntime().jsx)(_icons().Cross, {}),
    lockScroll: true,
    transition: 'rv-dialog-bounce',
    showConfirmButton: true,
    showCancelButton: false,
    closeOnClickOverlay: false
  };
  const {
    onClosed,
    onCancel = _utils.noop,
    onConfirm = _utils.noop,
    onClose = _utils.noop,
    cancelProps,
    confirmProps
  } = props,
        restProps = (0, _tslib().__rest)(props, ["onClosed", "onCancel", "onConfirm", "onClose", "cancelProps", "confirmProps"]);
  const userContainer = (0, _getContainer.resolveContainer)(props.teleport);
  const container = document.createElement('div');
  userContainer.appendChild(container);
  let destroy = _utils.noop;

  const TempDialog = () => {
    const [visible, setVisible] = (0, _react().useState)(false);
    const [cancelLoading, setCancelLoading] = (0, _react().useState)(false);
    const [okLoading, setOkLoading] = (0, _react().useState)(false);
    (0, _react().useEffect)(() => {
      setVisible(true);
    }, []);

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

    const _afterClose = () => {
      if (onClosed) {
        onClosed();
      }

      const unmountResult = (0, _render.unmount)(container);

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

    const _onConfirm = e => (0, _tslib().__awaiter)(void 0, void 0, void 0, function* () {
      const i = setTimeout(() => setOkLoading(true));

      if ((yield onConfirm(e)) !== false) {
        clearTimeout(i);
        destroy();
      } else {
        clearTimeout(i);
        setOkLoading(false);
      }
    });

    const _onCancel = (e, clickOverlay) => (0, _tslib().__awaiter)(void 0, void 0, void 0, function* () {
      if (clickOverlay) {
        destroy();
        return;
      }

      const i = setTimeout(() => setCancelLoading(true));

      if ((yield onCancel(e)) !== false) {
        clearTimeout(i);
        destroy();
      } else {
        clearTimeout(i);
        setCancelLoading(false);
      }
    });

    return (0, _jsxRuntime().jsx)(_Dialog.default, Object.assign({}, defaultOptions, restProps, {
      visible: visible,
      teleport: () => container,
      cancelProps: Object.assign({
        loading: cancelLoading
      }, cancelProps),
      confirmProps: Object.assign({
        loading: okLoading
      }, confirmProps),
      onClose: destroy,
      onCancel: _onCancel,
      onConfirm: _onConfirm,
      onClosed: _afterClose
    }));
  };

  (0, _render.render)((0, _jsxRuntime().jsx)(TempDialog, {}), container);
  return destroy;
}; // 可使用 async/await 的方式


Dialog.alert = props => {
  const {
    onConfirm = _utils.noop
  } = props;
  return new Promise(resolve => {
    Dialog.show(Object.assign(Object.assign({}, props), {
      onConfirm: e => {
        onConfirm(e);
        resolve(e);
      }
    }));
  });
};

Dialog.confirm = props => {
  const {
    onCancel = _utils.noop,
    onConfirm = _utils.noop
  } = props;
  return new Promise((resolve, reject) => {
    Dialog.show(Object.assign(Object.assign({
      // 强制显示 OK Btn
      showCancelButton: true
    }, props), {
      onCancel: e => {
        onCancel(e);
        reject();
      },
      onConfirm: e => {
        onConfirm(e);
        resolve(true);
      }
    }));
  });
};

var _default = Dialog;
exports.default = _default;