import { useUserStore } from '@/store/user'

/**
 * 接口/业务权限校验（非页面权限）
 * @param {string[]} value 如 ['system:user:add']
 */
export function checkPermi(value) {
  if (value && Array.isArray(value) && value.length > 0) {
    const { state } = useUserStore()
    const permissions = state.permissions || []
    const all = '*:*:*'
    return permissions.some(
      (p) => all === p || value.includes(p)
    )
  }
  console.error("need permissions! Like checkPermi(['system:user:add'])")
  return false
}

/**
 * 角色权限校验
 * @param {string[]} value 如 ['admin']
 */
export function checkRole(value) {
  if (value && Array.isArray(value) && value.length > 0) {
    const { state } = useUserStore()
    const roles = state.roles || []
    const superAdmin = 'admin'
    return roles.some((r) => superAdmin === r || value.includes(r))
  }
  console.error("need roles! Like checkRole(['admin'])")
  return false
}
