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(R.Components.common.TextLabel)local E=require(R.Components.common.ModelViewport)local x=require(Q.src.utils.Logger)local F=require(Q.M)local J=require(Q.src.objects.InventoryObjects)local S=require(Q.src.ModelManager)local k=require(R.Components.common.RoundButton)local i=require(R.Components.common.UICorner)local g=require(R.Components.common.Frame)local X=J.OBJECT_TYPES local L=require(Q.src.GamePasses)local h=require(Q.src.DeveloperProducts)local v=require(Q.Roact)local b=require(Q.RoactMaterial)local w=v.createElement local f=v.PureComponent:extend("\083\104\111\112\073\116\101\109")function f.render(q)local Q=q.props local R=q.props.item local J=q.props.isDisabled local v=q.props.cellWidth local f=q.props.buttonHeight local D=q.props.titleHeight local Y=Q.equippedItems local T=Q.inventory local W=F.find(Y,R.id)~=nil local C=T[R.id]~=nil local G=Q.isGhosting local U=Q.buyItem local K=Q.equipItem local O=Q.unequipItem local u=Q.startGhosting local t=Q.stopGhosting local n=function()if R.isDeveloperProduct then h:promptPurchase(R.id)elseif R.isGamePass then if not C then L:promptPurchase(R.id)elseif R.isGhost and G then t()elseif R.isGhost then u()end elseif W then O(R.id)elseif C then K(R.id)else U(R.id)end end if J then n=nil end local p local P if R.modelName and(R.modelFolder and not R.icon)then local q=S:findModel(R)if not q then x:e("\078\111 \109\111\100\101\108 \102\111\117\110\100 \119\105\116\104 \110\097\109\101\058 "..R.modelName)else p=w(E,{Position=UDim2.new(.5,0,.5,0),Size=UDim2.new(1,0,1,0);model=q:Clone();Visible=true;BackgroundTransparency=1,cameraOffset=R.cameraOffset;isRotating=R.isRotating})end else P=w("\085\073\065\115\112\101\099\116\082\097\116\105\111\067\111\110\115\116\114\097\105\110\116")p=w("\073\109\097\103\101\066\117\116\116\111\110",{Position=UDim2.new(.5,0,.5,0),Size=UDim2.new(1,0,1,0),AnchorPoint=Vector2.new(.5,.5);Image=R.icon;BackgroundTransparency=1,ZIndex=2})end local I=w(j,{AnchorPoint=Vector2.new(.5,1.0);Position=UDim2.new(.5,0,1,0),Size=UDim2.new(1,0,D,0);TextScaled=true,BackgroundColor3=Color3.new(0,0,0);BackgroundTransparency=.5,Font=Enum.Font.Cartoon;TextColor3=Color3.new(.85,.85,.85),TextXAlignment=Enum.TextXAlignment.Center,TextYAlignment=Enum.TextYAlignment.Center,TextSize=18;Text=R.name,LayoutOrder=0},{UICorner=w(i)})local B="\046\046"if R.type==X.PET then if W then B="\069\113\117\105\112\112\101\100 \040\088\041"elseif C then B="\069\113\117\105\112"elseif R.price then B=R.price.." \067\111\105\110\115"end elseif R.type==X.COLLECTABLE then B="\067\111\108\108\101\099\116\101\100"elseif R.type==X.COIN_PACK then if R.price then B=R.price.." \082\111\098\117\120"end elseif R.type==X.GAME_PASS then if R.isGhost and G then B="\083\116\111\112 \103\104\111\115\116\105\110\103"elseif R.isGhost and C then B="\083\116\097\114\116 \103\104\111\115\116\105\110\103"elseif R.isGamePass and C then B="\079\119\110\101\100"elseif R.price then B=R.price.." \082\111\098\117\120"end else x:w("\078\111 \116\121\112\101 \102\111\117\110\100 \102\111\114 \105\116\101\109",R)end if J then B="\066\117\121 \116\111\111\108 \102\105\114\115\116"end local d if R.amount then local q=R.amount==0 local Q=q and Color3.fromRGB(199,0,0)or Color3.fromRGB(255,255,255)d=w(j,{Position=UDim2.new(0,-5,0,5);Size=UDim2.new(1,0,0,20);TextScaled=true;Font=Enum.Font.Cartoon;TextColor3=Q,TextXAlignment=Enum.TextXAlignment.Right,TextYAlignment=Enum.TextYAlignment.Top;TextSize=12;Text=q and"\079\117\116 \079\102 \083\116\111\099\107"or R.amount.."\120"})end local l=w("\070\114\097\109\101",{BackgroundTransparency=1,Size=UDim2.new(.6,0,.6,0),LayoutOrder=1},{image=p;aspect=P;amountLabel=d})local c={name=I;imageLabel=l}local y={Text=B;Position=UDim2.new(.5,0,.5,0),TextScaled=true;AnchorPoint=Vector2.new(.5,.5),Size=UDim2.new(1,0,f,0);onClicked=n,BackgroundColor3=b.Colors.Blue500;LayoutOrder=2}if R.isGamePass and C then y=F.extend({},y,{BackgroundColor3=b.Colors.Grey400,onClick=nil})elseif W then y=F.extend({},y,{BackgroundColor3=b.Colors.Blue500})elseif C then y=F.extend({},y,{BackgroundColor3=b.Colors.Grey400})end if J then y=F.extend({},y,{BackgroundColor3=b.Colors.Grey400})end c.button=w(k,y)c.aspect=w("\085\073\065\115\112\101\099\116\082\097\116\105\111\067\111\110\115\116\114\097\105\110\116")return w(g,{Layout="\076\105\115\116",Padding=UDim.new(0,0),BackgroundTransparency=.7;BackgroundColor3=Color3.fromRGB(255,255,255);HorizontalAlignment=Enum.HorizontalAlignment.Center},c)end return f
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')

