import { Notify, NotifyType, Toast, Dialog, DialogOptions } 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()

    Notify({ type, message, duration })

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

/**
 * 显示加载状态
 * @param message 
 * @param callback 
 * @returns 
 */
export function showLoading(message = '正在加载...', callback?: (hideLoading: () => void) => void,) {
    const toast = Toast.loading({
        message,
        duration: 0,
        forbidClick: true,
    })
    callback && callback(toast.clear)
    return toast
}

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

/**
 * 模态框
 * @param message 
 * @param type 
 */
export function dialog<T = unknown>(message: string, type?: DialogOptions) {
    return Dialog({
        message,
        ...type
    }) as Promise<T>
}