local _AddonName, _EchoRaidTools = ...;

-- UTF-8 Sub
local Utf8Sub = function(input, size)
    local output = ""
    if type(input) ~= "string" then
      return output
    end
    local i = 1
    while (size > 0) do
      local byte = input:byte(i)
      if not byte then
        return output
      end
      if byte < 128 then
        -- ASCII byte
        output = output .. input:sub(i, i)
        size = size - 1
      elseif byte < 192 then
        -- Continuation bytes
        output = output .. input:sub(i, i)
      elseif byte < 244 then
        -- Start bytes
        output = output .. input:sub(i, i)
        size = size - 1
      end
      i = i + 1
    end

    -- Add any bytes that are part of the sequence
    while (true) do
      local byte = input:byte(i)
      if byte and byte >= 128 and byte < 192 then
        output = output .. input:sub(i, i)
      else
        break
      end
      i = i + 1
    end

    return output
  end
  _EchoRaidTools.Utf8Sub = Utf8Sub

  

---@diagnostic disable-next-line: lowercase-global
function tDClone(orig)
  local orig_type = type(orig);
  local copy;
  if orig_type == 'table' then
    copy = {};
    for orig_key, orig_value in next, orig, nil do
      copy[tDClone(orig_key)] = tDClone(orig_value);
    end
    setmetatable(copy, tDClone(getmetatable(orig)));
  else -- number, string, boolean, etc
    copy = orig;
  end
  return copy;
end
