import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App';
import { initAnalytics } from './platform/analytics';
import { useGameStore, flushGameState } from './store/useGameStore';

initAnalytics();

// 탭 종료/이탈 시 즉시 저장 (throttledStorage 보호)
window.addEventListener('beforeunload', flushGameState);
window.addEventListener('pagehide', flushGameState);

// 첫 진입 시 오프라인 보상 계산
useGameStore.getState().initOffline();

const rootEl = document.getElementById('root');
if (!rootEl) throw new Error('Root element #root not found in index.html');

createRoot(rootEl).render(
  <StrictMode>
    <App />
  </StrictMode>
);
