"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseUsername = parseUsername;
exports.abbreviateNumber = abbreviateNumber;
exports.getDateOrString = getDateOrString;
exports.truncateText = truncateText;
function parseUsername(username, ctx, font, size, maxLength, weight = '') {
    const safeUsername = username?.replace(/\s/g, '') ? username : '?????';
    const usernameChars = safeUsername.split('');
    let finalUsername = '';
    let newSize = +size;
    let textLength = 0;
    let finalized = false;
    while (!finalized) {
        const editableUsername = usernameChars.join('');
        ctx.font = `${weight} ${newSize}px ${font}`.trim();
        ctx.textAlign = 'left';
        ctx.fillStyle = '#FFFFFF';
        const actualLength = ctx.measureText(editableUsername).width;
        if (actualLength >= maxLength) {
            if (newSize > 60) {
                newSize -= 1;
            }
            else {
                usernameChars.pop();
            }
        }
        if (actualLength <= maxLength) {
            finalUsername = usernameChars.join('');
            textLength = actualLength;
            finalized = true;
        }
    }
    return {
        username: finalUsername,
        newSize,
        textLength,
    };
}
function getFirstDigitsAsDecimal(numString) {
    const digits = ((numString.length - 1) % 3) + 1;
    if (numString.length < 4) {
        return numString;
    }
    const decimal = numString.slice(digits, digits + 1);
    return `${numString.slice(0, digits)}${decimal === '0' || decimal === '00' || digits === 3
        ? ''
        : `.${decimal.replace(/0$/g, '')}`}`;
}
function abbreviateNumber(number) {
    const numString = `${number}`;
    const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const abbreviations = ['', 'K', 'M', 'B', 'T'].concat(new Array(letters.length)
        .fill('AA')
        .map((_, i) => letters[i]?.repeat(2) ?? ''));
    const selectedAbbr = abbreviations[Math.floor((numString.length - 1) / 3)] ?? '??';
    return `${getFirstDigitsAsDecimal(numString)}${selectedAbbr}`;
}
function getDateOrString(dateInput, createdTimestamp, localDateType = 'en') {
    const dateOptions = {
        month: 'short',
        day: 'numeric',
        year: 'numeric',
    };
    const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;
    if (typeof dateInput === 'string') {
        if (iso8601Regex.test(dateInput)) {
            const dateInstance = new Date(dateInput);
            return dateInstance.toLocaleDateString(localDateType, dateOptions);
        }
        return dateInput;
    }
    if (dateInput instanceof Date) {
        return dateInput.toLocaleDateString(localDateType, dateOptions);
    }
    if (dateInput === undefined || dateInput === null) {
        const timestamp = /^\d+$/.test(createdTimestamp)
            ? +createdTimestamp
            : createdTimestamp;
        return new Date(timestamp).toLocaleDateString(localDateType, dateOptions);
    }
    return '';
}
function truncateText(text, limit = 25, fromEnd = false) {
    if (text.length > limit) {
        if (fromEnd) {
            return `...${text.slice(-limit)}`;
        }
        return `${text.slice(0, limit)}...`;
    }
    return text;
}
//# sourceMappingURL=text.js.map