type Options<T> = {
	fetchUrl: (query: string) => Promise<T[]>;
	onSelect?: (item: T) => void;
	debounceMs?: number;
};

export function createTypeahead<T>(options: Options<T>) {
	const { fetchUrl, onSelect, debounceMs = 300 } = options;

	let query = $state('');
	let results: T[] = $state([]);
	let isOpen = $state(false);
	let loading = $state(false);
	let activeIndex = $state(-1);

	let debounceTimer: ReturnType<typeof setTimeout> | undefined;

	function setQuery(q: string) {
		query = q;
		isOpen = true;
		clearTimeout(debounceTimer);
		debounceTimer = setTimeout(async () => {
			loading = true;
			try {
				results = await fetchUrl(q);
			} catch (e) {
				console.error('typeahead fetch error', e);
				results = [];
			} finally {
				loading = false;
			}
		}, debounceMs);
	}

	function close() {
		isOpen = false;
		activeIndex = -1;
	}

	function setActiveIndex(idx: number) {
		activeIndex = idx;
	}

	function select(item: T) {
		onSelect?.(item);
		close();
	}

	/** Directly populate results without going through the debounce (e.g. on-focus preload). */
	function openWith(items: T[]) {
		results = items;
		isOpen = true;
	}

	return {
		get query() {
			return query;
		},
		get results() {
			return results;
		},
		get isOpen() {
			return isOpen;
		},
		get loading() {
			return loading;
		},
		get activeIndex() {
			return activeIndex;
		},
		setQuery,
		setActiveIndex,
		close,
		select,
		openWith
	};
}
