-- Reducer that holds various UI element states, such as the visibility of some components



local MerchBooth = script:FindFirstAncestor("MerchBooth")

local Rodux = require(MerchBooth.Packages.Rodux)
local Cryo = require(MerchBooth.Packages.Cryo)
local types = require(MerchBooth.types)
local setViewedItem = require(MerchBooth.Actions.setViewedItem)
local removeItemInfo = require(MerchBooth.Actions.removeItemInfo)
local setMerchBoothVisible = require(MerchBooth.Actions.setMerchBoothVisible)
local setMerchBoothFocused = require(MerchBooth.Actions.setMerchBoothFocused)
local setCatalogButtonVisible = require(MerchBooth.Actions.setCatalogButtonVisible)
local setCatalogFilter = require(MerchBooth.Actions.setCatalogFilter)
local setCatalogSortFunction = require(MerchBooth.Actions.setCatalogSortFunction)
local setEquipModalVisible = require(MerchBooth.Actions.setEquipModalVisible)
local setMerchBoothEnabled = require(MerchBooth.Actions.setMerchBoothEnabled)

local initialState = {
	merchBoothOpen = false,
	isMerchBoothFocused = false,
	catalogButtonVisible = true,
	catalogSortFunction = function(a: types.ItemInfo, b: types.ItemInfo)
		return a.title < b.title
	end,
	isEquipModalShown = false,
	isMerchBoothEnabled = false,
}

export type State = typeof(initialState) & {
	viewedItemId: number?,
	catalogFilter: types.ProductType?,
}

local ui = Rodux.createReducer(initialState, {
	[setMerchBoothEnabled.name] = function(state: State, action: setMerchBoothEnabled.Action)
		return Cryo.Dictionary.join(state, {
			isMerchBoothEnabled = action.isEnabled,
		})
	end,

	[setViewedItem.name] = function(state: State, action: setViewedItem.Action)
		return Cryo.Dictionary.join(state, {
			viewedItemId = action.assetId or Cryo.None,
		})
	end,

	[removeItemInfo.name] = function(state: State, action: removeItemInfo.Action)
		-- Kicks the user from item view if what they're viewing is being removed
		if state.viewedItemId == tonumber(action.itemId) then
			return Cryo.Dictionary.join(state, {
				viewedItemId = Cryo.None,
			})
		end
		return state
	end,

	[setMerchBoothVisible.name] = function(state: State, action: setMerchBoothVisible.Action)
		if action.visible then
			return Cryo.Dictionary.join(state, {
				merchBoothOpen = true,
				isMerchBoothFocused = true,
			})
		else
			return Cryo.Dictionary.join(state, {
				merchBoothOpen = false,
				isMerchBoothFocused = false,
				catalogFilter = Cryo.None, -- Reset the current filter
				viewedItemId = Cryo.None, -- Resets to catalog view when closing Merch Booth
			})
		end
	end,

	[setMerchBoothFocused.name] = function(state: State, action: setMerchBoothFocused.Action)
		return Cryo.Dictionary.join(state, {
			isMerchBoothFocused = action.isFocused,
		})
	end,

	[setCatalogButtonVisible.name] = function(state: State, action: setCatalogButtonVisible.Action)
		return Cryo.Dictionary.join(state, {
			catalogButtonVisible = action.visible,
		})
	end,

	[setEquipModalVisible.name] = function(state: State, action: setEquipModalVisible.Action)
		return Cryo.Dictionary.join(state, {
			isEquipModalShown = action.visible,
		})
	end,

	[setCatalogFilter.name] = function(state: State, action: setCatalogFilter.Action)
		return Cryo.Dictionary.join(state, {
			catalogFilter = action.filter,
		})
	end,

	[setCatalogSortFunction.name] = function(state: State, action: setCatalogSortFunction.Action)
		return Cryo.Dictionary.join(state, {
			catalogSortFunction = action.sort,
		})
	end,
})

return ui