// global.js
import util from './utils.js'
import time from './timeUtils.js'
import authorize from './authorize.js'
import md5 from './lib/md5.js'
import Oss from '@/utils/oss-upload/oss/index.js'
import { handleTree } from './tree.js'

// #ifdef H5
// import jWeixin from './lib/jweixin-module.js'
// import { wxPay } from './pay.js'
// #endif

export default function setupGlobalProperties(app) {
  // #ifdef H5
  // app.config.globalProperties.$jWeixin = jWeixin
  // app.config.globalProperties.$wxPay = wxPay
  // #endif

  app.config.globalProperties.$handleTree = handleTree
  app.config.globalProperties.$Oss = Oss
  app.config.globalProperties.$authorize = authorize
  app.config.globalProperties.$timeUtils = time
  app.config.globalProperties.$utils = util
  app.config.globalProperties.$md5 = md5

  app.config.globalProperties.previewImage = (urls, current) => {
    uni.previewImage({
      urls,
      current,
    })
  }
  app.config.globalProperties.openLocation = (latitude, longitude) => {
    if (!latitude || !longitude) return
    uni.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
    })
  }

  // 封装微信支付
  uni.requestPaymentWxPay = function (res) {
    return new Promise((success, error) => {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: res.result.timeStamp,
        nonceStr: res.result.nonceStr,
        package: res.result.packageValue,
        signType: res.result.signType,
        paySign: res.result.paySign,
        success: function (res) {
          console.log('支付成功', res)
          success(res)
        },
        fail: function (err) {
          console.log('支付失败', err)
          error(err)
          uni.showToast({
            icon: 'none',
            title: '支付失败'
          })
        }
      })
    })
  }
}