// retoor <retoor@molodetz.nl>

export class Format {
    static COMPACT_FROM = 1000000;
    static UNITS = [
        { limit: 1000000000000, suffix: "T" },
        { limit: 1000000000, suffix: "B" },
        { limit: 1000000, suffix: "M" },
    ];

    static number(value) {
        const amount = Math.trunc(Number(value) || 0);
        const magnitude = Math.abs(amount);
        if (magnitude < Format.COMPACT_FROM) return amount.toLocaleString("en-GB");
        for (const unit of Format.UNITS) {
            if (magnitude >= unit.limit) {
                const scaled = amount / unit.limit;
                const digits = Math.abs(scaled) < 10 ? 2 : 1;
                return `${Number(scaled.toFixed(digits))}${unit.suffix}`;
            }
        }
        return amount.toLocaleString("en-GB");
    }

    static coins(value) {
        return `${Format.number(value)}c`;
    }

    static exact(value) {
        return Math.trunc(Number(value) || 0).toLocaleString("en-GB");
    }

    static speed(value) {
        const amount = Number(value) || 0;
        return `${Number(amount.toFixed(2))}x`;
    }

    static duration(seconds) {
        const total = Math.max(0, Math.floor(Number(seconds) || 0));
        const hours = Math.floor(total / 3600);
        const minutes = Math.floor((total % 3600) / 60);
        const secs = total % 60;
        if (hours > 0) return `${hours}h ${minutes}m`;
        if (minutes > 0) return `${minutes}m ${secs}s`;
        return `${secs}s`;
    }
}
