import { persisted } from 'svelte-persisted-store';

const MAX_RECENT = 5;

export const recentSearches = persisted<string[]>('taskplaner-recent-searches', []);

export function addRecentSearch(query: string): void {
	if (!query || query.length < 2) return;

	recentSearches.update((searches) => {
		// Remove if already exists (will re-add at front)
		const filtered = searches.filter((s) => s.toLowerCase() !== query.toLowerCase());
		// Add to front, limit to MAX_RECENT
		return [query, ...filtered].slice(0, MAX_RECENT);
	});
}
