"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _components = require("../components");

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

var _getTypedIcon = require("../utils/getTypedIcon");

var _icon = _interopRequireDefault(require("../icon"));

var _button = _interopRequireDefault(require("../button"));

var CloseLine = _icon.default.CloseLine;

var defaultCloseIcon = /*#__PURE__*/_react.default.createElement(CloseLine, null);

var ActionModal = function ActionModal(props) {
  var type = props.type,
      title = props.title,
      customPrefixCls = props.prefixCls,
      classname = props.classname,
      centered = props.centered,
      contentCentered = props.contentCentered,
      content = props.content,
      footer = props.footer,
      wrapClassName = props.wrapClassName,
      cancelButtonProps = props.cancelButtonProps,
      okButtonProps = props.okButtonProps,
      cancelText = props.cancelText,
      okText = props.okText,
      icon = props.icon,
      width = props.width,
      size = props.size,
      closeModal = props.closeModal,
      onCancel = props.onCancel,
      onOk = props.onOk,
      closeIcon = props.closeIcon,
      confirmLoading = props.confirmLoading,
      reset = (0, _objectWithoutProperties2.default)(props, ["type", "title", "prefixCls", "classname", "centered", "contentCentered", "content", "footer", "wrapClassName", "cancelButtonProps", "okButtonProps", "cancelText", "okText", "icon", "width", "size", "closeModal", "onCancel", "onOk", "closeIcon", "confirmLoading"]);

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var actionModalPrefixCls = getPrefixClassName('modal', customPrefixCls);
  var actionModalCls = "".concat(actionModalPrefixCls, "-action");

  var _React$useState = _react.default.useState(!!confirmLoading),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      okLoading = _React$useState2[0],
      setOkloading = _React$useState2[1];

  var _React$useState3 = _react.default.useState(false),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      cancelLoading = _React$useState4[0],
      setCancelLoading = _React$useState4[1];

  if (typeof confirmLoading !== 'undefined' && confirmLoading !== okLoading) {
    setOkloading(confirmLoading);
  }

  var endCls = (0, _classnames.default)(classname, actionModalCls, (0, _defineProperty2.default)({}, "".concat(actionModalPrefixCls, "-center"), contentCentered));
  var wrapperCls = (0, _classnames.default)(wrapClassName, (0, _defineProperty2.default)({}, "".concat(actionModalPrefixCls, "-centered"), centered));
  var innerSizeMap = {
    large: 480,
    medium: 400,
    small: 320
  };
  var curIcon = typeof icon !== 'undefined' ? icon : (0, _getTypedIcon.getCurIcon)(_icon.default, type, true);

  var handleOKCancelReturn = function handleOKCancelReturn(result, actionType) {
    var curSetHandler = actionType === 'ok' ? setOkloading : setCancelLoading;
    curSetHandler(true);
    Promise.resolve(result).then(function () {
      curSetHandler(false);
      closeModal === null || closeModal === void 0 ? void 0 : closeModal();
    }, function (e) {
      curSetHandler(false);
      console.error(e);
    });
  };

  var onModalClose = function onModalClose(e) {
    var cancelReturn = onCancel === null || onCancel === void 0 ? void 0 : onCancel(e);
    handleOKCancelReturn(cancelReturn, 'cancel');
  };

  var onModalOk = function onModalOk(e) {
    var okReturn = onOk === null || onOk === void 0 ? void 0 : onOk(e);
    handleOKCancelReturn(okReturn, 'ok');
  };

  var renderFooter = function renderFooter() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_button.default, Object.assign({
      loading: cancelLoading,
      onClick: onModalClose
    }, cancelButtonProps), cancelText || '取消'), /*#__PURE__*/_react.default.createElement(_button.default, Object.assign({
      loading: okLoading,
      type: "primary",
      onClick: onModalOk
    }, okButtonProps), okText || '确定'));
  };

  var endProps = (0, _objectSpread2.default)({
    prefixCls: actionModalPrefixCls,
    title: '',
    closable: false,
    closeIcon: closeIcon || defaultCloseIcon,
    onClose: onModalClose,
    transitionName: "".concat(actionModalPrefixCls, "-zoom"),
    maskTransitionName: "".concat(actionModalPrefixCls, "-fade"),
    className: endCls,
    footer: footer !== undefined ? footer : renderFooter(),
    wrapClassName: wrapperCls,
    width: width || innerSizeMap[size]
  }, reset);
  return /*#__PURE__*/_react.default.createElement(_components.RcDialog, Object.assign({}, endProps), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child")
  }, curIcon ? /*#__PURE__*/_react.default.createElement("span", {
    className: "".concat(actionModalPrefixCls, "-child-").concat(type, " ").concat(actionModalPrefixCls, "-child-icon")
  }, curIcon) : null, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-wrapper")
  }, title && /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-header")
  }, title), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-body")
  }, content))));
};

ActionModal.defaultProps = {
  size: 'medium',
  centered: true,
  cancelText: '取消'
};
var _default = ActionModal;
exports.default = _default;