import Vue from 'vue'
import Main from './main.vue'
let ModalConstructor = Vue.extend(Main)
let instance = null
var Modal = function(data) {
    if (typeof data !== 'object') {
        data = { msg: '' + data }
    }
    if (instance && instance.visible) {
        Object.keys(data).forEach(function(key) {
            instance[key] = data[key]
        })
        instance.restartTimer()
    } else {
        instance = new ModalConstructor({
            data: data
        })
        instance.vm = instance.$mount()
        document.body.appendChild(instance.vm.$el)
    }
    return instance.vm
};
['success', 'warning', 'info', 'loading'].forEach(type => {
    Modal[type] = data => {
        if (typeof data !== 'object') {
            data = { msg: '' + data }
        }
        data.type = type
        return Modal(data)
    }
})

Modal.close = function() {
    instance && instance.close()
}
export default Modal