"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfigFromCloud = getConfigFromCloud;
exports.targetApiBuildDate = targetApiBuildDate;
exports.cloudCanAcceptChunkedResults = cloudCanAcceptChunkedResults;
const fetch_1 = require("../fetch");
const cloud_1 = require("../globalConfig/cloud");
const logger_1 = __importDefault(require("../logger"));
const CHUNKED_RESULTS_BUILD_DATE = new Date('2025-01-10');
function makeRequest(path, method, body) {
    const apiHost = cloud_1.cloudConfig.getApiHost();
    const apiKey = cloud_1.cloudConfig.getApiKey();
    const url = `${apiHost}/${path}`;
    return (0, fetch_1.fetchWithProxy)(url, {
        method,
        body: JSON.stringify(body),
        headers: { Authorization: `Bearer ${apiKey}` },
    });
}
async function getConfigFromCloud(id) {
    if (!cloud_1.cloudConfig.isEnabled()) {
        throw new Error('Cloud config is not enabled. Please run `promptfoo auth login` to login.');
    }
    const response = await makeRequest(`redteam/configs/${id}`, 'GET');
    const body = await response.json();
    if (response.ok) {
        logger_1.default.info(`Config fetched from cloud: ${id}`);
        logger_1.default.debug(`Config from cloud: ${JSON.stringify(body, null, 2)}`);
    }
    else {
        throw new Error(`Failed to fetch config from cloud: ${response.statusText}`);
    }
    return body;
}
async function targetApiBuildDate() {
    try {
        const response = await makeRequest('/version', 'GET');
        const data = await response.json();
        const { buildDate } = data;
        logger_1.default.debug(`[targetApiBuildDate] ${buildDate}`);
        if (buildDate) {
            return new Date(buildDate);
        }
        return null;
    }
    catch {
        return null;
    }
}
async function cloudCanAcceptChunkedResults() {
    const buildDate = await targetApiBuildDate();
    return buildDate != null && buildDate > CHUNKED_RESULTS_BUILD_DATE;
}
//# sourceMappingURL=cloud.js.map