--[[
============================================================
Decor Vendor Addon
© 2026 MidniteDestiny. All Rights Reserved.
============================================================

This file is part of the Decor Vendor addon.

All code, structure, and design are the intellectual
property of MidniteDestiny unless otherwise stated.

You may NOT:
• Copy, reproduce, or redistribute this code
• Modify and redistribute this code
• Use this code in other addons or projects

without explicit permission from the author.

This addon is distributed for personal use only.

============================================================
]]

local addonName, dv = ...

local COLLECTED_ICON_TEXTURE = "Interface\\AddOns\\DecorVendor\\Assets\\collected"

local merchantHooked = false

function dv.HideMerchantCheckmarks()
    for i = 1, MERCHANT_ITEMS_PER_PAGE do
        local button = _G["MerchantItem" .. i .. "ItemButton"]

        if button and button.dvCheckmark then
            button.dvCheckmark:Hide()
        end
    end
end

-- Compatibility wrapper.
-- Older DecorVendor code still calls HideMerchantCheckmarks() directly.
function HideMerchantCheckmarks()
    return dv.HideMerchantCheckmarks()
end

function dv.HookMerchantFrame()
    if merchantHooked then
        return
    end

    merchantHooked = true

    hooksecurefunc("MerchantFrame_Update", function()
        if not MerchantFrame or not MerchantFrame:IsShown() then
            dv.HideMerchantCheckmarks()
            return
        end

        if not vendorSettings.showMerchantCheckmarks then
            dv.HideMerchantCheckmarks()
            return
        end

        local numMerchantItems = GetMerchantNumItems()

        for i = 1, MERCHANT_ITEMS_PER_PAGE do
            local index = ((MerchantFrame.page - 1) * MERCHANT_ITEMS_PER_PAGE) + i
            local button = _G["MerchantItem" .. i .. "ItemButton"]

            if button then
                if not button.dvCheckmark then
                    local check = button:CreateTexture(nil, "OVERLAY", nil, 7)
                    check:SetSize(18, 18)
                    check:SetPoint("BOTTOM", 0, -8)
                    check:SetTexture(COLLECTED_ICON_TEXTURE)
                    button.dvCheckmark = check
                end

                local checked = false

                if button:IsShown() and index <= numMerchantItems then
                    local itemLink = GetMerchantItemLink(index)

                    if itemLink then
                        local itemID = GetItemInfoInstant(itemLink)

                        if itemID and dv.IsItemCollected and dv.IsItemCollected(itemID) then
                            checked = true
                        end
                    end
                end

                button.dvCheckmark:SetShown(checked)
            end
        end
    end)
end

-- Compatibility wrapper.
-- DecorVendor.lua still calls HookMerchantFrame() directly.
function HookMerchantFrame()
    return dv.HookMerchantFrame()
end