"use strict";
/**
 * Benchmark configuration loader.
 *
 * Loads .benchmark-config.json from the working directory with sensible defaults.
 * All fields are optional — missing fields fall back to defaults.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadBenchmarkConfig = loadBenchmarkConfig;
exports.getDefaultConfig = getDefaultConfig;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const zod_1 = require("zod");
const debug_1 = require("../../utils/debug");
const agent_interface_1 = require("../agent-interface");
const storage_paths_1 = require("../../utils/storage-paths");
/** Plugin + flag defaults that don't depend on `installDir`. */
const DEFAULT_PLUGINS = {
    tokens: true,
    cache: true,
    turns: true,
    compactions: true,
    contextSize: true,
    cost: true,
    duration: true,
    summary: true,
    jsonWriter: true,
};
const DEFAULT_FLAGS = {
    benchmarkEnabled: true,
    logEnabled: true,
    suppressWizardLogs: false,
};
/**
 * Build the default benchmark config. When `installDir` is provided, the
 * benchmark and log paths are scoped to that project's run dir under the
 * cache root. Without `installDir`, paths fall back to a `_bootstrap` run
 * dir — this only matters for the rare callers (and tests) that need a
 * config object before installDir resolution.
 */
function buildDefaultConfig(installDir) {
    const benchmarkPath = installDir
        ? (0, storage_paths_1.getBenchmarkFile)(installDir)
        : path_1.default.join((0, storage_paths_1.getCacheRoot)(), 'runs', '_bootstrap', 'benchmark.json');
    const logPath = installDir
        ? (0, storage_paths_1.getLogFile)(installDir)
        : path_1.default.join((0, storage_paths_1.getCacheRoot)(), 'bootstrap.log');
    return {
        plugins: { ...DEFAULT_PLUGINS },
        output: {
            benchmarkPath,
            benchmarkEnabled: DEFAULT_FLAGS.benchmarkEnabled,
            logPath,
            logEnabled: DEFAULT_FLAGS.logEnabled,
            suppressWizardLogs: DEFAULT_FLAGS.suppressWizardLogs,
        },
    };
}
const BenchmarkConfigFileSchema = zod_1.z
    .object({
    plugins: zod_1.z.record(zod_1.z.string(), zod_1.z.boolean()).optional(),
    output: zod_1.z
        .object({
        benchmarkPath: zod_1.z.string(),
        benchmarkEnabled: zod_1.z.boolean(),
        logPath: zod_1.z.string(),
        logEnabled: zod_1.z.boolean(),
        suppressWizardLogs: zod_1.z.boolean(),
    })
        .partial()
        .optional(),
})
    .passthrough();
function loadBenchmarkConfig(installDir) {
    const defaults = buildDefaultConfig(installDir);
    const configPath = process.env.AMPLITUDE_WIZARD_BENCHMARK_CONFIG ??
        path_1.default.join(installDir, '.benchmark-config.json');
    try {
        const raw = fs_1.default.readFileSync(configPath, 'utf-8');
        const result = BenchmarkConfigFileSchema.safeParse(JSON.parse(raw));
        if (!result.success)
            throw result.error;
        const parsed = result.data;
        const config = {
            plugins: { ...defaults.plugins, ...parsed.plugins },
            output: { ...defaults.output, ...parsed.output },
        };
        // Env var overrides for parallel runs
        if (process.env.AMPLITUDE_WIZARD_BENCHMARK_FILE) {
            config.output.benchmarkPath = process.env.AMPLITUDE_WIZARD_BENCHMARK_FILE;
        }
        if (process.env.AMPLITUDE_WIZARD_LOG_FILE) {
            config.output.logPath = process.env.AMPLITUDE_WIZARD_LOG_FILE;
        }
        // If benchmark output is disabled, disable the jsonWriter plugin
        if (!config.output.benchmarkEnabled) {
            config.plugins.jsonWriter = false;
        }
        (0, debug_1.logToFile)(`${agent_interface_1.AgentSignals.BENCHMARK} Loaded config from ${configPath}`);
        return config;
    }
    catch {
        // No config file or invalid JSON — use defaults
        const config = defaults;
        // Env var overrides
        if (process.env.AMPLITUDE_WIZARD_BENCHMARK_FILE) {
            config.output.benchmarkPath = process.env.AMPLITUDE_WIZARD_BENCHMARK_FILE;
        }
        if (process.env.AMPLITUDE_WIZARD_LOG_FILE) {
            config.output.logPath = process.env.AMPLITUDE_WIZARD_LOG_FILE;
        }
        return config;
    }
}
/**
 * Returns the default benchmark config with bootstrap paths. Tests and
 * callers without an `installDir` rely on this; in-flight wizard runs
 * should use {@link loadBenchmarkConfig} so paths land under the project's
 * run dir.
 */
function getDefaultConfig() {
    return buildDefaultConfig();
}
