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 Icon from '../icon';
import Button from '../button';
import destroyFnSet from './destroyFns';
var CloseLine = Icon.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 = _objectWithoutProperties(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.useState(!!outerVisible),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

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

  var isDestroyAllRef = React.useRef(false);

  var _React$useContext = React.useContext(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.useEffect(function () {
    var destoryCurrentModal = function destoryCurrentModal() {
      setDestroyOnClose(true);
      setVisible(false);
      isDestroyAllRef.current = true;
    };

    destroyFnSet.add(destoryCurrentModal);
    return function () {
      destroyFnSet.delete(destoryCurrentModal);
    };
  }, []);

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

  var renderModal = function renderModal() {
    var _classNames;

    var defaultCloseIcon = /*#__PURE__*/React.createElement(CloseLine, null);
    var endCls = classNames(wrapClassName, (_classNames = {}, _defineProperty(_classNames, "".concat(modalPrefixCls, "-contentCentered"), !!contentCentered), _defineProperty(_classNames, "".concat(modalPrefixCls, "-noTitle"), !title), _defineProperty(_classNames, "".concat(modalPrefixCls, "-noCloseIcon"), !closable), _defineProperty(_classNames, "".concat(modalPrefixCls, "-noFooter"), footer === null), _defineProperty(_classNames, "".concat(modalPrefixCls, "-noBody"), !children), _defineProperty(_classNames, "".concat(modalPrefixCls, "-centered"), !!centered), _defineProperty(_classNames, "".concat(modalPrefixCls, "-expansion"), modalStyle === 'expansion'), _defineProperty(_classNames, "".concat(modalPrefixCls, "-maxHeight"), maxHeight), _classNames));

    var endProps = _objectSpread({
      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.createElement(RcDialog, Object.assign({}, endProps), children);
  };

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

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