/**
 * 图片平台适配器
 * 封装微信小程序图片加载和保存 API
 */

/**
 * 保存图片到相册
 */
export function saveImageToAlbum(filePath: string): Promise<void> {
    return new Promise((resolve, reject) => {
        wx.saveImageToPhotosAlbum({
            filePath,
            success: () => resolve(),
            fail: (err) => {
                if (
                    err.errMsg.includes('auth deny') ||
                    err.errMsg.includes('authorize')
                ) {
                    wx.showModal({
                        title: '提示',
                        content: '需要授权保存图片到相册',
                        confirmText: '去设置',
                        success: (res) => {
                            if (res.confirm) {
                                wx.openSetting();
                            }
                        },
                    });
                }
                reject(err);
            },
        });
    });
}

/**
 * 获取图片信息
 */
export function getImageInfo(
    src: string,
): Promise<WechatMiniprogram.GetImageInfoSuccessCallbackResult> {
    return new Promise((resolve, reject) => {
        wx.getImageInfo({
            src,
            success: resolve,
            fail: reject,
        });
    });
}