local clientSrc = game:GetService('StarterPlayer'):WaitForChild('StarterPlayerScripts').clientSrc
local TextLabel = require(clientSrc.Components.common.TextLabel)
local ModelViewport = require(clientSrc.Components.common.ModelViewport)
local logger = require(Modules.src.utils.Logger)
local M = require(Modules.M)
local InventoryObjects = require(Modules.src.objects.InventoryObjects)
local ModelManager = require(Modules.src.ModelManager)
local RoundButton = require(clientSrc.Components.common.RoundButton)
local UICorner = require(clientSrc.Components.common.UICorner)
local Frame = require(clientSrc.Components.common.Frame)

local OBJECT_TYPES = InventoryObjects.OBJECT_TYPES
local GamePasses = require(Modules.src.GamePasses)
local DeveloperProducts = require(Modules.src.DeveloperProducts)
local Roact = require(Modules.Roact)
local RoactMaterial = require(Modules.RoactMaterial)

local createElement = Roact.createElement

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

function ShopItem:render()
	local props = self.props
	local item = self.props.item
	local isDisabled = self.props.isDisabled
	local cellWidth = self.props.cellWidth
	local buttonHeight = self.props.buttonHeight
	local titleHeight = self.props.titleHeight

	local equippedItems = props.equippedItems
	local inventory = props.inventory
	local isEquipped = M.find(equippedItems, item.id) ~= nil
	local isOwned = inventory[item.id] ~= nil

	local isGhosting = props.isGhosting

	local buyItem = props.buyItem
	local equipItem = props.equipItem
	local unequipItem = props.unequipItem
	local startGhosting = props.startGhosting
	local stopGhosting = props.stopGhosting

	local onClick = function()
		if item.isDeveloperProduct then
			DeveloperProducts:promptPurchase(item.id)
		elseif item.isGamePass then
			if not isOwned then
				GamePasses:promptPurchase(item.id)
			elseif item.isGhost and isGhosting then
				stopGhosting()
			elseif item.isGhost then
				startGhosting()
			end
		elseif isEquipped then
			unequipItem(item.id)
		elseif isOwned then
			equipItem(item.id)
		else
			buyItem(item.id)
		end
	end

	if isDisabled then
		onClick = nil
	end

	local image
	local aspect

	if item.modelName and item.modelFolder and not item.icon then
		local model = ModelManager:findModel(item)

		if not model then
			logger:e('No model found with name: ' .. item.modelName)
		else
			image = createElement(ModelViewport, {
				Position = UDim2.new(0.5, 0, 0.5, 0),
				Size = UDim2.new(1, 0, 1, 0),
				model = model:Clone(),
				Visible = true,
				BackgroundTransparency = 1,
				cameraOffset = item.cameraOffset,
				isRotating = item.isRotating,
			})
		end
	else
		aspect = createElement('UIAspectRatioConstraint')
		image = createElement('ImageButton', {
			Position = UDim2.new(0.5, 0, 0.5, 0),
			Size = UDim2.new(1, 0, 1, 0),
			AnchorPoint = Vector2.new(0.5, 0.5),
			Image = item.icon,
			BackgroundTransparency = 1,
			ZIndex = 2,
		})
	end

	local nameLabel = createElement(
		TextLabel,
		{
			AnchorPoint = Vector2.new(0.5, 1.0),
			Position = UDim2.new(0.5, 0, 1, 0),
			Size = UDim2.new(1, 0, titleHeight, 0),
			TextScaled = true,
			BackgroundColor3 = Color3.new(0, 0, 0),
			BackgroundTransparency = 0.5,
			Font = Enum.Font.Cartoon,
			TextColor3 = Color3.new(0.85, 0.85, 0.85),
			TextXAlignment = Enum.TextXAlignment.Center,
			TextYAlignment = Enum.TextYAlignment.Center,
			TextSize = 18,
			Text = item.name,
			LayoutOrder = 0,
		},
		{ UICorner = createElement(UICorner) }
	)

	local buttonText = '..'

	if item.type == OBJECT_TYPES.PET then
		if isEquipped then
			buttonText = 'Equipped (X)'
		elseif isOwned then
			buttonText = 'Equip'
		elseif item.price then
			buttonText = item.price .. ' Coins'
		end
	elseif item.type == OBJECT_TYPES.COLLECTABLE then
		buttonText = 'Collected'
	elseif item.type == OBJECT_TYPES.COIN_PACK then
		if item.price then
			buttonText = item.price .. ' Robux'
		end
	elseif item.type == OBJECT_TYPES.GAME_PASS then
		if item.isGhost and isGhosting then
			buttonText = 'Stop ghosting'
		elseif item.isGhost and isOwned then
			buttonText = 'Start ghosting'
		elseif item.isGamePass and isOwned then
			buttonText = 'Owned'
		elseif item.price then
			buttonText = item.price .. ' Robux'
		end
	else
		logger:w('No type found for item', item)
	end

	if isDisabled then
		buttonText = 'Buy tool first'
	end

	local amountLabel
	if item.amount then
		local outOfStock = item.amount == 0
		local color = outOfStock and Color3.fromRGB(199, 0, 0) or Color3.fromRGB(255, 255, 255)

		amountLabel = createElement(TextLabel, {
			Position = UDim2.new(0, -5, 0, 5),
			Size = UDim2.new(1, 0, 0, 20),
			TextScaled = true,
			Font = Enum.Font.Cartoon,
			TextColor3 = color,
			TextXAlignment = Enum.TextXAlignment.Right,
			TextYAlignment = Enum.TextYAlignment.Top,
			TextSize = 12,
			Text = outOfStock and 'Out Of Stock' or item.amount .. 'x',
		})
	end

	local imageLabel = createElement(
		'Frame',
		{
			BackgroundTransparency = 1,
			Size = UDim2.new(0.6, 0, 0.6, 0),
			LayoutOrder = 1,
		},
		{
			image = image,
			aspect = aspect,
			amountLabel = amountLabel,
		}
	)

	local children = {
		name = nameLabel,
		imageLabel = imageLabel,
	}

	local buttonProps = {
		Text = buttonText,
		Position = UDim2.new(0.5, 0, 0.5, 0),
		TextScaled = true,
		AnchorPoint = Vector2.new(0.5, 0.5),
		Size = UDim2.new(1, 0, buttonHeight, 0),
		onClicked = onClick,
		BackgroundColor3 = RoactMaterial.Colors.Blue500,
		LayoutOrder = 2,
	}

	if item.isGamePass and isOwned then
		buttonProps = M.extend({}, buttonProps, {
			BackgroundColor3 = RoactMaterial.Colors.Grey400,
			onClick = nil,
		})
	elseif isEquipped then
		buttonProps = M.extend({}, buttonProps, { BackgroundColor3 = RoactMaterial.Colors.Blue500 })
	elseif isOwned then
		buttonProps = M.extend({}, buttonProps, { BackgroundColor3 = RoactMaterial.Colors.Grey400 })
	end

	if isDisabled then
		buttonProps = M.extend({}, buttonProps, { BackgroundColor3 = RoactMaterial.Colors.Grey400 })
	end

	children['button'] = createElement(RoundButton, buttonProps)

	children['aspect'] = createElement('UIAspectRatioConstraint')

	return createElement(
		Frame,
		{
			Layout = 'List',
			Padding = UDim.new(0, 0),
			BackgroundTransparency = 0.7,
			BackgroundColor3 = Color3.fromRGB(255, 255, 255),
			HorizontalAlignment = Enum.HorizontalAlignment.Center,
		},
		children
	)
end

return ShopItem