// utils/authorize.js
const SCOPE_MAP = {                      // 中文提示，可扩展
  'scope.record':'录音',
  'scope.userLocation': '位置信息'
}

/**
 * 一次性申请多个权限
 * @param {Array<string>} scopes  例：['scope.camera','scope.record']
 * @returns {Promise<Object>}  { ok: boolean, granted: Array, denied: Array }
 */
export function authorizeBatch(scopes) {
  return new Promise((resolve) => {
    const granted = [], denied = []
    let idx = 0
    //debugger
    // 单步授权
    function next() {
      if (idx >= scopes.length) {           // 队列走完
        resolve({ ok: denied.length === 0, granted, denied })
        return
      }
      const cur = scopes[idx++]
      wx.getSetting({
        success: ({ authSetting }) => {
          if (authSetting[cur]) {           // 已授权
            granted.push(cur)
            return next()
          }
          // 未授权 → 发起授权
          wx.authorize({
            scope: cur,
            success() {
              granted.push(cur)
              next()
            },
            fail() {                        // 用户拒绝
              denied.push(cur)
              next()
            }
          })
        },
        fail: () => {                       // 异常也算拒绝
          denied.push(cur)
          next()
        }
      })
    }

    next()  // 开始串行
  })
}
