import { homedir } from "node:os";
export function formatPathForDisplay(path) {
    const home = homedir();
    if (path === home)
        return "~";
    if (path.startsWith(`${home}/`)) {
        return `~${path.slice(home.length)}`;
    }
    return path;
}
export function formatTokenWindow(tokens) {
    if (tokens >= 1_000_000) {
        const m = tokens / 1_000_000;
        return `${m.toFixed(tokens % 1_000_000 === 0 ? 0 : 1)}M`;
    }
    if (tokens >= 1_000) {
        const k = tokens / 1_000;
        return `${k.toFixed(tokens % 1_000 === 0 ? 0 : 1)}k`;
    }
    return String(tokens);
}
export function formatAdaptiveNumber(value, minDecimals, maxDecimals) {
    if (!Number.isFinite(value))
        return "0";
    if (value === 0)
        return value.toFixed(minDecimals);
    let decimals = minDecimals;
    let formatted = value.toFixed(decimals);
    while (decimals < maxDecimals && Number(formatted) === 0) {
        decimals += 1;
        formatted = value.toFixed(decimals);
    }
    return formatted;
}
export function formatUsageSnapshot(usage) {
    const stats = `↑${formatTokenWindow(usage.input)} ↓${formatTokenWindow(usage.output)} (r${formatTokenWindow(usage.cacheRead)} w${formatTokenWindow(usage.cacheWrite)})`;
    const percent = usage.contextWindow > 0 ? (usage.contextWindowUsageTokens / usage.contextWindow) * 100 : 0;
    const percentStr = `${formatAdaptiveNumber(percent, 1, 3)}%`;
    return `${stats} · ${percentStr}/${formatTokenWindow(usage.contextWindow)}`;
}
export function formatUsdCost(costTotal) {
    return `$${formatAdaptiveNumber(costTotal, 2, 5)}`;
}
export function formatCwd(cwd) {
    return formatPathForDisplay(cwd);
}
export function formatDurationMs(durationMs) {
    if (durationMs === null || durationMs === undefined || !Number.isFinite(durationMs)) {
        return "?";
    }
    const totalSeconds = Math.max(0, Math.floor(durationMs / 1000));
    const seconds = totalSeconds % 60;
    const minutesTotal = Math.floor(totalSeconds / 60);
    const minutes = minutesTotal % 60;
    const hours = Math.floor(minutesTotal / 60);
    if (hours > 0) {
        return `${hours}h ${minutes}m ${seconds}s`;
    }
    if (minutes > 0) {
        return `${minutes}m ${seconds}s`;
    }
    return `${seconds}s`;
}
//# sourceMappingURL=format.js.map