-- InventoryPagePatch.lua
-- Patches ISInventoryPage for Inventory Tetris B42

local Logger = require("Utils/Logger")
local InventoryPagePatch = {}

function InventoryPagePatch.apply()
    if not ISInventoryPage or not ISInventoryPage.updateLootButtonsForTetrisSearch then
        Logger.error("ISInventoryPage not available")
        return false
    end

    Logger.log("Applying ISInventoryPage patches...")

    -- Patch the loot buttons update function
    function ISInventoryPage:updateLootButtonsForTetrisSearch()
        if not self.tetrisSearchButton then return end

        local sisterPage = self:tetrisGetSisterPage()
        local needsSearch = self.needSearch or (sisterPage and sisterPage.needSearch)

        if self.onCharacter then
            if self.EDNLDropItems then
                self.EDNLDropItems:setVisible(not needsSearch)
            end
        else
            if type(self.lootAll) == 'table' then
                self.lootAll:setVisible(not needsSearch)
            end

            if self.EDNLLootItems then
                self.EDNLLootItems:setVisible(not needsSearch)
            end
        end

        self.tetrisSearchButton:setVisible(needsSearch and self.needSearch)
    end

    Logger.success("ISInventoryPage patches applied successfully")
    return true
end

return InventoryPagePatch