import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { getAgentDir } from "../sdk/coding-agent/index.js";
export const DEFAULTS = {
    observationThresholdTokens: 1_000,
    compactionThresholdTokens: 50_000,
    reflectionThresholdTokens: 10_000,
    passive: false,
    debugLog: false,
    observerMaxChunkTokens: 30_000,
};
const SETTINGS_KEY = "observational-memory";
const PASSIVE_ENV = "PI_OBSERVATIONAL_MEMORY_PASSIVE";
const DEFAULT_MAX_TURNS = 16;
function positiveIntegerOrUndefined(value) {
    return Number.isInteger(value) && typeof value === "number" && value > 0 ? value : undefined;
}
function normalizeTurnLimit(normalized, key) {
    if (!(key in normalized))
        return;
    const value = positiveIntegerOrUndefined(normalized[key]);
    if (value === undefined) {
        delete normalized[key];
    }
    else {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        normalized[key] = value;
    }
}
function normalizeSettingsConfig(value) {
    const normalized = { ...value };
    if ("passive" in normalized && typeof normalized.passive !== "boolean")
        delete normalized.passive;
    if ("debugLog" in normalized && typeof normalized.debugLog !== "boolean")
        delete normalized.debugLog;
    normalizeTurnLimit(normalized, "observerMaxTurnsPerRun");
    normalizeTurnLimit(normalized, "observerMaxChunkTokens");
    normalizeTurnLimit(normalized, "reflectorMaxTurnsPerPass");
    normalizeTurnLimit(normalized, "prunerMaxTurnsPerPass");
    normalizeTurnLimit(normalized, "compactionMaxToolCalls");
    return normalized;
}
export function resolveTurnLimits(config) {
    return {
        observerMaxTurnsPerRun: config.observerMaxTurnsPerRun ?? DEFAULT_MAX_TURNS,
        reflectorMaxTurnsPerPass: config.reflectorMaxTurnsPerPass ?? config.compactionMaxToolCalls ?? DEFAULT_MAX_TURNS,
        prunerMaxTurnsPerPass: config.prunerMaxTurnsPerPass ?? config.compactionMaxToolCalls ?? DEFAULT_MAX_TURNS,
    };
}
export function readEnvConfig(env = process.env) {
    const rawPassive = env[PASSIVE_ENV];
    if (rawPassive === undefined)
        return {};
    const passive = rawPassive.trim().toLowerCase();
    if (["1", "true", "yes", "on"].includes(passive))
        return { passive: true };
    if (["0", "false", "no", "off"].includes(passive))
        return { passive: false };
    return {};
}
function readNamespacedConfig(path) {
    if (!existsSync(path))
        return {};
    try {
        const raw = JSON.parse(readFileSync(path, "utf-8"));
        const nested = raw[SETTINGS_KEY];
        return nested && typeof nested === "object" ? normalizeSettingsConfig(nested) : {};
    }
    catch {
        return {};
    }
}
export function loadConfig(cwd, env = process.env) {
    const globalPath = join(getAgentDir(), "settings.json");
    const projectPath = join(cwd, ".spectral", "settings.json");
    return {
        ...DEFAULTS,
        ...readNamespacedConfig(globalPath),
        ...readNamespacedConfig(projectPath),
        ...readEnvConfig(env),
    };
}
