"use client";
import { useCallback, useRef, useState } from "react";
// ---------------------------------------------------------------------------
// Hook
// ---------------------------------------------------------------------------
const DEFAULT_DEBOUNCE_MS = 300;
/**
 * Headless hook that manages filter, sort, and search query state for
 * a resource collection.
 *
 * State changes are communicated to the consumer via `onStateChange`
 * so they can be synced to URL search params. The hook itself has
 * **zero router dependency** (DD-004 compliance).
 *
 * Platform builders use this alongside {@link useResourceCollection}
 * for full control. The `ResourceWorkbench` shell composes both.
 *
 * @example
 * ```tsx
 * const filters = useResourceFilters({
 *   filterDefs: agentFilterDefs,
 *   sortDefs: agentSortDefs,
 *   onStateChange: (state) => syncToUrl(state),
 * });
 * ```
 */
export function useResourceFilters(options = {}) {
    const { initialFilters = [], initialSort = null, initialQuery = "", onStateChange, queryDebounceMs = DEFAULT_DEBOUNCE_MS, } = options;
    const [filters, setFilters] = useState(initialFilters);
    const [sort, setSortState] = useState(initialSort);
    const [query, setQueryState] = useState(initialQuery);
    const [debouncedQuery, setDebouncedQuery] = useState(initialQuery);
    // Stable ref for the callback to avoid re-triggering effects.
    const onStateChangeRef = useRef(onStateChange);
    onStateChangeRef.current = onStateChange;
    const notify = useCallback((nextFilters, nextSort, nextQuery) => {
        onStateChangeRef.current?.({
            filters: nextFilters,
            sort: nextSort,
            query: nextQuery,
        });
    }, []);
    // --- Query debounce ----------------------------------------------------
    const debounceTimerRef = useRef(undefined);
    const setQuery = useCallback((value) => {
        setQueryState(value);
        clearTimeout(debounceTimerRef.current);
        debounceTimerRef.current = setTimeout(() => {
            setDebouncedQuery(value);
            // Notify uses the latest filter/sort state via closure.
            // Since we can't read state synchronously inside a timeout,
            // we use refs for filters and sort below.
        }, queryDebounceMs);
    }, [queryDebounceMs]);
    // Refs for latest state (used in debounce callback).
    const filtersRef = useRef(filters);
    filtersRef.current = filters;
    const sortRef = useRef(sort);
    sortRef.current = sort;
    // Notify on debounced query change.
    const prevDebouncedQuery = useRef(debouncedQuery);
    if (prevDebouncedQuery.current !== debouncedQuery) {
        prevDebouncedQuery.current = debouncedQuery;
        notify(filtersRef.current, sortRef.current, debouncedQuery);
    }
    // --- Filter actions ----------------------------------------------------
    const addFilter = useCallback((filter) => {
        setFilters((prev) => {
            const next = prev.filter((f) => !(f.filterId === filter.filterId && f.operator === filter.operator));
            const updated = [...next, filter];
            notify(updated, sortRef.current, prevDebouncedQuery.current);
            return updated;
        });
    }, [notify]);
    const removeFilter = useCallback((filterId) => {
        setFilters((prev) => {
            const updated = prev.filter((f) => f.filterId !== filterId);
            notify(updated, sortRef.current, prevDebouncedQuery.current);
            return updated;
        });
    }, [notify]);
    const clearFilters = useCallback(() => {
        setFilters([]);
        notify([], sortRef.current, prevDebouncedQuery.current);
    }, [notify]);
    // --- Sort action -------------------------------------------------------
    const setSort = useCallback((nextSort) => {
        setSortState(nextSort);
        sortRef.current = nextSort;
        notify(filtersRef.current, nextSort, prevDebouncedQuery.current);
    }, [notify]);
    return {
        filters,
        addFilter,
        removeFilter,
        clearFilters,
        sort,
        setSort,
        query,
        debouncedQuery,
        setQuery,
        hasActiveFilters: filters.length > 0,
    };
}
//# sourceMappingURL=useResourceFilters.js.map