/**
 * Single-photo preview overlay state. The lightbox sits in +layout.svelte
 * and listens to this store; any view (timeline, duplicates view, heaps)
 * can `open(uid)` to pop it. The order array is mirrored from whatever
 * list the user is currently looking at so prev/next stay in context.
 */
export const preview = $state<{
	uid: string | null;
	order: string[];
}>({
	uid: null,
	order: []
});

export function openPreview(uid: string, order?: string[]): void {
	if (order) preview.order = order;
	preview.uid = uid;
}

export function closePreview(): void {
	preview.uid = null;
}

export function previewNext(): void {
	if (!preview.uid) return;
	const i = preview.order.indexOf(preview.uid);
	if (i < 0 || i >= preview.order.length - 1) return;
	preview.uid = preview.order[i + 1];
}

export function previewPrev(): void {
	if (!preview.uid) return;
	const i = preview.order.indexOf(preview.uid);
	if (i <= 0) return;
	preview.uid = preview.order[i - 1];
}
