local _, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

-- namespace:CreateIcon(parent, stem, size, opts)
-- Passive glyph texture resolver: resolves `stem` to a Lucide icon path via
-- Theme.GetIconPath, special-casing "wolf"/"logo" to Theme.GetLogoPath() (Phase 8 asset,
-- separate Media directory/extension). Returns the texture object directly (NOT wrapped
-- in a synthetic frame) — Icon is a decorative element embedded by host widgets (Button's
-- leading icon, IconButton's glyph, etc.), never a standalone interactive control.
--
-- opts.texCoord (optional): a {left, right, top, bottom} 4-number table cropping the
-- texture via SetTexCoord -- lets callers center-crop a non-square source (e.g. a wide
-- Blizzard EJ instance banner rendered through the numeric-fileID branch) instead of
-- letting it squish into the forced size x size square. Omitted by every pre-existing
-- caller, so default full-texture rendering is unchanged unless a caller opts in.
--
-- No motion helper is used here (no AddHoverFade/AddPressDim/AddToggleAnimation) — Icon
-- is passive; hosts drive its idle/hover/disabled state via SetIconState.
function namespace:CreateIcon(parent, stem, size, opts)
    opts = opts or {}
    size = size or Theme.ICON_MD

    if not stem then
        error("namespace:CreateIcon requires a stem", 2)
    end

    local icon = parent:CreateTexture(nil, "ARTWORK")

    if type(stem) == "number" then
        -- Raw Blizzard texture fileID (e.g. an Encounter Journal instance button image)
        -- rendered directly, bypassing Lucide-stem path resolution entirely.
        icon:SetTexture(stem)
    else
        icon:SetTexture(Theme.GetIconPath(stem))
    end

    icon:SetSnapToPixelGrid(false)
    icon:SetTexelSnappingBias(0)
    icon:SetSize(size, size)

    if type(opts.texCoord) == "table" and #opts.texCoord == 4 then
        local left, right, top, bottom = unpack(opts.texCoord)
        icon:SetTexCoord(left, right, top, bottom)
    end

    -- Widget-local opacity defaults (NOT Theme tokens per 10-RESEARCH.md Area 4).
    icon.idleAlpha = opts.idleAlpha or 0.7
    icon.hoverAlpha = opts.hoverAlpha or 1.0
    icon.disabledAlpha = opts.disabledAlpha or 0.3
    icon:SetAlpha(icon.idleAlpha)

    -- SetTint accepts either (r, g, b, a) or a single color table {r=, g=, b=, a=}.
    -- No default tint is applied at construction — the glyph renders plain white
    -- until a caller opts in.
    function icon:SetTint(r, g, b, a)
        if type(r) == "table" then
            local c = r
            icon:SetVertexColor(c.r, c.g, c.b, c.a or 1)
        else
            icon:SetVertexColor(r, g, b, a or 1)
        end
    end

    -- SetIconState drives BOTH alpha and tint from one function so they can never desync
    -- (WTUI-05) — deliberately no separate tint-only state setter. Tint reuses the
    -- existing SetTint dispatch.
    function icon:SetIconState(state)
        local tint = Theme.ICON_TINT_IDLE
        if state == "hover" then
            tint = Theme.ICON_TINT_HOVER
        elseif state == "disabled" then
            tint = Theme.ICON_TINT_DISABLED
        end
        icon:SetTint(tint)

        if state == "hover" then
            icon:SetAlpha(icon.hoverAlpha)
        elseif state == "disabled" then
            icon:SetAlpha(icon.disabledAlpha)
        else
            icon:SetAlpha(icon.idleAlpha)
        end
    end

    function icon:ClearTint()
        icon:SetVertexColor(1, 1, 1, 1)
    end

    return icon
end
