/**
 * Multi-framework AI tool adapters for ag-bash.
 *
 * The primary entry point is `createBashTool()` which returns a BashToolBuilder.
 * The builder provides method chaining to convert to any supported framework format.
 *
 * @example
 * ```ts
 * import { Bash, createBashTool } from "@ag-bash/bash";
 *
 * const bash = new Bash({ files: { "/data.json": '{"ok":true}' } });
 * const builder = createBashTool({ sandbox: bash });
 *
 * // Vercel AI SDK (default, backwards-compatible)
 * const { tools } = builder.forVercel();
 *
 * // OpenAI function calling
 * const { tools, handleToolCall } = builder.forOpenAI();
 *
 * // Anthropic tool_use
 * const { tools, handleToolUse } = builder.forAnthropic();
 *
 * // LangChain
 * const { tools } = builder.forLangChain();
 * ```
 */
import { toAnthropic } from "./adapters/anthropic.js";
import { toLangChain } from "./adapters/langchain.js";
import { toOpenAI } from "./adapters/openai.js";
import { toVercel } from "./adapters/vercel.js";
import { buildToolDefinitions } from "./core.js";
/**
 * Creates a multi-framework bash tool builder.
 *
 * For backwards compatibility, the returned object also exposes a `.tools`
 * property that returns the Vercel format (matching the original API).
 *
 * @param options - Sandbox instance and optional lifecycle hooks.
 * @returns A BashToolBuilder with framework conversion methods.
 */
export function createBashTool(options) {
    const definitions = buildToolDefinitions(options);
    // Cache adapters lazily to avoid re-computation
    let cachedVercel;
    let cachedOpenAI;
    let cachedAnthropic;
    let cachedLangChain;
    let cachedGeneric;
    const builder = {
        forVercel() {
            if (!cachedVercel) {
                cachedVercel = toVercel(definitions);
            }
            return cachedVercel;
        },
        forOpenAI() {
            if (!cachedOpenAI) {
                cachedOpenAI = toOpenAI(definitions);
            }
            return cachedOpenAI;
        },
        forAnthropic() {
            if (!cachedAnthropic) {
                cachedAnthropic = toAnthropic(definitions);
            }
            return cachedAnthropic;
        },
        forLangChain() {
            if (!cachedLangChain) {
                cachedLangChain = toLangChain(definitions);
            }
            return cachedLangChain;
        },
        generic() {
            if (!cachedGeneric) {
                const toolLookup = new Map();
                for (const def of definitions) {
                    toolLookup.set(def.name, def);
                }
                cachedGeneric = {
                    tools: definitions,
                    async handleCall(name, args) {
                        const def = toolLookup.get(name);
                        if (!def) {
                            return { error: `Unknown tool: ${name}`, exitCode: 1 };
                        }
                        return def.execute(args);
                    },
                };
            }
            return cachedGeneric;
        },
        // Legacy backwards-compatible `.tools` property
        get tools() {
            return builder.forVercel().tools;
        },
    };
    return builder;
}
// Re-export adapter functions for direct use
export { toAnthropic, toLangChain, toOpenAI, toVercel };
