import { usePermission } from '@/utils/hooks/usePermission'
import { PropsWithChildren } from 'react'
import { Navigate } from 'react-router-dom'

type PermissionGuardProps = PropsWithChildren<{
  permissions?: string[]
}>

const PermissionGuard = (props: PermissionGuardProps) => {
  const { permissions = [], children } = props

  const { checkPermissions } = usePermission()
  const permissionsMatched = checkPermissions(permissions)

  return <>{permissionsMatched ? children : <Navigate to="/access-denied" />}</>
}

export default PermissionGuard
