import { useAuthStore } from '@/stores/authStore'

/**
 * Returns true if the authenticated user has at least one of the given permission codes.
 * OR semantics when passing an array — at least one match suffices.
 * Returns false when the user is not authenticated or has no matching permission.
 */
export function usePermission(code: string | string[]): boolean {
  // Select the user directly to avoid creating a new array reference on every render
  // when user is null (which would cause infinite re-renders in React 19).
  const user = useAuthStore((s) => s.user)
  const permisos = user?.permisos ?? []
  const wanted = Array.isArray(code) ? code : [code]
  return wanted.some((c) => permisos.includes(c))
}
