import { getContext, setContext } from "svelte";
import { SvelteSet } from "svelte/reactivity";
/*
* The visitor's side of the page: what they've tried. One per page, shared through
* context — the visuals write to it (a round reviewed, a vouch given).
*/
export type BeatId = "pr" | "home" | "ci" | "vouch" | "agents" | "byo";
export interface FeedEvent {
id: number;
who: string;
text: string;
beat: BeatId;
t: number;
shield?: boolean;
q?: string;
sub?: string;
}
export class Guest {
/** What the visitor has done, newest first — for the activity a later section shows. */
feed = $state<FeedEvent[]>([]);
done = new SvelteSet<BeatId>();
#ids = 0;
#timers: ReturnType<typeof setTimeout>[] = [];
later(fn: () => void, ms: number) {
const t = setTimeout(() => {
this.#timers = this.#timers.filter((x) => x !== t);
fn();
}, ms);
this.#timers.push(t);
}
destroy() {
this.#timers.forEach(clearTimeout);
}
ev(beat: BeatId, who: string, text: string, o: Partial<FeedEvent> = {}, after = 0) {
this.later(() => {
this.feed.unshift({ who, text, beat, ...o, id: ++this.#ids, t: Date.now() });
}, after);
}
markDone(b: BeatId) {
this.done.add(b);
}
}
const KEY = Symbol("guest");
export const setGuest = (guest: Guest) => setContext(KEY, guest);
export const getGuest = () => getContext<Guest>(KEY);