"use client";
import { useEffect, useState } from "react";
import { useFetch } from "../internal/useFetch";
const DEFAULT_PAGE_SIZE = 30;
const DEFAULT_DEBOUNCE_MS = 300;
/**
 * Internal hook that powers both `useMcpServerSearch` and `useSkillSearch`.
 *
 * Wraps a `ListParams -> ListResult` function with query state management,
 * debounced search, loading/error tracking, and cancellation-safe fetching.
 *
 * Not exported from the public API — consumers use the resource-specific
 * hooks (`useMcpServerSearch`, `useSkillSearch`) instead.
 */
export function useResourceSearch(listFn, org, options) {
    const [query, setQuery] = useState("");
    const [debouncedQuery, setDebouncedQuery] = useState("");
    const pageSize = options?.pageSize ?? DEFAULT_PAGE_SIZE;
    const debounceMs = options?.debounceMs ?? DEFAULT_DEBOUNCE_MS;
    const scope = options?.scope ?? "org";
    useEffect(() => {
        const timer = setTimeout(() => setDebouncedQuery(query), debounceMs);
        return () => clearTimeout(timer);
    }, [query, debounceMs]);
    const { data: results, isLoading, isRefetching, error, refetch } = useFetch(async () => {
        const params = {
            org,
            query: debouncedQuery || undefined,
            excludePublic: false,
            crossOrgPublic: scope === "all",
            page: { num: 1, size: pageSize },
        };
        const result = await listFn(params);
        return [...result.entries];
    }, [listFn, org, debouncedQuery, pageSize, scope], []);
    return { results, isLoading, isRefetching, error, query, setQuery, refetch };
}
//# sourceMappingURL=useResourceSearch.js.map