import { SvelteSet } from 'svelte/reactivity';

// Live accumulator. Selection persists across pagination and route changes
// within /documents and /enrich. Cleared on successful bulk save or via
// "Alles aufheben". The store is module-singleton — there is only ever one
// bulk-edit selection per browser session.
const selectedIds = new SvelteSet<string>();

export const bulkSelectionStore = {
	get ids(): SvelteSet<string> {
		return selectedIds;
	},
	get size(): number {
		return selectedIds.size;
	},
	has(id: string): boolean {
		return selectedIds.has(id);
	},
	toggle(id: string): void {
		if (selectedIds.has(id)) selectedIds.delete(id);
		else selectedIds.add(id);
	},
	add(id: string): void {
		selectedIds.add(id);
	},
	remove(id: string): void {
		selectedIds.delete(id);
	},
	setAll(ids: Iterable<string>): void {
		selectedIds.clear();
		for (const id of ids) selectedIds.add(id);
	},
	clear(): void {
		selectedIds.clear();
	}
};
