"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __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.maybeRecordFirstRun = exports.writeGlobalConfigPartial = exports.writeGlobalConfig = exports.readGlobalConfig = exports.resetGlobalConfig = void 0;
/**
 * Functions for manipulating the global configuration file, which lives at
 * ~/.promptfoo/promptfoo.yaml by default.
 */
const fs = __importStar(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const path = __importStar(require("path"));
const util_1 = require("./util");
let globalConfigCache = null;
function resetGlobalConfig() {
    globalConfigCache = null;
}
exports.resetGlobalConfig = resetGlobalConfig;
function readGlobalConfig() {
    if (!globalConfigCache) {
        const configDir = (0, util_1.getConfigDirectoryPath)();
        const configFilePath = path.join(configDir, 'promptfoo.yaml');
        if (fs.existsSync(configFilePath)) {
            globalConfigCache = js_yaml_1.default.load(fs.readFileSync(configFilePath, 'utf-8'));
        }
        else {
            if (!fs.existsSync(configDir)) {
                fs.mkdirSync(configDir, { recursive: true });
            }
            globalConfigCache = { hasRun: false };
            fs.writeFileSync(configFilePath, js_yaml_1.default.dump(globalConfigCache));
        }
    }
    return globalConfigCache;
}
exports.readGlobalConfig = readGlobalConfig;
function writeGlobalConfig(config) {
    fs.writeFileSync(path.join((0, util_1.getConfigDirectoryPath)(true /* createIfNotExists */), 'promptfoo.yaml'), js_yaml_1.default.dump(config));
}
exports.writeGlobalConfig = writeGlobalConfig;
/**
 * Merges the top-level keys into existing config.
 * @param partialConfig New keys to merge into the existing config.
 */
function writeGlobalConfigPartial(partialConfig) {
    const currentConfig = readGlobalConfig();
    const updatedConfig = { ...currentConfig };
    for (const key in partialConfig) {
        const value = partialConfig[key];
        if (value) {
            updatedConfig[key] = value;
        }
        else {
            delete updatedConfig[key];
        }
    }
    writeGlobalConfig(updatedConfig);
}
exports.writeGlobalConfigPartial = writeGlobalConfigPartial;
function maybeRecordFirstRun() {
    // Return true if first run
    try {
        const config = readGlobalConfig();
        if (!config.hasRun) {
            config.hasRun = true;
            writeGlobalConfig(config);
            return true;
        }
        return false;
    }
    catch (err) {
        return false;
    }
}
exports.maybeRecordFirstRun = maybeRecordFirstRun;
//# sourceMappingURL=globalConfig.js.map