import { writable } from 'svelte/store';

/**
 * Whether the current page has enough data on screen to be worth revealing.
 *
 * The weather pages fetch their forecast *after* the route swap, so a plain
 * navigation transition would cross-fade one skeleton into another and then cut
 * hard to the real content. The layout holds its transition open on this flag
 * instead, so the fade lines up with the page actually being loaded.
 */
export const pageContentReady = writable(true);

/**
 * Called by the layout before it swaps to a route that fetches its own data.
 *
 * The layout owns the "not ready yet" side deliberately: the incoming page's
 * effects have not necessarily run at that point (rendering is paused inside a
 * view transition), so a page that cleared the flag itself would sometimes be
 * announced as ready before it had fetched anything.
 */
export function markPageLoading(): void {
	pageContentReady.set(false);
}

/**
 * Declare a page's readiness. Pass a getter for "my data has arrived". Only
 * ever sets the flag - clearing it is the layout's job (see above).
 */
export function reportPageReady(isReady: () => boolean): void {
	$effect(() => {
		if (isReady()) pageContentReady.set(true);
	});
}
