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 _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["size", "type", "actionsRender", "title", "content", "icon", "children", "hideCloseButton", "hideBackdrop"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

/**
 * 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);

  var _super = _createSuper(ConfirmModal);

  function ConfirmModal(props) {
    var _this;

    _classCallCheck(this, ConfirmModal);

    _this = _super.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, _excluded);

      var StatusIcon = statusIcons[type];
      return /*#__PURE__*/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), /*#__PURE__*/React.createElement("div", {
        className: classNames("".concat(prefixCls, "-body"), _defineProperty({}, "".concat(prefixCls, "-body-full-width"), title))
      }, icon && /*#__PURE__*/isValidElement(icon) ? /*#__PURE__*/cloneElement(icon, {
        className: classNames("".concat(prefixCls, "-icon"), icon.props.className)
      }) : StatusIcon ? /*#__PURE__*/React.createElement(StatusIcon, {
        className: "".concat(prefixCls, "-icon"),
        size: "large"
      }) : null, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-container")
      }, title ? /*#__PURE__*/React.createElement("h6", {
        className: "".concat(prefixCls, "-title")
      }, title) : null, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-description")
      }, content || children))), /*#__PURE__*/React.createElement("div", {
        className: classNames("".concat(prefixCls, "-actions"))
      }, actionsRender ? actionsRender(this.close) : [type === 'confirm' ? /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleCancel,
        size: size,
        key: "cancel"
      }, "\u53D6\u6D88") : null, /*#__PURE__*/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( /*#__PURE__*/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;