"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContainsCodeLanguage = exports.handleCodeLanguage = exports.handleCodeCorrectness = exports.handleCodeReadability = void 0;
const matchers_1 = require("../matchers");
const invariant_1 = __importDefault(require("../util/invariant"));
const handleCodeReadability = async ({ assertion, prompt, test, outputString, workspaceId, }) => {
    let codeToEvaluate = '';
    if (typeof outputString === 'string') {
        codeToEvaluate = outputString;
    }
    else if (outputString != null) {
        try {
            codeToEvaluate = JSON.stringify(outputString, null, 2);
        }
        catch {
            codeToEvaluate = String(outputString);
        }
    }
    (0, invariant_1.default)(typeof prompt === 'string', 'Prompt must be a string');
    if (codeToEvaluate.trim() === '') {
        return {
            assertion,
            pass: false,
            score: 0,
            reason: 'No generated code available to evaluate for readability.',
            tokensUsed: {
                total: 0,
                prompt: 0,
                completion: 0,
                cached: 0,
                completionDetails: {
                    reasoning: 0,
                    acceptedPrediction: 0,
                    rejectedPrediction: 0,
                },
            },
        };
    }
    const options = test?.options ?? {};
    const resp = await (0, matchers_1.matchesCodeReadability)(prompt, codeToEvaluate, options, undefined, workspaceId);
    return {
        assertion,
        ...resp,
    };
};
exports.handleCodeReadability = handleCodeReadability;
const handleCodeCorrectness = async ({ assertion, outputString, prompt, test, renderedValue, workspaceId, }) => {
    (0, invariant_1.default)(typeof outputString === 'string', 'Code correctness assertion requires a string output (the LLM-generated code).');
    const instruction = renderedValue || test.vars?.query || prompt || '';
    (0, invariant_1.default)(typeof instruction === 'string' && instruction.trim() !== '', '"code-correctness" assertion type must have a non-empty string prompt value');
    const resp = await (0, matchers_1.matchesCodeCorrectness)(instruction, outputString, test.options, workspaceId);
    return {
        assertion,
        ...resp,
    };
};
exports.handleCodeCorrectness = handleCodeCorrectness;
const handleCodeLanguage = async ({ assertion, outputString, renderedValue, test, workspaceId, }) => {
    const prompt = renderedValue || test.vars?.query || '';
    (0, invariant_1.default)(typeof prompt === 'string' && prompt.trim() !== '', '"code-language" assertion type must have a non-empty string prompt value');
    (0, invariant_1.default)(typeof outputString === 'string', 'code-language assertion requires a string output.');
    const result = await (0, matchers_1.detectCodeLanguage)(outputString, test.options, prompt, workspaceId);
    return {
        assertion,
        ...result,
    };
};
exports.handleCodeLanguage = handleCodeLanguage;
const handleContainsCodeLanguage = async ({ assertion, renderedValue, outputString, test, workspaceId, }) => {
    (0, invariant_1.default)(typeof renderedValue === 'string', 'Contains code language assertion type must have a string value (the language to detect).');
    const prompt = renderedValue || test.vars?.query || '';
    (0, invariant_1.default)(typeof prompt === 'string' && prompt.trim() !== '', '"contains code-language" assertion type must have a non-empty string prompt value');
    (0, invariant_1.default)(typeof outputString === 'string', 'Contains code language assertion requires a string output (the text to analyze).');
    const resp = await (0, matchers_1.matchesContainsCodeLanguage)(renderedValue, outputString, test.options, undefined, workspaceId);
    return {
        assertion,
        ...resp,
    };
};
exports.handleContainsCodeLanguage = handleContainsCodeLanguage;
//# sourceMappingURL=handleCodeLanguage.js.map