import type { ReactNode } from 'react'
import { usePermission } from '@/hooks/usePermission'
import type { CrudAction } from '@/lib/menuKeys'

type Props = {
  menuKey: string
  action?: CrudAction
  fallback?: ReactNode
  children: ReactNode
}

export function PermissionGuard({ menuKey, action = 'Read', fallback = null, children }: Props) {
  const { can } = usePermission()
  if (!can(menuKey, action)) return <>{fallback}</>
  return <>{children}</>
}
