import { promises as fs } from "node:fs";
import path from "node:path";
const STATE_FILE_NAME = "cli-state.json";
function resolveStatePath(env) {
    const home = env.HOME?.trim() || env.USERPROFILE?.trim();
    if (!home)
        return null;
    return path.join(home, ".summarize", STATE_FILE_NAME);
}
function parseCliProvider(value) {
    if (value === "claude" ||
        value === "codex" ||
        value === "gemini" ||
        value === "agent" ||
        value === "openclaw" ||
        value === "opencode" ||
        value === "copilot" ||
        value === "agy" ||
        value === "pi") {
        return value;
    }
    return null;
}
export async function readLastSuccessfulCliProvider(env) {
    const statePath = resolveStatePath(env);
    if (!statePath)
        return null;
    try {
        const raw = await fs.readFile(statePath, "utf8");
        const parsed = JSON.parse(raw);
        return parseCliProvider(parsed.lastSuccessfulProvider);
    }
    catch {
        return null;
    }
}
export async function writeLastSuccessfulCliProvider({ env, provider, }) {
    const statePath = resolveStatePath(env);
    if (!statePath)
        return;
    try {
        await fs.mkdir(path.dirname(statePath), { recursive: true });
        const payload = {
            lastSuccessfulProvider: provider,
            updatedAt: new Date().toISOString(),
        };
        await fs.writeFile(statePath, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
    }
    catch {
        // Best-effort cache; failures should not affect summary output.
    }
}
//# sourceMappingURL=cli-fallback-state.js.map