"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;
const cloud_1 = require("../globalConfig/cloud");
const logger_1 = __importDefault(require("../logger"));
function makeRequest(path, method, body) {
    const apiHost = cloud_1.cloudConfig.getApiHost();
    const apiKey = cloud_1.cloudConfig.getApiKey();
    const url = `${apiHost}/${path}`;
    return fetch(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;
}
//# sourceMappingURL=cloud.js.map