import { useSyncExternalStore } from "react";

let raised = false;
const listeners = new Set<() => void>();

function subscribe(listener: () => void): () => void {
	listeners.add(listener);
	return () => listeners.delete(listener);
}

function getSnapshot(): boolean {
	return raised;
}

export function raiseRestartBanner(): void {
	raised = true;
	for (const listener of listeners) listener();
}

export function dismissRestartBanner(): void {
	raised = false;
	for (const listener of listeners) listener();
}

export function useRestartBanner(): boolean {
	return useSyncExternalStore(subscribe, getSnapshot);
}
