import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import classNames from 'classnames';
import { RcDialog } from '../components';
import { ConfigContext } from '../config-provider/context';
import { getCurIcon } from '../utils/getTypedIcon';
import Icons from '../icon';
import Button from '../button';
var CloseLine = Icons.CloseLine;
var defaultCloseIcon = /*#__PURE__*/React.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 = _objectWithoutProperties(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.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

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

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

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

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

  var endCls = classNames(classname, actionModalCls, _defineProperty({}, "".concat(actionModalPrefixCls, "-center"), contentCentered));
  var wrapperCls = classNames(wrapClassName, _defineProperty({}, "".concat(actionModalPrefixCls, "-centered"), centered));
  var innerSizeMap = {
    large: 480,
    medium: 400,
    small: 320
  };
  var curIcon = typeof icon !== 'undefined' ? icon : getCurIcon(Icons, 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.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, Object.assign({
      loading: cancelLoading,
      onClick: onModalClose
    }, cancelButtonProps), cancelText || '取消'), /*#__PURE__*/React.createElement(Button, Object.assign({
      loading: okLoading,
      type: "primary",
      onClick: onModalOk
    }, okButtonProps), okText || '确定'));
  };

  var endProps = _objectSpread({
    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.createElement(RcDialog, Object.assign({}, endProps), /*#__PURE__*/React.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child")
  }, curIcon ? /*#__PURE__*/React.createElement("span", {
    className: "".concat(actionModalPrefixCls, "-child-").concat(type, " ").concat(actionModalPrefixCls, "-child-icon")
  }, curIcon) : null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-wrapper")
  }, title && /*#__PURE__*/React.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-header")
  }, title), /*#__PURE__*/React.createElement("div", {
    className: "".concat(actionModalPrefixCls, "-child-body")
  }, content))));
};

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