'use strict';

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

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

exports["default"] = confirm;

var _react = require('react');

var React = _interopRequireWildcard(_react);

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

var ReactDOM = _interopRequireWildcard(_reactDom);

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

var _Modal2 = _interopRequireDefault(_Modal);

var _icon = require('../icon');

var _icon2 = _interopRequireDefault(_icon);

var _button = require('../button');

var _button2 = _interopRequireDefault(_button);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _objectAssign = require('object-assign');

var _objectAssign2 = _interopRequireDefault(_objectAssign);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }

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

var ActionButton = function (_React$Component) {
    (0, _inherits3["default"])(ActionButton, _React$Component);

    function ActionButton(props) {
        (0, _classCallCheck3["default"])(this, ActionButton);

        var _this = (0, _possibleConstructorReturn3["default"])(this, _React$Component.call(this, props));

        _this.onClick = function () {
            var _this$props = _this.props;
            var actionFn = _this$props.actionFn;
            var 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;
    }

    ActionButton.prototype.render = function render() {
        var _props = this.props;
        var type = _props.type;
        var children = _props.children;

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

    return ActionButton;
}(React.Component);

function confirm(config) {
    var _classNames;

    var props = (0, _objectAssign2["default"])({ iconType: 'question-circle' }, config);
    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 = ReactDOM.unmountComponentAtNode(div);
        if (unmountResult) {
            div.parentNode.removeChild(div);
        }
    }
    var body = React.createElement(
        'div',
        { className: 'ant-confirm-body' },
        React.createElement(_icon2["default"], { type: props.iconType }),
        React.createElement(
            'span',
            { className: 'ant-confirm-title' },
            props.title
        ),
        React.createElement(
            'div',
            { className: 'ant-confirm-content' },
            props.content
        )
    );
    var footer = null;
    if (props.okCancel) {
        footer = React.createElement(
            'div',
            { className: 'ant-confirm-btns' },
            React.createElement(
                ActionButton,
                { type: 'ghost', actionFn: props.onCancel, closeModal: close },
                props.cancelText
            ),
            React.createElement(
                ActionButton,
                { type: 'primary', actionFn: props.onOk, closeModal: close },
                props.okText
            )
        );
    } else {
        footer = React.createElement(
            'div',
            { className: 'ant-confirm-btns' },
            React.createElement(
                ActionButton,
                { type: 'primary', actionFn: props.onOk, closeModal: close },
                props.okText
            )
        );
    }
    var classString = (0, _classnames2["default"])((_classNames = {
        'ant-confirm': true
    }, (0, _defineProperty3["default"])(_classNames, 'ant-confirm-' + props.type, true), (0, _defineProperty3["default"])(_classNames, props.className, !!props.className), _classNames));
    ReactDOM.render(React.createElement(
        _Modal2["default"],
        { className: classString, visible: true, closable: false, title: '', transitionName: 'zoom', footer: '', maskTransitionName: 'fade', style: style, width: width },
        React.createElement(
            'div',
            { style: { zoom: 1, overflow: 'hidden' } },
            body,
            ' ',
            footer
        )
    ), div);
    return {
        destroy: close
    };
}
module.exports = exports['default'];