import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import { CartProvider } from './context/CartContext'; 
import './index.css'
import { initializeAPI } from './api.js' // <-- 1. Add this import line

// Only ONE render block and NO BrowserRouter here
// 2. Wrap the rendering sequence inside the .then() of your fallback check
initializeAPI().then(() => {
  ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
      <CartProvider>
        <App />
      </CartProvider>
    </React.StrictMode>
  )
})

// === ADDED FOR MOBILE INSTALLATION (PWA) ===
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/sw.js')
      .then(reg => console.log('UC Service Worker registered!', reg))
      .catch(err => console.error('Service Worker registration failed:', err));
  });
}