import Foundation

/// Compact formatting for the "advanced detail" thinking-tokens meter — the live token
/// estimate and elapsed time the transcript shows while the model reasons (see
/// `TranscriptItem.thinkingTokens`).
public enum TokenFormatting {
    /// A token count rendered compactly: exact up to 999, then truncated (floored, never
    /// rounded up) to one decimal of thousands with a "k" suffix — 50 → "50", 999 → "999",
    /// 1000 → "1.0k", 1450 → "1.4k", 12345 → "12.3k". Negatives pass through verbatim.
    public static func short(_ count: Int) -> String {
        guard count > 999 else { return "\(count)" }
        let tenthsOfK = count / 100  // floor to tenths of a thousand, so we never round up
        return "\(tenthsOfK / 10).\(tenthsOfK % 10)k"
    }

    /// Elapsed thinking time: one decimal of seconds under a minute ("12.3s"), then
    /// "Mm SSs" beyond ("1m 05s"). Sub-zero durations clamp to "0.0s".
    public static func elapsed(_ seconds: TimeInterval) -> String {
        let total = max(0, seconds)
        if total < 60 { return String(format: "%.1fs", total) }
        let whole = Int(total)
        return "\(whole / 60)m \(String(format: "%02d", whole % 60))s"
    }
}
