// Onboarding-guide "already seen" state.
//
// Extracted from OnboardingGuide.svelte so the AUTH FLOWS can mark it too. The guide is a
// first-run explainer, and the only thing that used to suppress it was this localStorage key
// being present on the device — which meant a returning guest who recovered their account
// (new phone, cleared site data, second browser) was shown the whole guide again as if they
// had never used the app.
//
// `/join` cannot distinguish them: its `is_new` field is hardcoded `true` in the backend,
// because a name that already exists 409s before that response is built. So `/recover` — both
// the dedicated page and the inline "name taken, enter your PIN" path on /join — is the
// authoritative "this person already has an account" signal, and it marks the guide seen.

import { browser } from '$app/environment';

const GUIDE_SEEN_KEY = 'eventsnap_guide_seen';

/** Has this device already been shown (and dismissed) the onboarding guide? */
export function hasSeenGuide(): boolean {
	if (!browser) return false;
	try {
		return localStorage.getItem(GUIDE_SEEN_KEY) !== null;
	} catch {
		// localStorage can throw in Safari private mode. Treating that as "not seen" would
		// re-show the guide on every navigation, so fail the quiet way instead.
		return true;
	}
}

/**
 * Suppress the guide from now on.
 *
 * Called when the user dismisses it, and when they authenticate as an EXISTING user via
 * recover — someone who already has an account has already been through onboarding, and
 * re-running it on a replacement device is the bug this exists to prevent.
 */
export function markGuideSeen(): void {
	if (!browser) return;
	try {
		localStorage.setItem(GUIDE_SEEN_KEY, '1');
	} catch {
		// Private mode / quota. Nothing to do: the guide is cosmetic, and the worst case is
		// that it shows again on this device.
	}
}
