--Main

local StringUtil: any = {}

function StringUtil.GetReadLength(length: number): number
    return 5
end

function StringUtil.CondenseText(str: string): string
    local finalStr: string = ""

    local isMarkdown: boolean = false
    
    local i = 0
    while i < #str do
        i += 1
        
        local currentChar: string = string.sub(str, i, i)
        
        if currentChar == "<" then
            isMarkdown = true
            continue
        elseif currentChar == ">" then
            isMarkdown = false
            continue
        elseif isMarkdown then
            continue
        end
        
        local wasEscapeCharacter: boolean = false

        if currentChar == "&" then
            --Suspected as escape character. 
            for j = i, #str do
                if string.sub(str, j, j) == ";" then
                    local suspectedEscape: string = string.sub(str, i, j)

                    for _, pattern: string in ipairs(RichTextEscapeCharacters) do
                        if suspectedEscape == pattern[1] then
                            --move i to j (end of escape character)
                            i = j
                            finalStr ..= pattern[2]

                            wasEscapeCharacter = true
                            break
                        end
                    end
                    break
                end
            end
        end

        if not wasEscapeCharacter then
            finalStr ..= currentChar
        end
    end
    
    return finalStr
end

function StringUtil.CountCharacters(str: string): number
    return #StringUtil.CondenseText(str)
end

return StringUtil
