// Exposes the per-game `GalaxyClient` instance through a Svelte
// context so command-driven UI (the order-tab submit button,
// later phases' inspector actions) can issue gateway calls without
// re-instantiating the client. The handle is intentionally a thin
// reactive wrapper: the layout populates `client` after the boot
// `Promise.all` resolves, and consumers read the latest value
// through the getter — `null` while the boot is in flight, set to
// the live client once the keypair / gateway public key are loaded.

import type { GalaxyClient } from "../api/galaxy-client";

/**
 * GALAXY_CLIENT_CONTEXT_KEY is the Svelte context key the in-game
 * shell layout uses to expose its bound `GalaxyClient` to
 * descendants. The order-tab submit button reads this to call
 * `submitOrder`.
 */
export const GALAXY_CLIENT_CONTEXT_KEY = Symbol("galaxy-client");

export interface GalaxyClientHandle {
	readonly client: GalaxyClient | null;
}

export class GalaxyClientHolder implements GalaxyClientHandle {
	#client: GalaxyClient | null = $state(null);

	get client(): GalaxyClient | null {
		return this.#client;
	}

	set(client: GalaxyClient | null): void {
		this.#client = client;
	}
}
