"use strict";
/**
 * Utility functions for the build pipeline stage.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.keysRelated = keysRelated;
exports.capToolArguments = capToolArguments;
exports.humanize = humanize;
exports.inferSchema = inferSchema;
exports.buildInputMappings = buildInputMappings;
const defaults_1 = require("../../../../modules/defaults");
/**
 * Check if two keys are semantically related (for computed key fallback).
 * Replicates the logic from pattern-detector but local to the build stage.
 */
function keysRelated(a, b) {
    const la = a.toLowerCase();
    const lb = b.toLowerCase();
    if (la.includes(lb) || lb.includes(la))
        return true;
    const urlKeys = ['url', 'href', 'link', 'src'];
    if (urlKeys.includes(la) && urlKeys.includes(lb))
        return true;
    const pathKeys = ['path', 'file', 'filepath', 'filename', 'name'];
    if (pathKeys.some(p => la.includes(p)) && pathKeys.some(p => lb.includes(p)))
        return true;
    return false;
}
/** Cap `limit` in tool arguments to avoid sending huge payloads to downstream LLM steps. */
function capToolArguments(args) {
    const capped = { ...args };
    if (typeof capped.limit === 'number' && capped.limit > defaults_1.TOOL_ARG_LIMIT_CAP) {
        capped.limit = defaults_1.TOOL_ARG_LIMIT_CAP;
    }
    return capped;
}
/** Convert a snake_case/camelCase field name to a readable label. */
function humanize(name) {
    return name
        .replace(/([a-z])([A-Z])/g, '$1 $2')
        .replace(/[_-]/g, ' ')
        .replace(/\b\w/g, (c) => c.toUpperCase())
        .trim();
}
/**
 * Infer a JSON Schema from a sample value, recursively.
 */
function inferSchema(value, withDefault = false) {
    if (value === null || value === undefined) {
        return { type: 'string' };
    }
    if (typeof value === 'string') {
        const schema = { type: 'string' };
        if (withDefault)
            schema.default = value;
        return schema;
    }
    if (typeof value === 'number') {
        const schema = { type: 'number' };
        if (withDefault)
            schema.default = value;
        return schema;
    }
    if (typeof value === 'boolean') {
        const schema = { type: 'boolean' };
        if (withDefault)
            schema.default = value;
        return schema;
    }
    if (Array.isArray(value)) {
        const schema = { type: 'array' };
        if (withDefault)
            schema.default = value;
        if (value.length > 0) {
            if (value.every((v) => typeof v === 'string')) {
                schema.items = { type: 'string' };
            }
            else if (value.every((v) => typeof v === 'number')) {
                schema.items = { type: 'number' };
            }
            else if (value.every((v) => v && typeof v === 'object' && !Array.isArray(v))) {
                const allKeys = new Map();
                for (const el of value) {
                    for (const [k, v] of Object.entries(el)) {
                        if (!allKeys.has(k))
                            allKeys.set(k, v);
                    }
                }
                const props = {};
                for (const [k, v] of allKeys) {
                    props[k] = inferSchema(v, false);
                    props[k].description = humanize(k);
                }
                schema.items = { type: 'object', properties: props };
            }
        }
        return schema;
    }
    if (typeof value === 'object') {
        const entries = Object.entries(value);
        const props = {};
        for (const [k, v] of entries) {
            props[k] = inferSchema(v, withDefault);
            props[k].description = humanize(k);
        }
        return { type: 'object', properties: props };
    }
    return { type: 'string' };
}
/**
 * Build data mappings from a previous activity's output fields.
 */
function buildInputMappings(prevActivityId, prevResult) {
    const mappings = {};
    if (prevResult && typeof prevResult === 'object' && !Array.isArray(prevResult)) {
        for (const key of Object.keys(prevResult)) {
            mappings[key] = `{${prevActivityId}.output.data.${key}}`;
        }
    }
    return mappings;
}
