import { appendFileSync, mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
export function formatUsageDateKey(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
}
export function getUsageLogDir(homeDir) {
    const home = homeDir ?? homedir();
    return join(home, ".config", "tau", "logs");
}
export function getUsageLogPath(date, homeDir) {
    const dir = getUsageLogDir(homeDir);
    const key = formatUsageDateKey(date);
    return join(dir, `usage-${key}.jsonl`);
}
export function appendUsageLogEntry(entry, homeDir) {
    try {
        const logDir = getUsageLogDir(homeDir);
        mkdirSync(logDir, { recursive: true, mode: 0o700 });
        const logPath = getUsageLogPath(new Date(entry.timestamp), homeDir);
        appendFileSync(logPath, `${JSON.stringify(entry)}\n`, { mode: 0o600 });
    }
    catch {
        // ignore logging errors
    }
}
export function getUsageTotals(usage) {
    const input = usage?.input ?? 0;
    const output = usage?.output ?? 0;
    const cacheRead = usage?.cacheRead ?? 0;
    const cacheWrite = usage?.cacheWrite ?? 0;
    const total = usage?.totalTokens ?? input + output + cacheRead + cacheWrite;
    return { input, output, cacheRead, cacheWrite, total };
}
export function getUsageCostTotal(usage) {
    return usage?.cost?.total ?? 0;
}
//# sourceMappingURL=logs.js.map