// Per-device "Datenmodus" — Saver loads compressed previews (default), Original loads
// the full file via the `/api/v1/upload/{id}/original` endpoint. That route is
// intentionally unauthenticated (the UUID acts as the capability) so it works from
// plain `<img src>` / `<video src>` without an Authorization header.
//
// Stored per-device in localStorage (not per-user) because data plans are a property
// of the device the guest is currently holding, not their identity.
//
// Used by:
// - Feed cards (FeedListCard / VirtualFeed grid tiles) to pick which URL to render
// - Lightbox
// - Diashow
// See [docs/FEATURES.md §2.5] for the user-facing model.

import { writable } from 'svelte/store';
import { browser } from '$app/environment';

export type DataMode = 'saver' | 'original';

const KEY = 'eventsnap_data_mode';
const DEFAULT: DataMode = 'saver';

function readInitial(): DataMode {
	if (!browser) return DEFAULT;
	const raw = localStorage.getItem(KEY);
	return raw === 'original' || raw === 'saver' ? raw : DEFAULT;
}

export const dataMode = writable<DataMode>(readInitial());

if (browser) {
	dataMode.subscribe((value) => {
		try {
			localStorage.setItem(KEY, value);
		} catch {
			// localStorage may be unavailable (Safari private mode); ignore.
		}
	});
}

/**
 * Build the URL for a feed upload given the current data mode and the URL variants
 * the backend returned. Centralised so every consumer (cards, lightbox, diashow)
 * follows the same fallback rule:
 *   Original mode → original API route. Falls back to preview if no upload id is
 *                   available (defensive — shouldn't happen in practice).
 *   Saver mode    → preview URL (compressed), falling back to thumbnail and then
 *                   original.
 */
export function pickMediaUrl(
	mode: DataMode,
	upload: { id: string; preview_url: string | null; thumbnail_url: string | null }
): string {
	if (mode === 'original') {
		return `/api/v1/upload/${upload.id}/original`;
	}
	return upload.preview_url ?? upload.thumbnail_url ?? `/api/v1/upload/${upload.id}/original`;
}
