"use client";
import { useCallback, useMemo, useState } from "react";
let nextId = 0;
function uid() {
    return `sv-${++nextId}-${Date.now()}`;
}
/**
 * Behavior hook that manages an array of session variable entries for
 * execution-scoped environment variables.
 *
 * Session variables are injected into the agent sandbox at execution
 * time. By default they are ephemeral (single execution), but users
 * can opt into persisting individual entries to their personal
 * environment via the `saveForFuture` flag.
 *
 * Follows the same controlled-state pattern as {@link useWorkspaceEntries}:
 * the consumer owns the hook instance, passes it to UI components, reads
 * the output during submission, and calls {@link UseSessionVariablesReturn.clear}
 * after sending to prevent stale values on the next follow-up.
 *
 * @example
 * ```tsx
 * function Chat({ sessionId, org }: { sessionId: string; org: string }) {
 *   const conv = useSessionConversation(sessionId, org);
 *   const sessionVariables = useSessionVariables();
 *
 *   const handleSubmit = (message: string, model?: string) => {
 *     const runtimeEnv = sessionVariables.isEmpty
 *       ? undefined
 *       : sessionVariables.toRuntimeEnv();
 *     conv.sendFollowUp(message, { modelName: model, runtimeEnv });
 *     sessionVariables.clear();
 *   };
 *
 *   return (
 *     <SessionComposer
 *       onSubmit={handleSubmit}
 *       sessionVariables={sessionVariables}
 *       disabled={!conv.canSendFollowUp}
 *       isSubmitting={conv.isSending}
 *     />
 *   );
 * }
 * ```
 */
export function useSessionVariables() {
    const [entries, setEntries] = useState([]);
    const addEntry = useCallback(() => {
        setEntries((prev) => [
            ...prev,
            { id: uid(), key: "", value: "", isSecret: true, saveForFuture: false },
        ]);
    }, []);
    const removeEntry = useCallback((id) => {
        setEntries((prev) => prev.filter((e) => e.id !== id));
    }, []);
    const updateEntry = useCallback((id, patch) => {
        setEntries((prev) => prev.map((e) => (e.id === id ? { ...e, ...patch } : e)));
    }, []);
    const clear = useCallback(() => {
        setEntries([]);
    }, []);
    const hasValidEntries = useMemo(() => entries.some((e) => e.key.trim() !== "" && e.value.trim() !== ""), [entries]);
    const toRuntimeEnv = useCallback(() => {
        const result = {};
        for (const entry of entries) {
            const key = entry.key.trim();
            if (key === "" || entry.value.trim() === "")
                continue;
            result[key] = { value: entry.value, isSecret: entry.isSecret };
        }
        return result;
    }, [entries]);
    const toSaveForFutureEnv = useCallback(() => {
        const result = {};
        for (const entry of entries) {
            const key = entry.key.trim();
            if (key === "" || entry.value.trim() === "")
                continue;
            if (!entry.saveForFuture)
                continue;
            result[key] = { value: entry.value, isSecret: entry.isSecret };
        }
        return result;
    }, [entries]);
    const hasSaveForFutureEntries = useMemo(() => entries.some((e) => e.saveForFuture &&
        e.key.trim() !== "" &&
        e.value.trim() !== ""), [entries]);
    const isEmpty = entries.length === 0;
    return useMemo(() => ({
        entries,
        addEntry,
        removeEntry,
        updateEntry,
        clear,
        isEmpty,
        hasValidEntries,
        toRuntimeEnv,
        toSaveForFutureEnv,
        hasSaveForFutureEntries,
    }), [entries, addEntry, removeEntry, updateEntry, clear, isEmpty, hasValidEntries, toRuntimeEnv, toSaveForFutureEnv, hasSaveForFutureEntries]);
}
//# sourceMappingURL=useSessionVariables.js.map