import { useCallback, useState } from "react";
const normalize = (value) => value.trim();
export function useSearchCommon({ initialValue, onPersistSearchValue, onTrackSearch, }) {
    const [displayedValue, setDisplayedValue] = useState(initialValue);
    const handleSearch = useCallback((queryOrEvent) => {
        const nextValue = typeof queryOrEvent === "string" ? queryOrEvent : queryOrEvent.target.value;
        setDisplayedValue(nextValue);
    }, []);
    const shouldEmitEvent = useCallback((currentQuery, previousQuery) => currentQuery !== "" && currentQuery !== previousQuery, []);
    const handleDebouncedChange = useCallback((current, previous) => {
        const currentQuery = normalize(current);
        const previousQuery = normalize(previous);
        onPersistSearchValue?.(currentQuery);
        if (!shouldEmitEvent(currentQuery, previousQuery))
            return;
        onTrackSearch?.(currentQuery);
    }, [onPersistSearchValue, onTrackSearch, shouldEmitEvent]);
    return { handleSearch, handleDebouncedChange, displayedValue };
}
//# sourceMappingURL=useSearch.js.map