"use client";
import { useMemo } from "react";
/**
 * Reactive computation hook that aggregates environment variable
 * availability from all session-level sources.
 *
 * The pool enables cross-referencing between the session variables
 * panel, agent env form, and MCP server env form — so a variable
 * entered in one place is recognized as "already provided" by the
 * others. This eliminates duplicate credential prompting.
 *
 * Pure computation — no side effects, no API calls. Recomputes
 * via `useMemo` when any input source changes.
 *
 * Platform builders who use individual setup hooks can pass the
 * pool's `availableKeys` to `useMcpServerSetup` and `useAgentSetup`
 * via their `poolKeys` parameter for cross-referencing.
 *
 * @example
 * ```tsx
 * const sessionVars = useSessionVariables();
 * const personalEnv = usePersonalEnvironment(org);
 *
 * const pool = useSessionEnvPool({
 *   personalEnvKeys: new Set(Object.keys(personalEnv.environment?.spec?.data ?? {})),
 *   manualSecrets: sessionVars.entries,
 * });
 *
 * // pool.isKeySatisfied("GITHUB_TOKEN") → true if entered anywhere
 * ```
 */
export function useSessionEnvPool(input) {
    const { personalEnvKeys, manualSecrets, agentRuntimeEnv, mcpRuntimeEnv, } = input;
    const valueMap = useMemo(() => {
        const map = new Map();
        if (agentRuntimeEnv) {
            for (const [key, value] of Object.entries(agentRuntimeEnv)) {
                map.set(key, value);
            }
        }
        if (mcpRuntimeEnv) {
            for (const [key, value] of Object.entries(mcpRuntimeEnv)) {
                map.set(key, value);
            }
        }
        if (manualSecrets) {
            for (const entry of manualSecrets) {
                const k = entry.key.trim();
                if (k !== "" && entry.value.trim() !== "") {
                    map.set(k, { value: entry.value, isSecret: entry.isSecret });
                }
            }
        }
        return map;
    }, [manualSecrets, agentRuntimeEnv, mcpRuntimeEnv]);
    const availableKeys = useMemo(() => {
        const keys = new Set();
        if (personalEnvKeys) {
            for (const key of personalEnvKeys) {
                keys.add(key);
            }
        }
        for (const key of valueMap.keys()) {
            keys.add(key);
        }
        return keys;
    }, [personalEnvKeys, valueMap]);
    const getAvailableValue = useMemo(() => (key) => valueMap.get(key), [valueMap]);
    const isKeySatisfied = useMemo(() => (key) => availableKeys.has(key), [availableKeys]);
    return { availableKeys, getAvailableValue, isKeySatisfied };
}
//# sourceMappingURL=useSessionEnvPool.js.map