"use client";
import { useCallback, useRef, useState } from "react";
import { ApiResourceKind } from "@stigmer/protos/ai/stigmer/commons/apiresource/apiresourcekind/api_resource_kind_pb";
import { useStigmer } from "../hooks.js";
import { useActiveOrgSlug } from "../organization/OrgProvider.js";
import { useFetch } from "../internal/useFetch.js";
const DEFAULT_PAGE_SIZE = 20;
const INITIAL_DATA = {
    sessions: [],
    totalCount: 0,
    totalPages: 0,
    currentPage: 1,
};
/**
 * Data hook that fetches sessions via the SearchService for lightweight,
 * org-scoped, paginated session listing.
 *
 * Unlike {@link useSessionList} which calls the dedicated SessionQueryController.list
 * RPC (loading full session documents with per-session FGA authorization),
 * this hook uses the SearchService which returns lightweight `SearchResult`
 * projections and benefits from the search index's optimized access patterns.
 *
 * Supports incremental "load more" pagination — each call to `loadMore()`
 * fetches the next page and appends results.
 *
 * @example
 * ```tsx
 * function SessionSidebar() {
 *   const { sessions, isLoading, hasMore, loadMore } = useSessionSearch();
 *
 *   return (
 *     <ul>
 *       {sessions.map((s) => <li key={s.id}>{s.description}</li>)}
 *       {hasMore && <button onClick={loadMore}>Load more</button>}
 *     </ul>
 *   );
 * }
 * ```
 */
export function useSessionSearch(options) {
    const stigmer = useStigmer();
    const org = useActiveOrgSlug();
    const pageSize = options?.pageSize ?? DEFAULT_PAGE_SIZE;
    const [page, setPage] = useState(1);
    const accumulatedRef = useRef([]);
    const pageRef = useRef(page);
    pageRef.current = page;
    const fetchFn = org
        ? async () => {
            const currentPage = pageRef.current;
            const resp = await stigmer.search.query({
                kinds: [ApiResourceKind.session],
                org,
                page: { num: currentPage, size: pageSize },
            });
            if (currentPage === 1) {
                accumulatedRef.current = [...resp.entries];
            }
            else {
                accumulatedRef.current = [...accumulatedRef.current, ...resp.entries];
            }
            return {
                sessions: accumulatedRef.current,
                totalCount: resp.totalCount,
                totalPages: resp.totalPages,
                currentPage,
            };
        }
        : null;
    const { data, isLoading, isRefetching, error, refetch } = useFetch(fetchFn, [org, pageSize, page, stigmer], INITIAL_DATA);
    const loadMore = useCallback(() => {
        if (data.currentPage < data.totalPages) {
            setPage((p) => p + 1);
        }
    }, [data.currentPage, data.totalPages]);
    const fullRefetch = useCallback(() => {
        accumulatedRef.current = [];
        setPage(1);
        refetch();
    }, [refetch]);
    return {
        sessions: data.sessions,
        totalCount: data.totalCount,
        totalPages: data.totalPages,
        currentPage: data.currentPage,
        isLoading,
        isRefetching,
        error,
        refetch: fullRefetch,
        hasMore: data.currentPage < data.totalPages,
        loadMore,
    };
}
//# sourceMappingURL=useSessionSearch.js.map