/**
 * 权限管理类
 */
class AuthManager {
  constructor() {
    this.permissions = uni.getStorageSync('userPermissions') || []
  }
  
  /**
   * 设置用户权限
   * @param {Array} permissions - 权限数组
   */
  setPermissions(permissions) {
    this.permissions = permissions
    uni.setStorageSync('userPermissions', permissions)
    console.log('权限已设置:', permissions)
  }

  /**
   * 清除权限
   */
  clearPermissions() {
    this.permissions = []
    uni.removeStorageSync('userPermissions')
  }

  /**
   * 检查权限
   * @param {string|Array} permission - 单个权限或权限数组
   * @returns {boolean}
   */
  check(permission) {
    if (!permission) return true
    
    // 如果是数组，检查是否有任一权限
    if (Array.isArray(permission)) {
      return permission.some(p => this.hasPermission(p))
    }
    
    // 单个权限检查
    return this.hasPermission(permission)
  }

  /**
   * 检查所有权限
   * @param {Array} permissions - 权限数组
   * @returns {boolean}
   */
  checkAll(permissions) {
    if (!Array.isArray(permissions)) return false
    return permissions.every(p => this.hasPermission(p))
  }

  /**
   * 内部权限检查方法
   * @param {string} permission 
   * @returns {boolean}
   */
  hasPermission(permission) {
    return this.permissions.includes(permission)
  }

  /**
   * 获取当前权限列表
   * @returns {Array}
   */
  getPermissions() {
    return [...this.permissions]
  }
}

// 创建单例实例
export const auth = new AuthManager()