import { writable } from 'svelte/store';

// ── Fragment quote toast ──────────────────────────────────────────────────────
// Shows a short sentence in the bottom-right when a fragment is collected.
// Auto-clears after 4 seconds; cancels any previous timer so rapid collection
// always shows the newest quote cleanly.

export const fragmentQuote = writable(null); // { text, color, key } | null

let toastTimer = null;
let quoteKey   = 0;

export function showFragmentQuote(text, color) {
    if (!text) return;
    if (toastTimer) clearTimeout(toastTimer);
    quoteKey++;
    fragmentQuote.set({ text, color, key: quoteKey });
    toastTimer = setTimeout(() => {
        fragmentQuote.set(null);
        toastTimer = null;
    }, 4200);
}

// ── Level-complete overlay ────────────────────────────────────────────────────
// Shows the color-psychology takeaway before the level-select redirect.

export const completeQuoteData = writable(null); // { text, color } | null

export function showCompleteQuote(text, color, nextDest) {
    if (!text) return;
    completeQuoteData.set({ text, color, nextDest });
}

export function clearCompleteQuote() {
    completeQuoteData.set(null);
}
