'use client'

import type { ReactNode } from 'react'
import { useAuth } from '@/contexts/AuthContext'
import { hasAnyPermission, hasPermission } from '@/lib/permissions'

export function PermissionGate({
  permission,
  anyOf,
  children,
  fallback = null,
}: {
  permission?: string
  anyOf?: string[]
  children: ReactNode
  fallback?: ReactNode
}) {
  const { user } = useAuth()
  const ok = permission
    ? hasPermission(user, permission)
    : anyOf
      ? hasAnyPermission(user, anyOf)
      : true
  return <>{ok ? children : fallback}</>
}
