import { createRoot } from 'react-dom/client';
import './theme/global.css';
import App from './App.jsx';
import { applyRestoreFromUrl } from './lib/migrate.js';

// Progress handed over from learnaiq.org lands in localStorage before React
// mounts, so App's state initialisers pick it up on the first render.
applyRestoreFromUrl();

// Arriving from the bridge is normally a full page load. If the link is opened
// in a tab that already has AIQ running, the hash changes without re-running
// this file — so reload and let the line above handle it properly.
window.addEventListener('hashchange', () => {
  if (/^#restore=/.test(location.hash)) location.reload();
});

createRoot(document.getElementById('root')).render(<App />);

// Offline shell. Registered after load so it never competes with first paint.
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/sw.js').catch(() => {});
  });
}
