class Text {
    static convertBase64ToHex(textInBase64) {
        if (!textInBase64) {
            return textInBase64;
        }
        if (!this.isBase64(textInBase64)) {
            return textInBase64;
        }
        const hex = Buffer.from(textInBase64, "base64").toString("hex");
        return hex;
    }
    /**
     * Convert an OTLP wire id (trace / span / profile id) to lowercase hex.
     *
     * OTLP/protobuf carries ids as bytes, which protobuf decoders render
     * as base64 strings — but OTLP/JSON carries the SAME fields as hex
     * strings (32 chars for trace ids, 16 for span ids). Hex strings also
     * satisfy the base64 alphabet, so feeding them to convertBase64ToHex
     * would silently decode them into garbage bytes. Length disambiguates
     * safely: the base64 form of an 8/16-byte id is always 12/24 chars,
     * so a 16- or 32-char hex-only string can never be a base64 id.
     */
    static convertOtlpIdToHex(value) {
        if (!value) {
            return "";
        }
        if (Text.OTLP_HEX_ID_REGEX.test(value)) {
            return value.toLowerCase();
        }
        try {
            return Text.convertBase64ToHex(value);
        }
        catch (_a) {
            return "";
        }
    }
    static getLetterFromAByNumber(number) {
        return String.fromCharCode("a".charCodeAt(0) + number);
    }
    static getNextLowercaseLetter(letter) {
        const charCode = letter.charCodeAt(0);
        const nextLetter = String.fromCharCode(charCode + 1).toString();
        return nextLetter;
    }
    static fromPascalCaseToDashes(text) {
        let result = text.replace(/([A-Z])/g, " $1");
        result = result.trim();
        result = result.replace(/\s+/g, "-");
        return result.toLowerCase();
    }
    static getFirstWord(text) {
        if (!text || text.length === 0) {
            return text;
        }
        const textArr = text.split(" ");
        let firstIndex = 0;
        while (firstIndex < textArr.length && !textArr[firstIndex]) {
            firstIndex++;
        }
        return textArr[firstIndex] || text;
    }
    static getLastWord(text) {
        if (!text || text.length === 0) {
            return text;
        }
        const textArr = text.split(" ");
        let lastIndex = textArr.length - 1;
        while (lastIndex >= 0 && !textArr[lastIndex]) {
            lastIndex--;
        }
        return textArr[lastIndex] || text;
    }
    static trimStartUntilThisWord(text, word) {
        if (!text || text.length === 0) {
            return text;
        }
        const index = text.indexOf(word);
        if (index === -1) {
            return text;
        }
        return text.substring(index);
    }
    static trimUpQuotesFromStartAndEnd(text) {
        if (!text || text.length === 0) {
            return text;
        }
        if (text.startsWith('"') && !text.endsWith('"')) {
            text = text.substring(1);
        }
        if (text.endsWith('"') && !text.startsWith('"')) {
            text = text.substring(0, text.length - 1);
        }
        // check for single quotes
        if (text.startsWith("'") && !text.endsWith("'")) {
            text = text.substring(1);
        }
        if (text.endsWith("'") && !text.startsWith("'")) {
            text = text.substring(0, text.length - 1);
        }
        return text;
    }
    static trimEndUntilThisWord(text, word) {
        if (!text || text.length === 0) {
            return text;
        }
        const index = text.lastIndexOf(word);
        if (index === -1) {
            return text;
        }
        return text.substring(0, index + word.length);
    }
    static isBase64(text) {
        if (!text || typeof text !== "string") {
            return false;
        }
        // Remove data URI prefix if present (e.g., data:image/jpeg;base64,)
        const base64String = text.replace(/^data:[^;]+;base64,/, "");
        // Check if string is empty after removing prefix
        if (!base64String) {
            return false;
        }
        // Base64 string length should be a multiple of 4
        if (base64String.length % 4 !== 0) {
            return false;
        }
        // Improved regex for Base64 validation
        const regex = /^[A-Za-z0-9+/]*={0,2}$/;
        return regex.test(base64String);
    }
    static extractBase64FromDataUri(text) {
        if (!text || typeof text !== "string") {
            return text;
        }
        // Check if it's a data URI
        if (text.startsWith("data:")) {
            const base64Index = text.indexOf(";base64,");
            if (base64Index !== -1) {
                return text.substring(base64Index + 8); // 8 is length of ';base64,'
            }
        }
        // Return original string if not a data URI
        return text;
    }
    static extractMimeTypeFromDataUri(text) {
        if (!text || typeof text !== "string") {
            return null;
        }
        // Check if it's a data URI
        if (text.startsWith("data:")) {
            const mimeTypeEnd = text.indexOf(";");
            if (mimeTypeEnd !== -1) {
                return text.substring(5, mimeTypeEnd); // 5 is length of 'data:'
            }
        }
        return null;
    }
    static generateRandomText(length) {
        if (!length) {
            length = 10;
        }
        let result = "";
        const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }
    static trimLines(text) {
        return text
            .split("\n")
            .map((line) => {
            return line.trim();
        })
            .join("\n");
    }
    static generateRandomNumber(length) {
        if (!length) {
            length = 10;
        }
        let result = "";
        const characters = "12134567890";
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }
    static convertNumberToWords(num) {
        const words = [
            "first",
            "second",
            "third",
            "fourth",
            "fifth",
            "sixth",
            "seventh",
            "eighth",
            "ninth",
            "tenth",
            "eleventh",
            "twelfth",
            "thirteenth",
            "fourteenth",
            "fifteenth",
            "sixteenth",
            "seventeenth",
            "eighteenth",
            "nineteenth",
            "twentieth",
        ];
        if (num <= 20) {
            return words[num - 1];
        }
        if (num % 10 === 0) {
            return `${words[19]} ${words[num / 10 - 2]}`;
        }
        return `${words[19]} ${words[Math.floor(num / 10) - 2]}-${words[(num % 10) - 1]}`;
    }
    static uppercaseFirstLetter(word) {
        if (word.length > 0) {
            return word.charAt(0).toUpperCase() + word.slice(1);
        }
        return word;
    }
    static fromDashesToPascalCase(word) {
        let tempWord = word.replace(/-/g, " ");
        tempWord = tempWord.replace(/\b\w/g, (m) => {
            return m.toUpperCase();
        });
        return tempWord;
    }
    static pascalCaseToDashes(word) {
        let tempWord = word.replace(/[A-Z]/g, (m) => {
            return "-" + m.toLowerCase();
        });
        while (tempWord.includes(" ")) {
            tempWord = tempWord.replace(" ", "-");
        }
        if (tempWord.startsWith("-")) {
            tempWord = this.replaceAt(0, tempWord, " ");
        }
        if (tempWord.endsWith("-")) {
            tempWord = this.replaceAt(tempWord.length - 1, tempWord, " ");
        }
        return tempWord.toLowerCase().trim();
    }
    static replaceAt(index, word, replacement) {
        return (word.substring(0, index) +
            replacement +
            word.substring(index + replacement.length));
    }
    static replaceAll(sentence, search, replaceBy) {
        return sentence.split(search).join(replaceBy);
    }
    static truncate(value, maxLength) {
        if (value === null || value === undefined) {
            return undefined;
        }
        if (maxLength <= 0) {
            return "";
        }
        return value.length > maxLength ? value.slice(0, maxLength) : value;
    }
}
/**
 * Matches an OTLP id already in hex form: 16 chars (8-byte span id)
 * or 32 chars (16-byte trace/profile id).
 */
Text.OTLP_HEX_ID_REGEX = /^(?:[0-9a-fA-F]{16}|[0-9a-fA-F]{32})$/;
export default Text;
//# sourceMappingURL=Text.js.map