import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App.jsx'
import './styles.css'

if ('scrollRestoration' in window.history) {
  window.history.scrollRestoration = 'manual'
}

const navigationEntry = window.performance.getEntriesByType('navigation')[0]

if (navigationEntry?.type === 'reload' && !window.location.hash) {
  const resetScroll = () => window.scrollTo({ top: 0, left: 0, behavior: 'auto' })

  window.addEventListener(
    'pageshow',
    () => {
      resetScroll()
      window.requestAnimationFrame(resetScroll)
      window.setTimeout(resetScroll, 120)
    },
    { once: true },
  )
}

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </StrictMode>,
)
