import {state} from '@/store'

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value:string[]):boolean {
  if (value && value instanceof Array && Array.isArray(value) && value.length > 0) {
    const permissions:string[] =  state.permissions
    const permissionDatas:string[] = value
    const all_permission:string = "*:*:*"

    const hasPermission:boolean = permissions.some((permission:string) => {
      return (all_permission === permission || permissionDatas.includes(permission)) as boolean
    }) as boolean
    return hasPermission
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkRole(value:string[]):boolean {
  if (value && value instanceof Array && Array.isArray(value) && value.length > 0) {
    const roles:string[] = state.roles
    const permissionRoles:string[] = value
    const super_admin:string = "admin"

    const hasRole = roles.some(role => {
      return (super_admin === role || permissionRoles.includes(role)) as boolean
    }) as boolean
    return hasRole;
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}