local A=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local p=A:WaitForChild("\077\111\100\117\108\101\115")local f=require(p.Roact)local R=require(p.src.utils.SupportLibrary)local u=f.createElement local K=f.PureComponent:extend("\083\099\114\111\108\108\105\110\103\070\114\097\109\101")K.defaultProps={BackgroundTransparency=1,BorderSizePixel=0,Size=UDim2.new(1,0,1,0);CanvasSize=UDim2.new(1,0,1,0)}function K.render(A)local p=R.CloneTable(A.props)if p.AspectRatio then local A=u("\085\073\065\115\112\101\099\116\082\097\116\105\111\067\111\110\115\116\114\097\105\110\116",{AspectRatio=p.AspectRatio})p[f.Children]=R.Merge({AspectRatio=A},p[f.Children]or{})end if p.Layout=="\076\105\115\116"then local K=u("\085\073\076\105\115\116\076\097\121\111\117\116",{FillDirection=p.LayoutDirection;Padding=p.Padding,HorizontalAlignment=p.HorizontalAlignment,VerticalAlignment=p.VerticalAlignment,SortOrder=p.SortOrder or"\076\097\121\111\117\116\079\114\100\101\114";[f.Ref]=function(p)A:UpdateContentSize(p)end,[f.Change.AbsoluteContentSize]=function(p)A:UpdateContentSize(p)end})p.Size=A:GetSize()p.CanvasSize=A:GetCanvasSize()p[f.Children]=R.Merge({Layout=K},p[f.Children]or{})end if p.Layout=="\071\114\105\100"then local K=u("\085\073\071\114\105\100\076\097\121\111\117\116",{FillDirection=p.LayoutDirection;SortOrder=p.SortOrder or"\076\097\121\111\117\116\079\114\100\101\114";HorizontalAlignment=p.HorizontalAlignment or Enum.HorizontalAlignment.Center;VerticalAlignment=p.VerticalAlignment or Enum.VerticalAlignment.Top;CellSize=p.CellSize;CellPadding=p.CellPadding,[f.Ref]=function(p)A:UpdateContentSize(p)end;[f.Change.AbsoluteContentSize]=function(p)A:UpdateContentSize(p)end})p.Size=A:GetSize()p.CanvasSize=A:GetCanvasSize()p[f.Children]=R.Merge({Layout=K},p[f.Children]or{})end if p.Layout=="\084\097\098\108\101"then local K=u("\085\073\084\097\098\108\101\076\097\121\111\117\116",{FillDirection=p.LayoutDirection,FillEmptySpaceColumns=true;Padding=p.Padding,HorizontalAlignment=p.HorizontalAlignment;VerticalAlignment=p.VerticalAlignment;SortOrder=p.SortOrder or"\076\097\121\111\117\116\079\114\100\101\114";[f.Ref]=function(p)A:UpdateContentSize(p)end,[f.Change.AbsoluteContentSize]=function(p)A:UpdateContentSize(p)end})p.Size=A:GetSize()p.CanvasSize=A:GetCanvasSize()p[f.Children]=R.Merge({Layout=K},p[f.Children]or{})end p.AspectRatio=nil p.Layout=nil p.LayoutDirection=nil p.HorizontalAlignment=nil p.VerticalAlignment=nil p.SortOrder=nil p.Width=nil p.Height=nil p.CanvasWidth=nil p.CanvasHeight=nil p.CellSize=nil p.CellPadding=nil p.Padding=nil return u("\083\099\114\111\108\108\105\110\103\070\114\097\109\101",p)end function K.GetSize(p,A)local f=p.props local R=f.Size=="\087\082\065\080\095\067\079\078\084\069\078\084"or f.Width=="\087\082\065\080\095\067\079\078\084\069\078\084"local u=f.Size=="\087\082\065\080\095\067\079\078\084\069\078\084"or f.Height=="\087\082\065\080\095\067\079\078\084\069\078\084"local K=R or u return UDim2.new((A and R)and UDim.new(0,A.X)or typeof(f.Width)=="\085\068\105\109"and f.Width or f.Size.X,(A and u)and UDim.new(0,A.Y)or typeof(f.Height)=="\085\068\105\109"and f.Height or f.Size.Y)end function K.GetCanvasSize(p,A)local f=p.props local R=f.CanvasSize=="\087\082\065\080\095\067\079\078\084\069\078\084"or f.CanvasWidth=="\087\082\065\080\095\067\079\078\084\069\078\084"local u=f.CanvasSize=="\087\082\065\080\095\067\079\078\084\069\078\084"or f.CanvasHeight=="\087\082\065\080\095\067\079\078\084\069\078\084"local K=R or u return UDim2.new((A and R)and UDim.new(0,A.X)or typeof(f.CanvasWidth)=="\085\068\105\109"and f.CanvasWidth or f.CanvasSize.X,(A and u)and UDim.new(0,A.Y)or typeof(f.CanvasHeight)=="\085\068\105\109"and f.CanvasHeight or f.CanvasSize.Y)end function K.UpdateContentSize(p,A)if not(A and A.Parent)then return end A.Parent.Size=p:GetSize(A.AbsoluteContentSize)A.Parent.CanvasSize=p:GetCanvasSize(A.AbsoluteContentSize)end return K
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
-- local logger = require(Modules.src.utils.Logger)

local Roact = require(Modules.Roact)
local Support = require(Modules.src.utils.SupportLibrary)
-- Roact
local createElement = Roact.createElement

-- Create component
local ScrollingFrame = Roact.PureComponent:extend'ScrollingFrame'

