local NS = rawget(_G, "MCDVoiceCooldownNS") or {}
_G.MCDVoiceCooldownNS = NS

NS.UI = NS.UI or {}
NS.UI.DialogTextArea = NS.UI.DialogTextArea or {}

local DialogTextArea = NS.UI.DialogTextArea
local Skin = NS.UI.Skin

function DialogTextArea:Create(parent, width, height)
    width = tonumber(width) or 650
    height = tonumber(height) or 300

    local scrollFrame = CreateFrame("ScrollFrame", nil, parent, "UIPanelScrollFrameTemplate,BackdropTemplate")
    scrollFrame:SetSize(width, height)
    if Skin and Skin.ApplyWindowChrome then
        Skin:ApplyWindowChrome(scrollFrame, { inset = true, noFooter = true, noTopLine = true, topHeight = 0 })
    end

    local editBox = CreateFrame("EditBox", nil, scrollFrame)
    editBox:SetMultiLine(true)
    editBox:SetAutoFocus(false)
    editBox:SetFontObject("ChatFontNormal")
    editBox:SetSize(math.max(1, width - 34), height)
    editBox:SetTextInsets(8, 8, 8, 8)
    editBox:SetScript("OnEscapePressed", function(self)
        self:ClearFocus()
    end)
    editBox:SetScript("OnTextChanged", function(self)
        local textHeight = self:GetHeight() or height
        if self.GetNumLines then
            textHeight = math.max(height, (self:GetNumLines() or 1) * 18 + 20)
        end
        self:SetHeight(textHeight)
    end)
    scrollFrame:SetScrollChild(editBox)

    if Skin and Skin.SkinEditBox then
        Skin:SkinEditBox(editBox)
    end

    local scrollBar = scrollFrame.ScrollBar or (scrollFrame.GetName and rawget(_G, tostring(scrollFrame:GetName() or "") .. "ScrollBar"))
    if scrollBar and Skin and Skin.SkinScrollBar then
        Skin:SkinScrollBar(scrollBar)
    end

    return scrollFrame, editBox
end
