-- Main

local StringUtil = {}

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 RICH_TEXT_ESCAPE_CHARACTERS 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
