--## Author: Yuyuli ## Version: 13
if WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE then return end

local DeleteHearthstone = CreateFrame("Frame")
DeleteHearthstone:RegisterEvent("PLAYER_LOGIN")

---@type number[]
local Toys = {
  54452,  -- Ethereal Portal
  93672,  -- Dark Portal
  162973, -- Greatfather Winter's Hearthstone
  163045, -- Headless Horseman's Hearthstone
  165669, -- Lunar Elder's Hearthstone
  165670, -- Peddlefeet's Lovely Hearthstone
  165802, -- Noble Gardener's Hearthstone
  166746, -- Fire Eater's Hearthstone
  166747, -- Brewfest Reveler's Hearthstone
  168907, -- Holographic Digitalization Hearthstone
  172179, -- Eternal Traveler's Hearthstone
  180290, -- Night Fae Hearthstone
  182773, -- Necrolord Hearthstone
  183716, -- Venthyr Sinstone
  184353, -- Kyrian Hearthstone
  188952, -- Dominated Hearthstone
  190196, -- Enlightened Hearthstone
  190237, -- Broker Translocation Matrix
  193588, -- Timewalker's Hearthstone
  200630, -- Ohn'ir Windsage's Hearthstone
  208704, -- Deepdweller's Earthen Hearthstone
  208704, -- Deepdweller's Earthen Hearthstone
  209035, -- Hearthstone of the Flame
  212337, -- Stone of the Hearth
  228940, -- Notorious Thread's Hearthstone
  235016, -- Redeployment Module
  236687, -- Explosive Hearthstone
  245970, -- P.O.S.T. Master's Express Hearthstone
  246565, -- Cosmic Hearthstone
}

DeleteHearthstone.itemsDeletedRecently = {}

function DeleteHearthstone:HasHearthstoneItemsAndToys()
  local hasHearthstone = C_Item.GetItemCount(6948) > 0
  self.hasGarrisonHearthstone = C_Item.GetItemCount(110560) > 0 and PlayerHasToy(110560)
  self.hasDalaranHearthstone = C_Item.GetItemCount(140192) > 0 and PlayerHasToy(140192)

  if hasHearthstone then
    for i = 1, #Toys do
      if PlayerHasToy(Toys[i]) then
        self.hasHearthstone = true
        return true;
      end
    end
  end

  if self.hasGarrisonHearthstone or self.hasDalaranHearthstone then
    return true
  end

  return false
end

function DeleteHearthstone:DeleteItem(bagID, slotIndex, itemLink)
  C_Container.PickupContainerItem(bagID, slotIndex)
  DeleteCursorItem()
  --print("|cffEEE4AEDelete Hearthstone:|r Deleted " .. itemLink)
end

function DeleteHearthstone:SearchBagsAndDestroy()
  if InCombatLockdown() then return end
  if not self:HasHearthstoneItemsAndToys() then return end

  C_Timer.After(3, function() wipe(self.itemsDeletedRecently) end)

  for bagID = BACKPACK_CONTAINER, NUM_BAG_SLOTS do
    for slotIndex = 1, C_Container.GetContainerNumSlots(bagID) do
      local itemLink = C_Container.GetContainerItemLink(bagID, slotIndex)
      if itemLink then
        local itemID = C_Item.GetItemInfoInstant(itemLink)
        if (itemID == 6948 and self.hasHearthstone)
        or (itemID == 110560 and self.hasGarrisonHearthstone)
        or (itemID == 140192 and self.hasDalaranHearthstone) then
          -- if you click too fast would try deleting the same first item it finds
          if not self.itemsDeletedRecently[itemID] then
            self:DeleteItem(bagID, slotIndex, itemLink)
            self.itemsDeletedRecently[itemID] = true
            return
          end
        end
      end
    end
  end
end

DeleteHearthstone:SetScript("OnEvent", function(self, event, ...)
  if event == "PLAYER_LOGIN" then
    WorldFrame:HookScript("OnMouseDown", function()
      self:SearchBagsAndDestroy()
    end)
  end
end)