import { writable } from 'svelte/store';
import { getTotalMessageCount } from '$lib/firebase/messages.js';

// Shared, app-wide value for the "memory counter" pill (GlobalCountPill.svelte) -
// the live total from meta/stats (see messages.js for the increment side).
// Starts at `null` (not yet loaded) rather than 0, so the pill can wait to
// render until the first real value arrives instead of flashing
// "0 messages have been left worldwide" before the fetch resolves.
export const globalCount = writable(null);

// Re-fetches the counter from Firestore and updates the shared store. Called
// once on initial load (GlobalCountPill's onMount) and again after this
// device successfully posts a message (ComposeSheet), so the number "feels
// alive" without needing a full page reload.
export async function refreshGlobalCount() {
    globalCount.set(await getTotalMessageCount());
}
