"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolFormatter = void 0;
const zapRegistry_1 = require("../tools/goatTools/zapRegistry");
class ToolFormatter {
    firstResult(step) {
        var _a, _b;
        return (_b = (_a = step === null || step === void 0 ? void 0 : step.toolResults) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
    }
    toJson(value, pretty = true) {
        try {
            return JSON.stringify(value, null, pretty ? 2 : 0);
        }
        catch (_a) {
            return String(value);
        }
    }
    formatText(step, mode) {
        const entry = this.firstResult(step);
        if (!entry) {
            return {
                formatString: "No tool results found.",
                input: {},
                output: {}
            };
        }
        const { toolName, input = {}, output } = entry;
        return this.buildText(toolName, input, output, mode);
    }
    buildText(toolName, args, result, mode) {
        var _a, _b, _c, _d, _e;
        const isTest = mode === "test";
        switch (toolName) {
            case "BalanceTool": {
                return {
                    formatString: "Retrieved balance information.",
                    input: args,
                    output: result
                };
            }
            case "PriceTool": {
                return {
                    formatString: "Price Retrived",
                    input: args,
                    output: result
                };
            }
            case "TokenAddressTool": {
                const symbol = (_a = args.symbol) !== null && _a !== void 0 ? _a : "token";
                const chainId = (_b = args.chainId) !== null && _b !== void 0 ? _b : "unknown";
                const address = String(result !== null && result !== void 0 ? result : "");
                return {
                    formatString: isTest
                        ? `Address of ${symbol} on chain ${chainId} is ${address}`
                        : `Found the ${symbol} contract address on chain ${chainId}: ${address}`,
                    input: args,
                    output: result
                };
            }
            case "DollarToTokenAmountTool": {
                const amount = (_c = args.amount) !== null && _c !== void 0 ? _c : "0";
                const token = (_d = args.tokenAddresses) !== null && _d !== void 0 ? _d : "token";
                const chainId = (_e = args.chainId) !== null && _e !== void 0 ? _e : "unknown";
                const tokenAmount = String(result !== null && result !== void 0 ? result : "");
                return {
                    formatString: isTest
                        ? `$${amount} is equal to ${tokenAmount} ${token} on chain ${chainId}`
                        : `Converted $${amount} into ${tokenAmount} ${token} on chain ${chainId}.`,
                    input: args,
                    output: result
                };
            }
            case "TrendingNewsTool": {
                return {
                    formatString: isTest ? this.toJson(result) : "Fetching trending news...",
                    input: args,
                    output: result
                };
            }
            case "TrendingTokenTool": {
                return {
                    formatString: isTest ? this.toJson(result) : "Fetching trending tokens...",
                    input: args,
                    output: result
                };
            }
            case "BridgeLinkGeneratorTool": {
                const link = String(result !== null && result !== void 0 ? result : "https://test.dzap.io/trade");
                return {
                    formatString: link,
                    input: args,
                    output: result !== null && result !== void 0 ? result : link
                };
            }
            case "SwapLinkGeneratorTool": {
                const link = String(result !== null && result !== void 0 ? result : "https://test.dzap.io/trade");
                return {
                    formatString: link,
                    input: args,
                    output: result !== null && result !== void 0 ? result : link
                };
            }
            case "ZapCallDataGeneratorTool": {
                const sessionId = args.sessionId;
                const callData = sessionId ? (0, zapRegistry_1.getZapData)(sessionId) : null;
                return {
                    formatString: this.toJson(callData),
                    input: args,
                    output: callData
                };
            }
            case "RequestChainChangeTool": {
                return {
                    formatString: String(result),
                    input: args,
                    output: result
                };
            }
            case "ShowBalanceUITool": {
                return {
                    formatString: this.toJson(args),
                    input: args,
                    output: result
                };
            }
            default: {
                const formatString = !isTest
                    ? `Completed ${toolName}.`
                    : [
                        `Tool Name: ${toolName}`,
                        `Tool Args: ${this.toJson(args)}`,
                        `Tool Result: ${this.toJson(result)}`
                    ].join("\n");
                return {
                    formatString,
                    input: args,
                    output: result
                };
            }
        }
    }
}
exports.ToolFormatter = ToolFormatter;
