/*
 * @Author: wyd
 * @Date: 2024-03
 * @LastEditors: wyd
 * @LastEditTime: 2024-03
 * @Description: 权限函数
 */
import { user } from '@/stores'
import { judementSameArr } from '@/utils'

/**
 * 单个权限验证
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反false
 */
export function auth(value: string): boolean {
  const stores = user()
  if (stores.permissions.length) {
    return stores.permissions.some((v: string) => v === value)
  }
  return true
}

/**
 * 多个权限验证，满足一个则为 true
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function auths(value: Array<string>): boolean {
  let flag = false
  const stores = user()
  stores.permissions.map((val: string) => {
    value.map((v: string) => {
      if (val === v) flag = true
    })
  })
  return flag
}

/**
 * 多个权限验证，全部满足则为 true
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function authAll(value: Array<string>): boolean {
  const stores = user()
  return judementSameArr(value, stores.permissions)
}
