import { PayStatusEnum } from '@/enums/appEnums'
import { handleClientEvent } from '../client'
//#ifdef H5
import wechatOa from '../wechat'
//#endif
export class Wechat {
    init(name: string, pay: any) {
        pay[name] = this
    }

    async run(options: any) {
        try {
            //@ts-ignore
            const res = await handleClientEvent({
                MP_WEIXIN: () => {
                    return new Promise((resolve) => {
                        uni.requestPayment({
                            provider: 'wxpay',
                            timeStamp: options?.timeStamp,
                            nonceStr: options?.nonceStr,
                            package: options?.packageValue,
                            signType: options?.signType,
                            paySign: options?.paySign,
                            success: () => {
                                resolve(PayStatusEnum.SUCCESS)
                            },
                            fail: () => {
                                resolve(PayStatusEnum.FAIL)
                            }
                        })
                    })
                },
                OA_WEIXIN: () => {
                    return new Promise((resolve) => {
                        wechatOa
                            .pay(options)
                            .then(() => {
								console.log('success')
                                resolve(PayStatusEnum.SUCCESS)
                            })
                            .catch(() => {
								console.log('fail')
                                resolve(PayStatusEnum.FAIL)
                            })
                    })
                },
                H5: () => {
                    return new Promise((resolve) => {
                        window.location.replace(options)
                        resolve(PayStatusEnum.PENDING)
                    })
                }
            })
            return res
        } catch (error) {
            return Promise.reject(error)
        }
    }
}
