/**
 * index.js
 *
 * @Author: jruif
 * @Date: 2017/7/10 下午10:52
 */

import Modal from './Modal';
import ModalHeader from './ModalHeader';
import ModalFooter from './ModalFooter';
import ModalContent from './ModalContent';
import Confirm from './Confirm';
import extend from 'deep-extend';
import { isString, isObject } from '../util/isType';
/*
 * 可以通过 Modal.Footer 来调用，例如：
 * <Modal.Footer>
 *     <Button>确定</Button>
 * </Modal.Footer>
 * */
Modal.Header = ModalHeader;
Modal.Content = ModalContent;
Modal.Footer = ModalFooter;

var noOp = function noOp() {};
export var iconTypeObj = {
    info: 'info',
    error: 'cancel-o',
    warning: 'warn',
    success: 'ok-o'
};
var handleConfig = function handleConfig(config) {
    var defaultConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    if (isString(config)) {
        return extend(defaultConfig, {
            content: config
        });
    } else if (isObject(config)) {
        return extend(defaultConfig, config);
    }
    return defaultConfig;
};
/*
* config 接受的参数：
* content: 提示内容
* okText：确定按钮内容
* cancelText：取消按钮内容，默认Modal.confirm类型拥有
* onOk：确定按钮回调事件，关闭弹窗条件：返回undefined、true、promise的fulfilled(Promise.resolve())状态
* onCancel：取消按钮回调，默认不做任何处理，直接关闭弹窗，返回同onOk
* type: 提示类型，
* iconType：显示的icon，类型同Icon，
* title：标题,
* 其他参数同Modal
* */
Modal.info = function (config) {
    return Confirm(handleConfig(config, {
        type: 'info',
        iconType: iconTypeObj.info,
        title: '提示',
        content: '',
        onOk: noOp
    }));
};

Modal.success = function (config) {
    return Confirm(handleConfig(config, {
        type: 'success',
        iconType: iconTypeObj.success,
        title: '已成功',
        content: '',
        onOk: noOp
    }));
};

Modal.error = function (config) {
    return Confirm(handleConfig(config, {
        type: 'error',
        iconType: iconTypeObj.error,
        title: '发生错误',
        content: '',
        onOk: config.onClose
    }));
};

Modal.warning = function (config) {
    return Confirm(handleConfig(config, {
        type: 'warning',
        iconType: iconTypeObj.warning,
        title: '警告',
        content: '',
        onOk: noOp
    }));
};

Modal.confirm = function (config) {
    return Confirm(handleConfig(config, {
        type: 'warning',
        iconType: iconTypeObj.warning,
        title: '请确认',
        content: '',
        onOk: noOp,
        onCancel: function onCancel() {}
    }));
};

export default Modal;