import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { hydrateFromDisk, startPersistSubscription, flushPendingPersistSync } from "./lib/persist";
import { fetchHotkeyStatus } from "./lib/hotkey";

async function bootstrap(): Promise<void> {
  await hydrateFromDisk();
  startPersistSubscription();
  // Pull the hotkey registration result. Pull-style avoids the
  // listener-not-yet-registered race that an event-based approach would have.
  void fetchHotkeyStatus();

  // beforeunload handlers must be synchronous; we use a sync flush helper
  // that issues the write. The async save will race with window destruction
  // — whichever wins is fine, the write either lands or we retry next session.
  window.addEventListener("beforeunload", () => {
    flushPendingPersistSync();
  });

  ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
    <React.StrictMode>
      <App />
    </React.StrictMode>
  );
}

void bootstrap();
