"use strict";
/**
 * Detection and classification of consecutive tool-call runs.
 *
 * Finds sequences of 3+ consecutive calls to the same tool and
 * classifies their arguments as varying, constant, or wired.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.findConsecutiveRuns = findConsecutiveRuns;
exports.classifyRunArguments = classifyRunArguments;
const types_1 = require("./types");
/**
 * Detect runs of consecutive calls to the same tool (same name + same server).
 */
function findConsecutiveRuns(steps, minLength = 3) {
    const runs = [];
    let i = 0;
    while (i < steps.length) {
        const step = steps[i];
        if (step.kind !== 'tool') {
            i++;
            continue;
        }
        let runEnd = i + 1;
        while (runEnd < steps.length &&
            steps[runEnd].kind === 'tool' &&
            steps[runEnd].toolName === step.toolName &&
            steps[runEnd].mcpServerId === step.mcpServerId) {
            runEnd++;
        }
        if (runEnd - i >= minLength) {
            runs.push({
                startIndex: i,
                endIndex: runEnd,
                toolName: step.toolName,
                serverId: step.mcpServerId,
                steps: steps.slice(i, runEnd),
            });
        }
        i = Math.max(i + 1, runEnd);
    }
    return runs;
}
/**
 * Analyze a run of repeated tool calls and separate arguments into:
 * - **varying**: keys whose values differ across calls (the iterated data)
 * - **constant**: keys whose values are identical across all calls (shared config)
 * - **wired**: inter-step handles -- excluded from both
 */
function classifyRunArguments(run) {
    const allKeys = new Set();
    for (const step of run) {
        for (const key of Object.keys(step.arguments)) {
            allKeys.add(key);
        }
    }
    const varying = [];
    const constant = {};
    for (const key of allKeys) {
        if (types_1.WIRED_KEYS.has(key))
            continue;
        const values = run.map(s => JSON.stringify(s.arguments[key] ?? null));
        const allSame = values.every(v => v === values[0]);
        if (allSame) {
            constant[key] = run[0].arguments[key];
        }
        else {
            varying.push(key);
        }
    }
    const items = run.map(s => {
        const item = {};
        for (const key of varying) {
            if (s.arguments[key] !== undefined) {
                item[key] = s.arguments[key];
            }
        }
        return item;
    });
    return { varying, constant, items };
}
