import Toast from './Toast'
const ToastComponent = {
    install(Vue) {
        /**
         * 提示框组件
         */
        const toastConstructor = Vue.extend(Toast)
        const toastInstance = new toastConstructor()
        toastInstance.$mount(document.createElement('div'))
        document.body.appendChild(toastInstance.$el)

        /**
         * 显示信息提示框
         * @param {String} msg 
         * @param {String} type 
         * @param {Number} duration 
         */

        Vue.prototype.$toast = function (msg, type = 'tips', duration = 1500) {
            return new Promise(resolve => {
                toastInstance.duration = duration
                toastInstance.msg = msg
                toastInstance.show = true
                toastInstance.type = type
                setTimeout(() => {
                    toastInstance.duration = 0
                    toastInstance.show = false
                    resolve()
                }, duration)
            })
        }

        /**
         * 打开加载状态
         * @param {String} msg 
         */
        Vue.prototype.$showLoading = function (msg = '加载中') {
            toastInstance.duration = 0
            toastInstance.msg = msg
            toastInstance.show = true
            toastInstance.type = 'loading'
        }
        /**
         * 关闭加载状态
         */
        Vue.prototype.$hideLoading = function () {
            if (toastInstance.duration === 0) {
                toastInstance.show = false
            }
        }
    }
}
export default ToastComponent