local q=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local Q=q:WaitForChild("\077\111\100\117\108\101\115")local R=((game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")):WaitForChild("\083\116\097\114\116\101\114\080\108\097\121\101\114\083\099\114\105\112\116\115")).clientSrc local j=require(Q.src.utils.SupportLibrary)local E=game:GetService("\067\111\110\116\101\120\116\065\099\116\105\111\110\083\101\114\118\105\099\101")local x=require(Q.Roact)local F=require(Q.RoactRodux)local J=require(Q.M)local S=require(R.Components.Shop)local k=require(R.Components.common.UICorner)local i=require(R.Components.common.RoundButton)local g=require(R.Components.common.Frame)local X=require(R.getApiFromComponent)local L=require(Q.src.objects.InventoryObjects)local h=L.OBJECT_TYPES local v=x.createElement local b=x.PureComponent:extend("\073\110\118\101\110\116\111\114\121\065\110\100\083\104\111\112\066\117\116\116\111\110\115")function b.init(q)q.state={shopOpen=false,inventoryOpen=false}q.api=X(q)end function b.render(q)local Q=q.props local R=Q.isPlaying local E=Q.inventory if R then return end local x=function()q:setState(function(q)return{inventoryOpen=not q.inventoryOpen}end)end local F=function()q:setState(function(q)return{shopOpen=not q.shopOpen}end)end local J=v(i,{icon="\105\110\098\111\120",onClicked=x})local X=v(i,{icon="\115\104\111\112",onClicked=F})if not q.state.shopOpen and not q.state.inventoryOpen then return v(g,{Layout="\076\105\115\116",LayoutDirection="\086\101\114\116\105\099\097\108";Padding=UDim.new(0,20);Size=UDim2.new(0,50,0,100),Position=UDim2.new(0,0,0,20)},{X;J})end local L=function()q:setState(function()return{inventoryOpen=false;shopOpen=false}end)end local b={tabs=h;closeClick=L,equippedItems=q.props.equippedItems;isGhosting=q.props.isGhosting,inventory=E,buyItem=function(Q)q.api:buyItem(Q)end,startGhosting=function()q.api:startGhosting()end;stopGhosting=function()q.api:stopGhosting()end,equipItem=function(Q)q.api:equipItem(Q)end,unequipItem=function(Q)q.api:unequipItem(Q)end}return v(g,{BackgroundColor3=Color3.fromRGB(179,216,236);BackgroundTransparency=.1},{UICorner=v(k);Inventory=v(S,j.Merge({items=q.state.inventoryOpen and E or q.props.items},b))})end function b.didMount(q)local function Q(Q,R)if R==Enum.UserInputState.Begin then q:setState(function(q)return{shopOpen=not q.shopOpen,inventoryOpen=false}end)end end local function R(Q,R)if R==Enum.UserInputState.Begin then q:setState(function(q)return{inventoryOpen=not q.inventoryOpen;shopOpen=false}end)end end E:BindAction("\111\112\101\110\083\104\111\112",Q,false,Enum.KeyCode.Q)E:BindAction("\111\112\101\110\073\110\118\101\110\116\111\114\121",R,false,Enum.KeyCode.R)end local w=(F.connect(function(q)local function Q(q)return q.type~=h.ROOM end local function R(q)return q.id,q end return{items=q.shop.items,equippedItems=q.player.equippedItems,isPlaying=q.player.isPlaying,playerSlotsCount=q.player.playerSlotsCount;isGhosting=q.player.isGhosting;inventory=J.map(J.filter(q.inventory,Q),R)}end))(b)return w
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
-- local logger = require(Modules.src.utils.Logger)
local clientSrc = game:GetService('StarterPlayer'):WaitForChild('StarterPlayerScripts').clientSrc
local Support = require(Modules.src.utils.SupportLibrary)
local ContextActionService = game:GetService('ContextActionService')

local Roact = require(Modules.Roact)
local RoactRodux = require(Modules.RoactRodux)
local M = require(Modules.M)
local Shop = require(clientSrc.Components.Shop)
local UICorner = require(clientSrc.Components.common.UICorner)

local RoundButton = require(clientSrc.Components.common.RoundButton)
local Frame = require(clientSrc.Components.common.Frame)

local getApiFromComponent = require(clientSrc.getApiFromComponent)

local InventoryObjects = require(Modules.src.objects.InventoryObjects)
local OBJECT_TYPES = InventoryObjects.OBJECT_TYPES

local createElement = Roact.createElement

local InventoryAndShopButtons = Roact.PureComponent:extend('InventoryAndShopButtons')

function InventoryAndShopButtons:init()
	self.state = {
		shopOpen = false,
		inventoryOpen = false,
	}

	self.api = getApiFromComponent(self)
end

function InventoryAndShopButtons:render()
	local props = self.props
	local isPlaying = props.isPlaying
	local inventory = props.inventory

	if isPlaying then return end

	local toggleInventory = function()
		self:setState(function(prevState)
			return { inventoryOpen = not prevState.inventoryOpen }
		end)
	end
	local toggleShop = function()
		self:setState(function(prevState)
			return { shopOpen = not prevState.shopOpen }
		end)
	end

	local inventoryButton = createElement(RoundButton, {
		icon = 'inbox',
		onClicked = toggleInventory,
	})
	local shopButton = createElement(RoundButton, {
		icon = 'shop',
		onClicked = toggleShop,
	})

	if not self.state.shopOpen and not self.state.inventoryOpen then
		return createElement(
			Frame,
			{
				Layout = 'List',
				LayoutDirection = 'Vertical',
				Padding = UDim.new(0, 20),
				Size = UDim2.new(0, 50, 0, 100),
				Position = UDim2.new(0, 0, 0, 20),
			},
			{ shopButton, inventoryButton }
		)
	end

	local closeInventoryAndShop = function()
		self:setState(function()
			return {
				inventoryOpen = false,
				shopOpen = false,
			}
		end)
	end

	local shopProps = {
		tabs = OBJECT_TYPES,
		closeClick = closeInventoryAndShop,
		equippedItems = self.props.equippedItems,
		isGhosting = self.props.isGhosting,
		inventory = inventory,
		buyItem = function(itemId)
			self.api:buyItem(itemId)
		end,
		startGhosting = function()
			self.api:startGhosting()
		end,
		stopGhosting = function()
			self.api:stopGhosting()
		end,
		equipItem = function(itemId)
			self.api:equipItem(itemId)
		end,
		unequipItem = function(itemId)
			self.api:unequipItem(itemId)
		end,
	}

	return createElement(
		Frame,
		{
			BackgroundColor3 = Color3.fromRGB(179, 216, 236),
			BackgroundTransparency = 0.1,
		},
		{
			UICorner = createElement(UICorner),
			Inventory = createElement(
				Shop,

				Support.Merge(
					{ items = self.state.inventoryOpen and inventory or self.props.items },
					shopProps
				)
			),
		}
	)
end

function InventoryAndShopButtons:didMount()
	local function openShop(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			self:setState(function(state)
				return {
					shopOpen = not state.shopOpen,
					inventoryOpen = false,
				}
			end)
		end
	end

	local function openInventory(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			self:setState(function(state)
				return {
					inventoryOpen = not state.inventoryOpen,
					shopOpen = false,
				}
			end)
		end
	end

	ContextActionService:BindAction('openShop', openShop, false, Enum.KeyCode.Q)

	ContextActionService:BindAction('openInventory', openInventory, false, Enum.KeyCode.R)
end

local InventoryAndShopButtonsConnected = RoactRodux.connect(function(state)
	local function isVisible(item)
		return item.type ~= OBJECT_TYPES.ROOM
	end
	local function byId(item)
		return item.id, item
	end

	return {
		items = state.shop.items,
		equippedItems = state.player.equippedItems,
		isPlaying = state.player.isPlaying,
		playerSlotsCount = state.player.playerSlotsCount,
		isGhosting = state.player.isGhosting,
		inventory = M.map(M.filter(state.inventory, isVisible), byId),
	}
end)(InventoryAndShopButtons)

return InventoryAndShopButtonsConnected