'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.default = confirm;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _icon = require('mxa/lib/icon');

var _icon2 = _interopRequireDefault(_icon);

var _button = require('mxa/lib/button');

var _button2 = _interopRequireDefault(_button);

var _Modal = require('./Modal');

var _Modal2 = _interopRequireDefault(_Modal);

var _locale = require('./locale');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/* eslint-disabled */
var ActionButton = function (_React$Component) {
  _inherits(ActionButton, _React$Component);

  function ActionButton(props) {
    _classCallCheck(this, ActionButton);

    var _this = _possibleConstructorReturn(this, (ActionButton.__proto__ || Object.getPrototypeOf(ActionButton)).call(this, props));

    _this.onClick = function () {
      var _this$props = _this.props,
          actionFn = _this$props.actionFn,
          closeModal = _this$props.closeModal;

      if (actionFn) {
        var ret = void 0;
        if (actionFn.length) {
          ret = actionFn(closeModal);
        } else {
          ret = actionFn();
          if (!ret) {
            closeModal();
          }
        }
        if (ret && ret.then) {
          _this.setState({ loading: true });
          ret.then(function () {
            // It's unnecessary to set loading=false, for the Modal will be unmounted after close.
            // this.setState({ loading: false });
            closeModal.apply(undefined, arguments);
          });
        }
      } else {
        closeModal();
      }
    };

    _this.state = {
      loading: false
    };
    return _this;
  }
  /* eslint-disable */


  _createClass(ActionButton, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      if (this.props.autoFocus) {
        (function () {
          var $this = _reactDom2.default.findDOMNode(_this2);
          _this2.timeoutId = setTimeout(function () {
            return $this.focus();
          });
        })();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      clearTimeout(this.timeoutId);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          type = _props.type,
          children = _props.children;

      var loading = this.state.loading;
      return _react2.default.createElement(
        _button2.default,
        { type: type, size: 'large', onClick: this.onClick, loading: loading },
        children
      );
    }
  }]);

  return ActionButton;
}(_react2.default.Component);

function confirm(config) {
  var _classNames;

  var props = Object.assign({ iconType: 'question-circle' }, config);
  var prefixCls = props.prefixCls || 'mx-confirm-self';
  var div = document.createElement('div');
  document.body.appendChild(div);

  var width = props.width || 416;
  var style = props.style || {};

  // 默认为 true，保持向下兼容
  if (!('okCancel' in props)) {
    props.okCancel = true;
  }

  var runtimeLocale = (0, _locale.getConfirmLocale)();

  props.okText = props.okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
  props.cancelText = props.cancelText || runtimeLocale.cancelText;

  function close() {
    var unmountResult = _reactDom2.default.unmountComponentAtNode(div);
    if (unmountResult) {
      div.parentNode.removeChild(div);
    }
  }

  var body = _react2.default.createElement(
    'div',
    { className: prefixCls + '-body' },
    _react2.default.createElement(
      'div',
      { className: prefixCls + '-title-bg' },
      _react2.default.createElement(
        'span',
        { className: prefixCls + '-title' },
        props.title
      )
    ),
    _react2.default.createElement(
      'div',
      { className: prefixCls + '-content' },
      props.content
    )
  );

  var footer = null;
  if (props.okCancel) {
    footer = _react2.default.createElement(
      'div',
      { className: prefixCls + '-btns' },
      _react2.default.createElement(
        ActionButton,
        { type: 'primary', actionFn: props.onOk, closeModal: close, autoFocus: true },
        props.okText
      ),
      _react2.default.createElement(
        ActionButton,
        { type: 'ghost', actionFn: props.onCancel, closeModal: close },
        props.cancelText
      )
    );
  } else {
    footer = _react2.default.createElement(
      'div',
      { className: prefixCls + '-btns' },
      _react2.default.createElement(
        ActionButton,
        { type: 'primary', actionFn: props.onOk, closeModal: close, autoFocus: true },
        props.okText
      )
    );
  }

  var classString = (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, prefixCls + '-' + props.type, true), _defineProperty(_classNames, props.className, !!props.className), _classNames));

  _reactDom2.default.render(_react2.default.createElement(
    _Modal2.default,
    {
      className: classString,
      onCancel: close,
      visible: true,
      title: '',
      transitionName: 'zoom',
      footer: '',
      maskTransitionName: 'fade',
      maskClosable: false,
      style: style,
      width: width
    },
    _react2.default.createElement(
      'div',
      { className: prefixCls + '-body-wrapper' },
      body,
      ' ',
      footer
    )
  ), div);

  return {
    destroy: close
  };
}