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=require(Q.src.utils.Logger)local j=((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 E=require(Q.Roact)local x=require(Q.M)local F=require(j.Components.ShopItem)local J=require(j.Components.common.ScrollingFrame)local S=require(j.Components.common.RoundButton)local k=require(j.Components.common.Frame)local i=require(Q.src.utils.SupportLibrary)local g=require(j.Components.common.UIPadding)local X=require(j.Components.EquipStatusCell)local L=require(Q.src.objects.InventoryObjects)local h=L.OBJECT_TYPES local v=E.createElement local b=E.Component:extend("\083\104\111\112")function b.init(q)q:setState({activeTab=nil})local Q=q.props local R=Q.items local j=Q.tabs if j and R then local function Q(q)local Q=x.countf(R,function(Q)return Q.type==q end)return Q>0 end local E=x.select(j,Q)q:setState({activeTab=(x.sort(E))[1]})end end function b.render(q)local Q=q.props local R=Q.tabs local j=Q.inventory local L=Q.closeClick local b=q.state.activeTab local w=v(S,{icon="\099\108\111\115\101";onClicked=L;Size=UDim2.new(.08,0,.08,0)})local f={}local function D(R)local function j(q)return q.type==R end local E=x.countf(Q.items,j)if E>0 then function OnClick()q:setState({activeTab=R})end local Q=b==R local j=v(S,{Text=R,Position=UDim2.new(.5,0,1,0),AnchorPoint=Vector2.new(.5,0),onClicked=OnClick;BackgroundColor3=Q and Color3.fromRGB(158,46,238)or nil;TextColor3=Q and Color3.fromRGB(255,255,255)or Color3.fromRGB(205,203,206),Size=UDim2.new(.2,0,1,0)})f[R]=j end end x.each(R,D)local function Y(q)return q.type==b end local T=x.filter(Q.items,Y)local W=b==h.PET local C={UIPadding=v(g,{padding=10}),W and v(X)or nil}local G=.2 local U=.2 local K=.2 local O=.2 for q,R in ipairs(T)do local E=false if R.toolObjectId and not j[R.toolObjectId]then E=true end C[R.id]=v(F,i.Merge({index=q,cellWidth=G;buttonHeight=U,titleHeight=K;item=R,isDisabled=E},Q or{}))end local u=v(J,{Layout="\076\105\115\116",CanvasSize="\087\082\065\080\095\067\079\078\084\069\078\084";LayoutDirection="\072\111\114\105\122\111\110\116\097\108",SortOrder=Enum.SortOrder.Name,HorizontalAlignment=Enum.HorizontalAlignment.Center,VerticalAlignment=Enum.VerticalAlignment.Center;ScrollBarThickness=4;ScrollBarImageTransparency=.6;VerticalScrollBarInset="\083\099\114\111\108\108\066\097\114";LayoutOrder=1,ElasticBehavior=Enum.ElasticBehavior.Always,Size=UDim2.new(1,0,.08,0);Padding=UDim.new(0,10);[E.Children]=f})local t=v(J,{Layout="\071\114\105\100",CanvasSize="\087\082\065\080\095\067\079\078\084\069\078\084",ScrollingDirection=Enum.ScrollingDirection.Y,SortOrder=Enum.SortOrder.Name;CellSize=UDim2.new(G,0,O,0);CellPadding=UDim2.new(.05,0,.05,0);HorizontalAlignment=Enum.HorizontalAlignment.Left;VerticalAlignment=Enum.VerticalAlignment.Top,ScrollBarThickness=4;ScrollBarImageTransparency=.6,VerticalScrollBarInset="\083\099\114\111\108\108\066\097\114";LayoutOrder=2,ElasticBehavior=Enum.ElasticBehavior.Always;Size=UDim2.new(1,0,.92,0),Position=UDim2.new(0,0,.08,0);[E.Children]=C})return v(k,{Size=UDim2.new(1,0,1,0)},{closeButton=w,tabsContainer=u;itemsContainer=t})end return b
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 Roact = require(Modules.Roact)
local M = require(Modules.M)
local ShopItem = require(clientSrc.Components.ShopItem)
local ScrollingFrame = require(clientSrc.Components.common.ScrollingFrame)
local RoundButton = require(clientSrc.Components.common.RoundButton)
local Frame = require(clientSrc.Components.common.Frame)
local Support = require(Modules.src.utils.SupportLibrary)
local UIPadding = require(clientSrc.Components.common.UIPadding)
local EquipStatusCell = require(clientSrc.Components.EquipStatusCell)
local InventoryObjects = require(Modules.src.objects.InventoryObjects)
local OBJECT_TYPES = InventoryObjects.OBJECT_TYPES

local createElement = Roact.createElement

local Shop = Roact.Component:extend('Shop')

function Shop:init()
	self:setState({ activeTab = nil })

	local props = self.props
	local items = props.items
	local tabs = props.tabs

	if tabs and items then
		local function findFirstNotEmpty(tabName)
			local itemsCount = M.countf(items, function(item)
				return item.type == tabName
			end)

			return itemsCount > 0
		end

		local firstTab = M.select(tabs, findFirstNotEmpty)

		self:setState({ activeTab = M.sort(firstTab)[1] })
	end
end

function Shop:render()
	local props = self.props
	local tabs = props.tabs
	local inventory = props.inventory
	local closeClick = props.closeClick
	local activeTab = self.state.activeTab

	local closeButton = createElement(RoundButton, {
		icon = 'close',
		onClicked = closeClick,
		Size = UDim2.new(0.08, 0, 0.08, 0),
	})

	local shopTabs = {}

	local function createTab(tabName)
		local function hasType(item)
			return item.type == tabName
		end

		local itemsCount = M.countf(props.items, hasType)

		if itemsCount > 0 then
			function OnClick()
				self:setState({ activeTab = tabName })
			end

			local isActive = activeTab == tabName

			local button = createElement(RoundButton, {
				Text = tabName,
				Position = UDim2.new(0.5, 0, 1, 0),
				AnchorPoint = Vector2.new(0.5, 0),
				onClicked = OnClick,
				BackgroundColor3 = isActive and Color3.fromRGB(158, 46, 238) or nil,
				TextColor3 = isActive and Color3.fromRGB(
					255,
					255,
					255
				) or Color3.fromRGB(205, 203, 206),
				Size = UDim2.new(0.2, 0, 1, 0),
			})

			shopTabs[tabName] = button
		end
	end

	M.each(tabs, createTab)

	local function isVisible(item)
		return item.type == activeTab
	end

	local itemList = M.filter(props.items, isVisible)

	local isPetTab = activeTab == OBJECT_TYPES.PET
	local shopItems = {
		UIPadding = createElement(UIPadding, { padding = 10 }),
		isPetTab and createElement(EquipStatusCell) or nil,
	}

	local cellWidth = 0.2
	local buttonHeight = 0.2
	local titleHeight = 0.2
	local cellHeight = 0.2

	for index, item in ipairs(itemList) do
		local isDisabled = false
		if item.toolObjectId and not inventory[item.toolObjectId] then
			isDisabled = true
		end

		shopItems[item.id] = createElement(
			ShopItem,
			Support.Merge(
				{
					index = index,
					cellWidth = cellWidth,
					buttonHeight = buttonHeight,
					titleHeight = titleHeight,
					item = item,
					isDisabled = isDisabled,
				},
				props or {}
			)
		)
	end

	local shopTabsContainer = createElement(ScrollingFrame, {
		Layout = 'List',
		CanvasSize = 'WRAP_CONTENT',
		LayoutDirection = 'Horizontal',
		SortOrder = Enum.SortOrder.Name,
		HorizontalAlignment = Enum.HorizontalAlignment.Center,
		VerticalAlignment = Enum.VerticalAlignment.Center,
		ScrollBarThickness = 4,
		ScrollBarImageTransparency = 0.6,
		VerticalScrollBarInset = 'ScrollBar',
		LayoutOrder = 1,
		ElasticBehavior = Enum.ElasticBehavior.Always,
		Size = UDim2.new(1, 0, 0.08, 0),
		Padding = UDim.new(0, 10),
		[Roact.Children] = shopTabs,
	})

	local shopItemsContainer = createElement(ScrollingFrame, {
		Layout = 'Grid',
		CanvasSize = 'WRAP_CONTENT',
		ScrollingDirection = Enum.ScrollingDirection.Y,
		SortOrder = Enum.SortOrder.Name,
		CellSize = UDim2.new(cellWidth, 0, cellHeight, 0),
		CellPadding = UDim2.new(0.05, 0, 0.05, 0),
		HorizontalAlignment = Enum.HorizontalAlignment.Left,
		VerticalAlignment = Enum.VerticalAlignment.Top,
		ScrollBarThickness = 4,
		ScrollBarImageTransparency = 0.6,
		VerticalScrollBarInset = 'ScrollBar',
		LayoutOrder = 2,
		ElasticBehavior = Enum.ElasticBehavior.Always,
		Size = UDim2.new(1, 0, 0.92, 0),
		Position = UDim2.new(0, 0, 0.08, 0),
		[Roact.Children] = shopItems,
	})

	return createElement(
		Frame,
		{ Size = UDim2.new(1, 0, 1, 0) },
		{
			closeButton = closeButton,
			tabsContainer = shopTabsContainer,
			itemsContainer = shopItemsContainer,
		}
	)
end

return Shop