import { useAuth } from '@hooks/useAuth'
import { NAV_ITEM } from '@routes/nav-route'
import { PathConstants } from '@routes/routes'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'

function getFirstItem(array) {
  const first = array.filter((item) => item.show)[0]

  if (first.children) {
    return getFirstItem(first.children)
  }
  return first
}

export default function Home() {
  const { t } = useTranslation()
  const { auth } = useAuth()
  const navigate = useNavigate()

  useEffect(() => {
    if (auth?.userInfo?.isAdmin) {
      const first = getFirstItem(NAV_ITEM(t, auth?.userInfo?.isAdmin))
      navigate(first ? first.pathName : PathConstants.PROFILE)
    }
  }, [auth.userInfo?.isAdmin])

  return <></>
}
