"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestChainChangeTool = void 0;
const zod_1 = require("zod");
const ai_1 = require("ai");
const chainChangeRegistry_1 = require("./chainChangeRegistry");
const node_fs_1 = __importDefault(require("node:fs"));
const staticPath_1 = require("../../utils/staticPath");
const requestChainChangeToolInputSchema = zod_1.z.object({
    chainID: zod_1.z.number().describe("The chain ID of the blockchain network to switch to"),
    sessionId: zod_1.z.string().describe("The current session ID for this user"),
});
class RequestChainChangeTool {
    constructor() {
        this.tool = (0, ai_1.tool)({
            description: "Request a change of the blockchain network for transactions. " +
                "Use this tool to initiate a network switch." +
                "This tool must be followed by ChangeChainTool",
            inputSchema: requestChainChangeToolInputSchema,
            execute: async (parameters) => {
                const { chainID, sessionId } = parameters;
                console.log(`Requesting blockchain network change to chain ID: ${chainID}`);
                if (!isChainIDSupported(chainID)) {
                    return `Error: Chain ID ${chainID} is not supported.`;
                }
                (0, chainChangeRegistry_1.saveChainChangeData)(sessionId, { chainID });
                return chainID;
            }
        });
    }
}
exports.RequestChainChangeTool = RequestChainChangeTool;
function isChainIDSupported(chainID) {
    const filePath = (0, staticPath_1.resolveStaticFilePath)("chain_IDs.json");
    const chainIDs = JSON.parse(node_fs_1.default.readFileSync(filePath, "utf-8"));
    const supportedChainIDs = Object.values(chainIDs);
    return supportedChainIDs.includes(chainID);
}
