import _defineProperty from 'babel-runtime/helpers/defineProperty';

var _jsxFileName = 'src/modal/Confirm.jsx',
    _this = this;

/**
 * Confirm.jsx
 *
 * @Author: jruif
 * @Date: 2017/7/11 下午5:28
 */

import React from 'react';
import ReactDOM from 'react-dom';
import extend from 'object-assign';
import classNames from 'classnames';
import { isFunction } from '../util/isType';
import Modal, { defaultProps } from './Modal';
import ActionButton from './ActionButton';
import Icon from '../icon';
import omit from '../util/omit';

var noOp = function noOp() {};
// type有info,success,error,warning,confirm
// props 与 Modal.jsx 中属性大致一致
var Confirm = function Confirm(config) {
    var props = extend({}, defaultProps, {
        iconType: 'attention',
        isOpen: true,
        prefixCls: 'ne-confirm',
        size: 'sm'
    }, config);
    var prefixCls = props.prefixCls || 'ne-confirm';
    var rootNode = document.createElement('div');

    document.body.appendChild(rootNode);

    function _close() {
        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        setTimeout(function () {
            var unmountResult = ReactDOM.unmountComponentAtNode(rootNode);
            if (unmountResult && rootNode.parentNode) {
                rootNode.parentNode.removeChild(rootNode);
            }
            var triggerCancel = args && args.length && args.some(function (param) {
                return param && param.triggerCancel;
            });

            if (props.onClose && triggerCancel) {
                props.onClose.apply(props, args);
            }
        }, defaultProps.transitionDuration);
    }

    var footer = [React.createElement(
        ActionButton,
        {
            type: 'primary',
            onClick: props.onOk,
            autoFocus: true,
            key: prefixCls + '-ok',
            closeModal: function closeModal() {
                return runRender(false, _close);
            }, __source: {
                fileName: _jsxFileName,
                lineNumber: 53
            },
            __self: _this
        },
        props.okText
    )];

    if (props.onCancel) {
        footer.push(React.createElement(
            ActionButton,
            {
                hollow: true,
                onClick: props.onCancel,
                key: prefixCls + '-cancel',
                closeModal: function closeModal() {
                    return runRender(false, _close);
                }, __source: {
                    fileName: _jsxFileName,
                    lineNumber: 65
                },
                __self: _this
            },
            props.cancelText
        ));
    }

    function runRender(isOpen) {
        var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noOp;

        var classString = classNames(_defineProperty({}, prefixCls + '-' + props.type, true), props.className);
        ReactDOM.render(React.createElement(
            Modal,
            Object.assign({
                className: classString,
                mountNode: rootNode,
                onClose: function onClose(e) {
                    return _close({ triggerCancel: true });
                }
            }, omit(props, ['className', 'type', 'iconType', 'title', 'isOpen']), {
                isOpen: isOpen, __source: {
                    fileName: _jsxFileName,
                    lineNumber: 83
                },
                __self: this
            }),
            React.createElement(
                Modal.Content,
                {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 89
                    },
                    __self: this
                },
                props.title && React.createElement(
                    'div',
                    { className: prefixCls + '-icon ' + prefixCls + '-icon-' + props.type, __source: {
                            fileName: _jsxFileName,
                            lineNumber: 91
                        },
                        __self: this
                    },
                    React.createElement(Icon, { type: props.iconType, __source: {
                            fileName: _jsxFileName,
                            lineNumber: 92
                        },
                        __self: this
                    })
                ),
                React.createElement(
                    'div',
                    { className: prefixCls + '-content', __source: {
                            fileName: _jsxFileName,
                            lineNumber: 95
                        },
                        __self: this
                    },
                    props.title && React.createElement(
                        'span',
                        { className: prefixCls + '-content-title', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 96
                            },
                            __self: this
                        },
                        props.title
                    ),
                    React.createElement(
                        'div',
                        { className: prefixCls + '-content-desc', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 97
                            },
                            __self: this
                        },
                        isFunction(props.content) ? props.content() : props.content
                    )
                )
            ),
            React.createElement(
                Modal.Footer,
                {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 102
                    },
                    __self: this
                },
                footer
            )
        ), rootNode, function () {
            // 回调
            callback();
        });
    }

    runRender(props.isOpen);

    return {
        close: function close() {
            return runRender(false, _close);
        },
        open: function open() {
            return runRender(true);
        }
    };
};

export default Confirm;