"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 _icon = _interopRequireDefault(require("../icon"));

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

var _destroyFns = _interopRequireDefault(require("./destroyFns"));

var CloseLine = _icon.default.CloseLine;

var Modal = function Modal(props) {
  var children = props.children,
      customPrefixCls = props.prefixCls,
      closeIcon = props.closeIcon,
      footer = props.footer,
      title = props.title,
      contentCentered = props.contentCentered,
      centered = props.centered,
      wrapClassName = props.wrapClassName,
      cancelButtonProps = props.cancelButtonProps,
      okButtonProps = props.okButtonProps,
      cancelText = props.cancelText,
      okText = props.okText,
      onCancel = props.onCancel,
      onOk = props.onOk,
      getContainer = props.getContainer,
      width = props.width,
      size = props.size,
      modalStyle = props.modalStyle,
      confirmLoading = props.confirmLoading,
      maxHeight = props.maxHeight,
      closable = props.closable,
      outerVisible = props.visible,
      outerDestroyOnClose = props.destroyOnClose,
      resetProps = (0, _objectWithoutProperties2.default)(props, ["children", "prefixCls", "closeIcon", "footer", "title", "contentCentered", "centered", "wrapClassName", "cancelButtonProps", "okButtonProps", "cancelText", "okText", "onCancel", "onOk", "getContainer", "width", "size", "modalStyle", "confirmLoading", "maxHeight", "closable", "visible", "destroyOnClose"]);

  var _React$useState = _react.default.useState(!!outerVisible),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

  var _React$useState3 = _react.default.useState(!!outerDestroyOnClose),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      destroyOnClose = _React$useState4[0],
      setDestroyOnClose = _React$useState4[1];

  var isDestroyAllRef = _react.default.useRef(false);

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

  var modalPrefixCls = getPrefixClassName('modal', customPrefixCls);
  var innerSizeMap = {
    large: 480,
    medium: 400,
    small: 320
  };

  if (typeof outerVisible !== 'undefined' && outerVisible !== visible && !isDestroyAllRef.current) {
    setVisible(outerVisible);
  }

  if (typeof outerDestroyOnClose !== 'undefined' && outerDestroyOnClose !== destroyOnClose && !isDestroyAllRef.current) {
    setDestroyOnClose(outerDestroyOnClose);
  }

  isDestroyAllRef.current = false;

  var onModalClose = function onModalClose(e) {
    onCancel === null || onCancel === void 0 ? void 0 : onCancel(e);
  };

  var onModalOk = function onModalOk(e) {
    onOk === null || onOk === void 0 ? void 0 : onOk(e);
  };

  _react.default.useEffect(function () {
    var destoryCurrentModal = function destoryCurrentModal() {
      setDestroyOnClose(true);
      setVisible(false);
      isDestroyAllRef.current = true;
    };

    _destroyFns.default.add(destoryCurrentModal);

    return function () {
      _destroyFns.default.delete(destoryCurrentModal);
    };
  }, []);

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

  var renderModal = function renderModal() {
    var _classNames;

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

    var endCls = (0, _classnames.default)(wrapClassName, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-contentCentered"), !!contentCentered), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-noTitle"), !title), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-noCloseIcon"), !closable), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-noFooter"), footer === null), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-noBody"), !children), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-centered"), !!centered), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-expansion"), modalStyle === 'expansion'), (0, _defineProperty2.default)(_classNames, "".concat(modalPrefixCls, "-maxHeight"), maxHeight), _classNames));
    var endProps = (0, _objectSpread2.default)({
      prefixCls: modalPrefixCls,
      closeIcon: closeIcon || defaultCloseIcon,
      footer: footer !== undefined ? footer : renderFooter(),
      onClose: onModalClose,
      transitionName: "".concat(modalPrefixCls, "-zoom"),
      maskTransitionName: "".concat(modalPrefixCls, "-fade"),
      wrapClassName: endCls,
      getContainer: getContainer || getPopupContainer,
      title: title,
      closable: closable,
      visible: visible,
      destroyOnClose: destroyOnClose,
      width: width || innerSizeMap[size]
    }, resetProps);
    return /*#__PURE__*/_react.default.createElement(_components.RcDialog, Object.assign({}, endProps), children);
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderModal());
};

Modal.defaultProps = {
  cancelText: '取消',
  centered: true,
  closable: true,
  destroyOnClose: false,
  size: 'medium',
  zIndex: 1000,
  modalStyle: 'default',
  maxHeight: false
};
var _default = Modal;
exports.default = _default;