import "./app.css";
import "@xterm/xterm/css/xterm.css";
import App from "./App.svelte";
import { mount } from "svelte";
import { Electroview } from "electrobun/view";
import type { PtyRPCSchema } from "../shared/pty-rpc-schema.ts";
import { setAppRpc } from "./rpc.ts";

/**
 * Set up Electroview RPC.
 *
 * The schema is split from the Bun side's perspective:
 *   - "requests"  in PtyRPCSchema = what WE (WebView) call on Bun → these become
 *                 methods on electrobun.rpc.request.*
 *   - "messages"  in PtyRPCSchema = what BUN pushes to us → we listen via
 *                 electrobun.rpc.addMessageListener(name, handler)
 *
 * Electroview.defineRPC takes the schema where handlers.requests = what the
 * WebView handles (i.e., requests FROM Bun to us). Since Bun never calls us
 * with requests (only messages), that section is empty.
 */
const rpc = Electroview.defineRPC<PtyRPCSchema>({
  maxRequestTime: 10_000,
  handlers: {
    requests: {
      // No request handlers needed — Bun only pushes messages to us, not requests.
    },
    messages: {
      // These are messages that WE send to Bun (fire-and-forget).
      // Empty: WebView doesn't initiate any fire-and-forget messages.
    },
  },
});

// Register the RPC singleton so all modules can import from rpc.ts
setAppRpc(rpc);

export const electrobun = new Electroview({ rpc });

/** @deprecated Import from './rpc.ts' instead. */
export { rpc as appRpc };

const app = mount(App, {
  target: document.getElementById("app")!,
});

export default app;
