'use client';

import { useEffect, useRef, useState } from "react";

//#region src/hooks/useSearch.ts
const useSearch = (options) => {
	const { minChars = 3, debounceMs = 300, trim = true, defaultValue, onSearch, onClear } = options ?? {};
	const searchRef = useRef("");
	const timerRef = useRef(null);
	const [search, setDebouncedSearch] = useState(defaultValue ?? "");
	const clearTimer = () => {
		if (timerRef.current) {
			clearTimeout(timerRef.current);
			timerRef.current = null;
		}
	};
	const setSearch = (value) => {
		searchRef.current = value;
		const normalized = trim ? value.trim() : value;
		clearTimer();
		if (normalized.length < minChars) {
			setDebouncedSearch("");
			onClear?.();
			return;
		}
		timerRef.current = setTimeout(() => {
			const current = trim ? searchRef.current.trim() : searchRef.current;
			if (current.length >= minChars) {
				setDebouncedSearch(current);
				onSearch?.(current);
			} else {
				setDebouncedSearch("");
				onClear?.();
			}
		}, debounceMs);
	};
	useEffect(() => {
		return () => {
			clearTimer();
		};
	}, []);
	return {
		setSearch,
		search,
		hasMinChars: search.length >= minChars
	};
};

//#endregion
export { useSearch };
//# sourceMappingURL=useSearch.mjs.map