-- Set defaults
ScrollingFrame.defaultProps = {
	BackgroundTransparency = 1,
	BorderSizePixel = 0,
	Size = UDim2.new(1, 0, 1, 0),
	CanvasSize = UDim2.new(1, 0, 1, 0),
}

function ScrollingFrame:render()
	local props = Support.CloneTable(self.props)

	-- Include aspect ratio constraint if specified
	if props.AspectRatio then
		local Constraint =
			createElement('UIAspectRatioConstraint', { AspectRatio = props.AspectRatio })

		-- Insert constraint into children
		props[Roact.Children] =
			Support.Merge({ AspectRatio = Constraint }, props[Roact.Children] or {})
	end

	-- Include list layout if specified
	if props.Layout == 'List' then
		local Layout = createElement('UIListLayout', {
			FillDirection = props.LayoutDirection,
			Padding = props.Padding,
			HorizontalAlignment = props.HorizontalAlignment,
			VerticalAlignment = props.VerticalAlignment,
			SortOrder = props.SortOrder or 'LayoutOrder',
			[Roact.Ref] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
			[Roact.Change.AbsoluteContentSize] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
		})

		-- Update size
		props.Size = self:GetSize()
		props.CanvasSize = self:GetCanvasSize()

		-- Insert layout into children
		props[Roact.Children] = Support.Merge({ Layout = Layout }, props[Roact.Children] or {})
	end

	if props.Layout == 'Grid' then
		local Layout = createElement('UIGridLayout', {
			FillDirection = props.LayoutDirection,
			SortOrder = props.SortOrder or 'LayoutOrder',
			HorizontalAlignment = props.HorizontalAlignment or Enum.HorizontalAlignment.Center,
			VerticalAlignment = props.VerticalAlignment or Enum.VerticalAlignment.Top,
			CellSize = props.CellSize,
			CellPadding = props.CellPadding,
			[Roact.Ref] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
			[Roact.Change.AbsoluteContentSize] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
		})

		-- Update size
		props.Size = self:GetSize()
		props.CanvasSize = self:GetCanvasSize()

		-- Insert layout into children
		props[Roact.Children] = Support.Merge({ Layout = Layout }, props[Roact.Children] or {})
	end

	if props.Layout == 'Table' then
		local Layout = createElement('UITableLayout', {
			FillDirection = props.LayoutDirection,
			FillEmptySpaceColumns= true,
			Padding = props.Padding,
			HorizontalAlignment = props.HorizontalAlignment,
			VerticalAlignment = props.VerticalAlignment,
			SortOrder = props.SortOrder or 'LayoutOrder',
			[Roact.Ref] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
			[Roact.Change.AbsoluteContentSize] = function(rbx)
				self:UpdateContentSize(rbx)
			end,
		})

		-- Update size
		props.Size = self:GetSize()
		props.CanvasSize = self:GetCanvasSize()

		-- Insert layout into children
		props[Roact.Children] = Support.Merge({ Layout = Layout }, props[Roact.Children] or {})
	end

	-- Filter out custom properties
	props.AspectRatio = nil
	props.Layout = nil
	props.LayoutDirection = nil
	props.HorizontalAlignment = nil
	props.VerticalAlignment = nil
	props.SortOrder = nil
	props.Width = nil
	props.Height = nil
	props.CanvasWidth = nil
	props.CanvasHeight = nil
	props.CellSize = nil
	props.CellPadding = nil
	props.Padding = nil

	-- Display component in wrapper

	return createElement('ScrollingFrame', props)
end

function ScrollingFrame:GetSize(ContentSize)
	local props = self.props

	-- Determine dynamic dimensions
	local DynamicWidth = props.Size == 'WRAP_CONTENT' or props.Width == 'WRAP_CONTENT'
	local DynamicHeight = props.Size == 'WRAP_CONTENT' or props.Height == 'WRAP_CONTENT'
	local DynamicSize = DynamicWidth or DynamicHeight

	-- Calculate size based on content if dynamic
	return UDim2.new(
		(ContentSize and DynamicWidth) and UDim.new(0, ContentSize.X) or (typeof(
			props.Width
		) == 'UDim' and props.Width or props.Size.X),
		(ContentSize and DynamicHeight) and UDim.new(0, ContentSize.Y) or (typeof(
			props.Height
		) == 'UDim' and props.Height or props.Size.Y)
	)
end

function ScrollingFrame:GetCanvasSize(ContentSize)
	local props = self.props

	-- Determine dynamic canvas dimensions
	local DynamicCanvasWidth =
		props.CanvasSize == 'WRAP_CONTENT' or props.CanvasWidth == 'WRAP_CONTENT'
	local DynamicCanvasHeight =
		props.CanvasSize == 'WRAP_CONTENT' or props.CanvasHeight == 'WRAP_CONTENT'
	local DynamicCanvasSize = DynamicCanvasWidth or DynamicCanvasHeight

	-- Calculate size based on content if dynamic
	return UDim2.new(
		(ContentSize and DynamicCanvasWidth) and UDim.new(0, ContentSize.X) or (typeof(
			props.CanvasWidth
		) == 'UDim' and props.CanvasWidth or props.CanvasSize.X),
		(ContentSize and DynamicCanvasHeight) and UDim.new(0, ContentSize.Y) or (typeof(
			props.CanvasHeight
		) == 'UDim' and props.CanvasHeight or props.CanvasSize.Y)
	)
end

function ScrollingFrame:UpdateContentSize(Layout)
	if not (Layout and Layout.Parent) then return end

	-- Set container size based on content
	Layout.Parent.Size = self:GetSize(Layout.AbsoluteContentSize)
	Layout.Parent.CanvasSize = self:GetCanvasSize(Layout.AbsoluteContentSize)
end

return ScrollingFrame