import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import AircraftProfilePage from './AircraftProfilePage.tsx'
import SimBriefOFPControl from './SimBriefOFPControl.tsx'

function placeAircraftProfileInSidebar() {
  const launcher = document.querySelector<HTMLButtonElement>('.aircraft-profile-launcher')
  const navigation = document.querySelector<HTMLElement>('.sidebar nav')
  if (!launcher || !navigation || launcher.parentElement === navigation) return false

  launcher.classList.add('sidebar-aircraft-profile-link')
  launcher.innerHTML = '<span>▰</span>Perfil A320neo'
  navigation.appendChild(launcher)
  return true
}

const menuStyle = document.createElement('style')
menuStyle.textContent = `
.sidebar nav .aircraft-profile-launcher {
  position: static;
  left: auto;
  bottom: auto;
  z-index: auto;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  box-shadow: none;
}
.sidebar nav .aircraft-profile-launcher:hover {
  background: rgba(255,255,255,0.06);
}
`
document.head.appendChild(menuStyle)

const observer = new MutationObserver(() => {
  if (placeAircraftProfileInSidebar()) observer.disconnect()
})
observer.observe(document.body, { childList: true, subtree: true })

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
    <AircraftProfilePage />
    <SimBriefOFPControl />
  </StrictMode>,
)
