-- VDS_Selectors_View
-- ============================================================================
-- currentView picks one of: default | canvas | live | liveWithGallery.
-- Per-view row schemas are consumed by Layout:Compute (rows ordered by name, top to bottom).

local Selectors = VDS.Selectors

-- Forward-declared here so currentView can call it; Selectors_Live.lua loads after this file.

Selectors:Register("live.gallery.hasVariants", {
    reads = { "session.ui.live.decorID", "account.canvas.loadouts" },
    fn    = function(state)
        local decorID = state.session.ui.live.decorID
        if not decorID then return false end
        local list = state.account.canvas.loadouts[decorID]
        return list ~= nil and #list > 0
    end,
})

-- config.canShow -- cog button hidden in canvas + live (those views own their right column).
Selectors:Register("config.canShow", {
    reads = { "session.ui.filterMode", "session.ui.live.active" },
    fn    = function(state)
        if state.session.ui.live.active then return false end
        if state.session.ui.filterMode == "canvas" then return false end
        return true
    end,
})

Selectors:Register("currentView", {
    reads = {
        "session.ui.filterMode",
        "session.ui.live.active",
        "session.ui.configShown",
    },
    calls = { "live.gallery.hasVariants" },
    fn    = function(state)
        if state.session.ui.live.active then
            if Selectors:Call("live.gallery.hasVariants", state, {}) then
                return "liveWithGallery"
            end
            return "live"
        end
        if state.session.ui.filterMode == "canvas"   then return "canvas" end
        if state.session.ui.filterMode == "crafting" then return "stock"  end
        -- Custom: left-side palette-management popout; dye list renders like default.
        if state.session.ui.filterMode == "custom" then return "defaultWithCustom" end
        -- Config sidebar for default-mode only; toggled via title.configBtn.
        if state.session.ui.configShown then return "defaultWithConfig" end
        return "default"
    end,
})

-- default + canvas rows are static in VDS_LayoutConfig.lua. live/liveWithGallery rows are dynamic.

-- liveEdit height: pad-top 4 + headerRow 20 + harmonyStrip 18 + N*(slot 44) + actionBarGap 10 +
--                  actionBar 34 + pad-bot 6 + gaps (N+3)*4 = 104 + N*48.
-- (slot row is 44 since "Slot N" moved INSIDE the swatch; actionBar is 34 to fit
--  the tall primary Apply button.)
-- +32 when the recent-dye strip is visible (strip 28 + one more stack gap 4);
-- mirrors the live.hasRecentDyes gate on liveEdit.recentStrip.
Selectors:Register("view.live.rows", {
    reads = { "session.ui.live.slots", "account.live.recentDyes" },
    fn    = function(state)
        local slots = state.session.ui.live.slots
        local numSlots = slots and #slots or 0
        local recentH = #state.account.live.recentDyes > 0 and 32 or 0
        return {
            { name = "title",     height = 28 },
            { name = "search",    height = 28 },
            { name = "tabs",      height = 26 },
            { name = "subfilter", height = 24 },
            { name = "body",      height = "fill" },
            { name = "liveEdit",  height = 104 + (numSlots * 48) + recentH },
        }
    end,
})

-- Total window height = 552 + liveEdit growth. Extended by 22 when the harmonyStrip
-- landed (Stage B); was 530 before. Extends downward so the dye list isn't squeezed.
-- Drives `dynamicHeight` on live + liveWithGallery view specs.
Selectors:Register("view.live.height", {
    reads = { "session.ui.live.slots", "account.live.recentDyes" },
    fn    = function(state)
        local slots = state.session.ui.live.slots
        local numSlots = slots and #slots or 0
        local recentH = #state.account.live.recentDyes > 0 and 32 or 0
        return 564 + (numSlots * 48) + recentH  -- actionBar 22->34 (+12); slot 50->44 (label moved inside)
    end,
})
