--[[
============================================================
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 = ...

------------------------------------------------
-- Event Handler For Found Vendors
-- Marks vendors as found when opening their merchant window.
------------------------------------------------

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("MERCHANT_SHOW")

eventFrame:SetScript("OnEvent", function()
    if not dv.npcs then
        return
    end

    local guid = UnitGUID("target")

    if not guid or issecretvalue(guid) then
        return
    end

    local npcID = select(6, strsplit("-", guid))
    npcID = tonumber(npcID)

    if not npcID then
        return
    end

    vendorSettings.visited = vendorSettings.visited or {}

    for _, group in ipairs(dv.npcs) do
        for _, vendor in ipairs(group.vendors or {}) do
            if vendor.id == npcID then
                if not vendorSettings.visited[vendor.id] then
                    vendorSettings.visited[vendor.id] = true

                    if vendorSettings.hideCompletedThings or vendorSettings.markFoundVendors then
                        if BuildVendorUI then
                            BuildVendorUI()
                        elseif dv.BuildVendorUI then
                            dv.BuildVendorUI()
                        end
                    end
                end

                return
            end
        end
    end
end)