-- BlizzardSetExport.lua (v15.48.0)
-- Patch 5.5.4 replaced MoP Classic's transmog with the modern Wardrobe and
-- removed the apply API (C_Transmog.SetPending). Instead of maintaining a
-- parallel apply UI, ETB now feeds its sets into Blizzard's native Custom Sets,
-- which apply natively — including incomplete sets (owned pieces on, rest
-- skipped via appearanceID 0). This module translates an ETB set into a
-- C_TransmogCollection.NewCustomSet(name, icon, itemTransmogInfoList) call.
local ADDON, NS = ...
local ETB = NS.ETB

-- equip-location string -> Blizzard transmog slot ID (confirmed from a live
-- custom-set dump: Wrist=9, Hands=10, Back=15, MainHand=16, OffHand=17, ...)
local INVTYPE_TO_SLOT = {
    INVTYPE_HEAD=1, INVTYPE_SHOULDER=3, INVTYPE_BODY=4,
    INVTYPE_CHEST=5, INVTYPE_ROBE=5, INVTYPE_WAIST=6, INVTYPE_LEGS=7,
    INVTYPE_FEET=8, INVTYPE_WRIST=9, INVTYPE_HAND=10, INVTYPE_CLOAK=15,
    INVTYPE_WEAPON=16, INVTYPE_WEAPONMAINHAND=16, INVTYPE_2HWEAPON=16,
    INVTYPE_RANGED=16, INVTYPE_RANGEDRIGHT=16,
    INVTYPE_WEAPONOFFHAND=17, INVTYPE_HOLDABLE=17, INVTYPE_SHIELD=17,
    INVTYPE_TABARD=19,
}
local NUM_SLOTS = 19

local function makeInfo(srcID)
    srcID = srcID or 0
    if CreateAndInitFromMixin and ItemTransmogInfoMixin then
        local ok, info = pcall(CreateAndInitFromMixin, ItemTransmogInfoMixin, srcID, 0, 0)
        if ok and info then return info end
    end
    return { appearanceID = srcID, secondaryAppearanceID = 0, illusionID = 0 }
end

-- Build a dense slot-indexed ItemTransmogInfo list from an ETB set. Undefined
-- slots stay at appearanceID 0 → Blizzard skips them (incomplete-set behaviour).
function ETB:BuildTransmogInfoListForSet(setID)
    local items = ETB.SetsData and ETB.SetsData.GetSetItems and ETB.SetsData:GetSetItems(setID)
    if not items then return nil, 0 end
    local list = {}
    for i = 1, NUM_SLOTS do list[i] = makeInfo(0) end
    local placed = 0
    for _, item in pairs(items) do
        local itemID = item and item.itemID
        if itemID then
            local srcID = item.sourceID
            if (not srcID or srcID == 0) and C_TransmogCollection and C_TransmogCollection.GetItemInfo then
                local ok, _, s = pcall(C_TransmogCollection.GetItemInfo, itemID)
                if ok then srcID = s end
            end
            local _, _, _, equipLoc = GetItemInfoInstant(itemID)
            local slot = equipLoc and INVTYPE_TO_SLOT[equipLoc]
            if slot and srcID and srcID > 0 then
                list[slot] = makeInfo(srcID)
                placed = placed + 1
            end
        end
    end
    return list, placed
end

-- Create (or could later update) a Blizzard custom set from an ETB set.
function ETB:SendSetToBlizzard(setID)
    if not (C_TransmogCollection and C_TransmogCollection.NewCustomSet) then
        print("|cffff5555ETB|r: custom-set API not available on this client.")
        return
    end
    local nMax = C_TransmogCollection.GetNumMaxCustomSets and C_TransmogCollection.GetNumMaxCustomSets() or 25
    local have = C_TransmogCollection.GetCustomSets and #C_TransmogCollection.GetCustomSets() or 0
    if have >= nMax then
        print(string.format("|cffff5555ETB|r: custom-set slots full (%d/%d). Delete one and retry.", have, nMax))
        return
    end
    local list, placed = self:BuildTransmogInfoListForSet(setID)
    if not list or placed == 0 then
        print("|cffff5555ETB|r: couldn't resolve any transmoggable pieces for set "..tostring(setID))
        return
    end
    local name = "ETB " .. tostring(setID)
    if ETB.SetsData and ETB.SetsData.GetSet then
        local ok, s = pcall(ETB.SetsData.GetSet, ETB.SetsData, setID)
        if ok and s and s.name then name = "ETB: " .. s.name end
    end
    local icon = 134400
    local ok, newID = pcall(C_TransmogCollection.NewCustomSet, name, icon, list)
    if ok and newID ~= nil then
        print(string.format("|cff44ff44ETB|r: created Blizzard custom set '%s' (id %s) with %d pieces. Open the transmog NPC -> Custom Sets tab to apply.", name, tostring(newID), placed))
    else
        print("|cffff5555ETB|r: NewCustomSet failed: "..tostring(newID))
    end
end

SLASH_ETBSEND1 = "/etbsend"
SlashCmdList.ETBSEND = function(msg)
    local setID = tonumber((msg or ""):match("%-?%d+"))
    if not setID then print("|cff33ddffETB|r usage: /etbsend <setID>  (sends an ETB set to Blizzard's Custom Sets)"); return end
    ETB:SendSetToBlizzard(setID)
end
