import { showNotify, NotifyType, showLoadingToast, showToast, showDialog, DialogOptions, ToastType } from 'vant'
import { timerInterceptor } from '@/utils/timer'
import plus from '@/utils/h5plus'

/**
 * 消息通知
 * @param message 
 * @param type 
 * @param duration 
 */
export function notify(message?: string, type?: NotifyType, duration = 2000) {
    plus.hideStatusBar()

    showNotify({ type, message, duration })

    timerInterceptor.debounce(() => {
        plus.showStatusBar()
    }, duration, 'notify')
}

/**
 * 显示加载状态
 * @param message 
 * @param callback 
 * @returns 
 */
export function showLoading(message = '加载中...', callback?: (hideLoading: (msg?: string, type?: ToastType) => void) => void,) {
    const toast = showLoadingToast({
        message,
        duration: 0,
        forbidClick: true,
    })
    callback && callback((msg, type) => {
        if (msg) {
            showToast({
                message: msg,
                type,
            })
        } else {
            toast.close()
        }
    })
    return toast
}

/**
 * 全屏加载状态
 * @param callback 
 * @param message 
 */
export function fullloading(callback: (hideLoading: (msg?: string, type?: ToastType) => void) => void, message = '提交中...') {
    return showLoading(message, callback)
}

/**
 * 模态框
 * @param message 
 * @param type 
 */
export function dialog(options: string | DialogOptions) {
    if (typeof options === 'string') {
        return showDialog({
            message: options,
        })
    }
    return showDialog(options)
}