"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordCreateSnippetExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const CreateVariableStep_1 = __importDefault(require("../../sharedUtils/mablscript/canonical/CreateVariableStep"));
const EvaluateJSStep_1 = __importDefault(require("../../sharedUtils/mablscript/canonical/EvaluateJSStep"));
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const mablApi_1 = require("../../../mablApi");
const encodingUtil_1 = require("../../../util/encodingUtil");
const variable_constants_1 = require("../../sharedUtils/variable-constants");
exports.recordCreateSnippetExecutor = {
    needsFind: () => false,
    getStepType: () => mablApi_1.ExecuteToolName.Javascript,
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        if (!toolParams?.snippet_body) {
            logger.error('Record create snippet executor requires snippet_body');
            return {
                success: false,
                errorMessage: 'snippet_body parameter is required',
            };
        }
        const { snippet_body: snippetBody, snippet_name: snippetName, snippet_description: snippetDescription, save_result_to_variable: saveResultToVariable, parameters: snippetParameters, } = toolParams;
        try {
            const snippetParameterDefinitions = [];
            const parameterOverrides = [];
            if (snippetParameters) {
                for (const [name, value] of Object.entries(snippetParameters)) {
                    snippetParameterDefinitions.push({ id: name, name });
                    parameterOverrides.push({ id: name, value: String(value) });
                }
            }
            const inlineSnippet = {
                snippet_type: 'javascript',
                body: (0, encodingUtil_1.b64EncodeUnicodeInNode)(snippetBody),
                name: snippetName,
                description: snippetDescription,
                parameters: snippetParameterDefinitions,
            };
            const javaScriptDescriptor = {
                inlineSnippet,
                parameterOverrides,
            };
            let step;
            if (saveResultToVariable) {
                const variable = {
                    variableType: mablscriptFind_1.VariableType.JAVASCRIPT,
                    name: `user.${saveResultToVariable}`,
                    humanizedName: saveResultToVariable,
                    value: variable_constants_1.EMPTY_VARIABLE_VALUE_PLACEHOLDER,
                    javaScript: javaScriptDescriptor,
                    snippet: inlineSnippet,
                };
                step = new CreateVariableStep_1.default(variable);
                eventHandler.saveVariable(variable);
            }
            else {
                step = new EvaluateJSStep_1.default(javaScriptDescriptor, false, undefined, inlineSnippet);
            }
            return (0, toolExecutor_types_1.recordStepAndTrackResult)(() => Promise.resolve({ steps: [step], stepsToReplay: 1, step }), 0, stateProvider, eventHandler, logger);
        }
        catch (error) {
            logger.error('Error executing create snippet tool:', error);
            return {
                success: false,
                errorMessage: error instanceof Error ? error.message : 'Unknown error',
            };
        }
    },
};
