export const Message = {
    /**
     * 消息轻提示
     * @param title 消息内容
     */
    toast: (title: string, duration?: 'short' | 'long'): void => {
        if (title) {
            // #ifndef APP-PLUS
            uni.showToast({
                title,
                icon: 'none',
                duration: duration === 'short' ? 2000 : 3500,
            })
            // #endif

            // #ifdef APP-PLUS
            plus.nativeUI.toast(title, {
                duration: duration || 'short',
            })
            // #endif
        }
    },
    /**
     * 消息弹窗提示
     * @param content 消息内容
     * @param title 标题
     * @returns Promise<void>
     */
    alert: (content: string, title = '提示', confirmText = '确定'): Promise<void> => {
        return new Promise((resolve, reject) => {
            // #ifndef APP-PLUS
            uni.showModal({
                title,
                content,
                showCancel: false,
                confirmText,
                success: () => resolve(),
                fail: (err) => reject(err),
            })
            // #endif

            // #ifdef APP-PLUS
            plus.nativeUI.alert(content, resolve, title, confirmText)
            // #endif
        })
    },
    /**
     * 消息确认提示
     * @param content 提示内容
     * @param title 标题
     * @param confirmText 确认按钮文字
     * @param showCancel 是否显示取消按钮
     * @returns Promise<boolean> 是否点击了确认按钮
     */
    confirm: (content: string, title = '提示', confirmText = '确定', showCancel = true): Promise<boolean> => {
        return new Promise((resolve, reject) => {
            // #ifndef APP-PLUS
            uni.showModal({
                title,
                content,
                showCancel,
                confirmText,
                success: (res) => resolve(res.confirm),
                fail: (err) => reject(err),
            })
            // #endif

            // #ifdef APP-PLUS
            plus.nativeUI.confirm(content, ({ index }) => resolve(index === 0), {
                title,
                buttons: [confirmText, '取消'],
                verticalAlign: 'center',
            })
            // #endif
        })
    },
    /**
     * 加载中
     * @param title 加载中文字
     */
    loading: (title = '加载中...'): void => {
        uni.showLoading({
            title,
            mask: true,
        })
    },
    /**
     * 隐藏加载中
     */
    hideLoading: (): void => {
        uni.hideLoading()
    },
}
