import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * Confirm.jsx
 *
 * @Author: jruif
 * @Date: 2017/7/11 下午5:28
 */
import React, { isValidElement, cloneElement } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import Modal from './Modal';
import statusIcons from '../icons/statusIcons';
import Button from '../button';
/**
 * @ignore
 */

var ConfirmModal =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ConfirmModal, _React$Component);

  function ConfirmModal(props) {
    var _this;

    _classCallCheck(this, ConfirmModal);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ConfirmModal).call(this));

    _defineProperty(_assertThisInitialized(_this), "open", function (modalProps) {
      _this.setState({
        isOpen: true
      });

      if (modalProps) _this.update(modalProps);
    });

    _defineProperty(_assertThisInitialized(_this), "update", function (modalProps) {
      _this.setState({
        modalProps: modalProps
      });
    });

    _defineProperty(_assertThisInitialized(_this), "close", function () {
      _this.setState({
        isOpen: false,
        loading: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleCancel", function (e) {
      var modalProps = _this.state.modalProps;
      var onCancel = modalProps.onCancel;
      var close = false;

      if (onCancel) {
        close = onCancel(e);
      }

      if (!close) {
        _this.close();
      } else if (close.then) {
        close.then(_this.close).catch(function () {
          _this.setState({
            loading: false
          });
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleSure", function (e) {
      var modalProps = _this.state.modalProps;
      var onSure = modalProps.onSure;
      var close = false;

      if (onSure) {
        close = onSure(e);
      }

      if (!close) {
        _this.close();
      } else if (close.then) {
        _this.setState({
          loading: true
        });

        close.then(_this.close).catch(function () {
          _this.setState({
            loading: false
          });
        });
      }
    });

    _this.state = {
      isOpen: false,
      loading: false,
      modalProps: props.modalProps
    };
    return _this;
  }

  _createClass(ConfirmModal, [{
    key: "render",
    value: function render() {
      var prefixCls = this.props.prefixCls;
      var _this$state = this.state,
          isOpen = _this$state.isOpen,
          modalProps = _this$state.modalProps,
          loading = _this$state.loading;

      var size = modalProps.size,
          type = modalProps.type,
          actionsRender = modalProps.actionsRender,
          title = modalProps.title,
          content = modalProps.content,
          icon = modalProps.icon,
          children = modalProps.children,
          hideCloseButton = modalProps.hideCloseButton,
          hideBackdrop = modalProps.hideBackdrop,
          other = _objectWithoutProperties(modalProps, ["size", "type", "actionsRender", "title", "content", "icon", "children", "hideCloseButton", "hideBackdrop"]);

      var StatusIcon = statusIcons[type];
      return React.createElement(Modal, _extends({
        hideBackdrop: hideBackdrop,
        className: classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-").concat(type), type)),
        size: size,
        hideCloseButton: hideCloseButton,
        isOpen: isOpen,
        onClose: this.close
      }, other), React.createElement("div", {
        className: classNames("".concat(prefixCls, "-body"), _defineProperty({}, "".concat(prefixCls, "-body-full-width"), title))
      }, icon && isValidElement(icon) ? cloneElement(icon, {
        className: classNames("".concat(prefixCls, "-icon"), icon.props.className)
      }) : StatusIcon ? React.createElement(StatusIcon, {
        className: "".concat(prefixCls, "-icon"),
        size: "large"
      }) : null, React.createElement("div", {
        className: "".concat(prefixCls, "-container")
      }, title ? React.createElement("h6", {
        className: "".concat(prefixCls, "-title")
      }, title) : null, React.createElement("div", {
        className: "".concat(prefixCls, "-description")
      }, content || children))), React.createElement("div", {
        className: classNames("".concat(prefixCls, "-actions"))
      }, actionsRender ? actionsRender(this.close) : [type === 'confirm' ? React.createElement(Button, {
        onClick: this.handleCancel,
        size: size,
        key: "cancel"
      }, "\u53D6\u6D88") : null, React.createElement(Button, {
        color: "primary",
        loading: loading,
        variant: "contained",
        onClick: this.handleSure,
        key: "sure"
      }, "\u786E\u8BA4")]));
    }
  }]);

  return ConfirmModal;
}(React.Component);

_defineProperty(ConfirmModal, "defaultProps", {
  modalProps: {},
  prefixCls: 'ne-confirm'
});

var defaultProps = {
  disableBackdropClick: true,
  hideBackdrop: true,
  hideCloseButton: true,
  animationName: 'slide-down',
  type: 'confirm'
};
var confirm = {
  createConfirm: function createConfirm(callback) {
    var root = document.body;
    var containerElement = document.createElement('div'); // 不在DOM中创建节点

    root.appendChild(containerElement);
    var mounted = false;

    function getModal(modal) {
      if (mounted) {
        return;
      }

      mounted = true;
      callback && callback({
        element: modal,
        destroy: function destroy() {
          ReactDOM.unmountComponentAtNode(containerElement);
          containerElement.parentNode.removeChild(containerElement);
        }
      });
    }

    ReactDOM.render(React.createElement(ConfirmModal, _extends({}, defaultProps, {
      ref: getModal
    })), containerElement);
  },
  open: function open(itemProps) {
    var props = Object.assign({}, defaultProps, itemProps);

    if (this.confirm) {
      this.confirm.element.open(props);
    } else {
      this.createConfirm(function (confirmModal) {
        confirm.confirm = confirmModal;
        confirmModal.element.update(props);
        confirmModal.element.open();
      });
    }
  },
  config: function config(newConfig) {
    Object.assign(defaultProps, newConfig);
  },
  destroy: function destroy() {
    if (this.confirm) {
      this.confirm.destroy();
      delete this.confirm;
    }
  }
};
export default confirm;