"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDocumentSourcesFromDatasetConfig = getDocumentSourcesFromDatasetConfig;
exports.generateDocumentDatasetRowsForEvaluation = generateDocumentDatasetRowsForEvaluation;
const fs = __importStar(require("node:fs"));
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
const logger_1 = __importDefault(require("../../../logger"));
const downloadDocumentSourcesToTemp_1 = require("./downloadDocumentSourcesToTemp");
const runPythonDocumentDatasetSynthesis_1 = require("./runPythonDocumentDatasetSynthesis");
/**
 * Resolve ordered document sources from dataset config (saved at create time).
 */
function getDocumentSourcesFromDatasetConfig(datasetConfig) {
    if (!datasetConfig || typeof datasetConfig !== 'object') {
        return [];
    }
    const raw = datasetConfig.documentSources;
    if (Array.isArray(raw)) {
        return raw
            .map((item) => {
            if (item && typeof item === 'object' && typeof item.url === 'string') {
                return { name: typeof item.name === 'string' ? item.name : undefined, url: item.url };
            }
            return null;
        })
            .filter((x) => x !== null);
    }
    const urls = datasetConfig.documentSourceUrls;
    const names = datasetConfig.documentNames;
    if (Array.isArray(urls) && urls.length > 0) {
        return urls.map((url, i) => {
            const u = typeof url === 'string' ? url : '';
            let name;
            if (Array.isArray(names) && names[i] && typeof names[i] === 'object' && names[i] !== null) {
                const n = names[i].name;
                name = typeof n === 'string' ? n : undefined;
            }
            return { url: u, name };
        });
    }
    return [];
}
function filterRowsByFieldNames(rows, fieldNames) {
    if (!fieldNames || fieldNames.length === 0) {
        return rows;
    }
    return rows.map((row) => {
        if (!row || typeof row !== 'object') {
            return row;
        }
        const out = {};
        for (const name of fieldNames) {
            if (name in row) {
                out[name] = row[name];
            }
        }
        return out;
    });
}
async function generateDocumentDatasetRowsForEvaluation(params) {
    const entries = getDocumentSourcesFromDatasetConfig(params.datasetConfig);
    if (entries.length === 0) {
        throw new Error('No document sources configured for this dataset. Save document URLs (documentSources) before running evaluation.');
    }
    const validEntries = entries.filter((e) => e.url && e.url.startsWith('http'));
    if (validEntries.length === 0) {
        throw new Error('Document sources must be valid http(s) URLs.');
    }
    let cleanupDownload = null;
    let previousJsonPath = null;
    let previousJsonDir = null;
    try {
        const { sources, cleanup } = await (0, downloadDocumentSourcesToTemp_1.downloadDocumentSourcesToTemp)(validEntries.map((e) => ({ url: e.url, name: e.name })));
        cleanupDownload = cleanup;
        const documentPaths = sources.map((s) => s.localPath);
        logger_1.default.info(`Evaluation-time document synthesis: ${documentPaths.length} file(s), numSamples=${params.numSamples}`);
        const prev = Array.isArray(params.previousRecordData) ? params.previousRecordData : [];
        if (prev.length > 0) {
            previousJsonDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trusys-prev-rows-'));
            previousJsonPath = path.join(previousJsonDir, 'previous-rows.json');
            fs.writeFileSync(previousJsonPath, JSON.stringify(prev), 'utf8');
            logger_1.default.info(`Evaluation-time document synthesis: passing ${prev.length} previous row(s) for novelty filtering`);
        }
        const pyResult = await (0, runPythonDocumentDatasetSynthesis_1.runPythonDocumentDatasetSynthesis)({
            documentPaths,
            includeExpectedOutput: params.includeExpectedOutput,
            numSamples: params.numSamples,
            previousRowsJsonPath: previousJsonPath ?? '',
        });
        if (!pyResult.success) {
            throw new Error(pyResult.error || 'Document synthesis failed');
        }
        const rows = filterRowsByFieldNames(pyResult.testCaseVars, params.fieldsToGenerate);
        return { rows, tokenUsages: pyResult.tokenUsages };
    }
    finally {
        cleanupDownload?.();
        if (previousJsonPath) {
            try {
                if (fs.existsSync(previousJsonPath)) {
                    fs.unlinkSync(previousJsonPath);
                }
            }
            catch {
                // best-effort
            }
        }
        if (previousJsonDir) {
            try {
                if (fs.existsSync(previousJsonDir)) {
                    fs.rmdirSync(previousJsonDir);
                }
            }
            catch {
                // best-effort
            }
        }
    }
}
//# sourceMappingURL=generateDocumentDatasetRowsForEvaluation.js.map