"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSearchCommon = void 0;
const react_1 = require("react");
const normalize = (value) => value.trim();
function useSearchCommon({ initialValue, onPersistSearchValue, onTrackSearch, }) {
    const [displayedValue, setDisplayedValue] = (0, react_1.useState)(initialValue);
    const handleSearch = (0, react_1.useCallback)((queryOrEvent) => {
        const nextValue = typeof queryOrEvent === "string" ? queryOrEvent : queryOrEvent.target.value;
        setDisplayedValue(nextValue);
    }, []);
    const shouldEmitEvent = (0, react_1.useCallback)((currentQuery, previousQuery) => currentQuery !== "" && currentQuery !== previousQuery, []);
    const handleDebouncedChange = (0, react_1.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 };
}
exports.useSearchCommon = useSearchCommon;
//# sourceMappingURL=useSearch.js.map