local n=game:GetService("\084\101\120\116\083\101\114\118\105\099\101")local Z=require((script:FindFirstAncestor("\077\097\122\101\071\101\110\101\114\097\116\111\114\080\108\117\103\105\110")).Roact)local w=require(script.Parent.Parent.Dictionary)local i=Z.createElement local X=Z.Component:extend("\070\105\116\084\101\120\116")function X.init(n)n.ref=Z.createRef()n.sizeBinding,n.setSize=Z.createBinding(UDim2.new())end function X.render(n)local X=n.props.Kind or"\084\101\120\116\076\097\098\101\108"local G=w.merge(n.props,{FitAxis=w.None;Kind=w.None;Padding=w.None;MinSize=w.None,Size=n.sizeBinding;[Z.Ref]=n.ref,[Z.Change.AbsoluteSize]=function()n:updateTextMeasurements()end})return i(X,G)end function X.didMount(n)n:updateTextMeasurements()end function X.didUpdate(n)n:updateTextMeasurements()end function X.updateTextMeasurements(Z)local w=Z.props.MinSize or Vector2.new(0,0)local i=Z.props.Padding or Vector2.new(0,0)local X=Z.props.FitAxis or"\088\089"local G=Z.props.Size local K=Z.props.Text or""local V=Z.props.Font or Enum.Font.Legacy local F=Z.props.TextSize or 12 local y=Z.ref.current.AbsoluteSize local J if X=="\088\089"then J=Vector2.new(9000000.0,9000000.0)elseif X=="\088"then J=Vector2.new(9000000.0,y.Y-i.Y*2)elseif X=="\089"then J=Vector2.new(y.X-i.X*2,9000000.0)end local v=n:GetTextSize(K,F,V,J)local m=math.max(w.X,i.X*2+v.X)local s=math.max(w.Y,i.Y*2+v.Y)local e if X=="\088\089"then e=UDim2.new(0,m,0,s)elseif X=="\088"then e=UDim2.new(0,m,G.Y.Scale,G.Y.Offset)elseif X=="\089"then e=UDim2.new(G.X.Scale,G.X.Offset,0,s)end Z.setSize(e)end return X
local TextService = game:GetService('TextService')

local Roact = require(script:FindFirstAncestor('MazeGeneratorPlugin').Roact)

local Dictionary = require(script.Parent.Parent.Dictionary)

local e = Roact.createElement

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

function FitText:init()
	self.ref = Roact.createRef()
	self.sizeBinding, self.setSize = Roact.createBinding(UDim2.new())
end

function FitText:render()
	local kind = self.props.Kind or 'TextLabel'

	local containerProps = Dictionary.merge(self.props, {
		FitAxis = Dictionary.None,
		Kind = Dictionary.None,
		Padding = Dictionary.None,
		MinSize = Dictionary.None,
		Size = self.sizeBinding,
		[Roact.Ref] = self.ref,
		[Roact.Change.AbsoluteSize] = function()
			self:updateTextMeasurements()
		end,
	})

	return e(kind, containerProps)
end

function FitText:didMount()
	self:updateTextMeasurements()
end

function FitText:didUpdate()
	self:updateTextMeasurements()
end

function FitText:updateTextMeasurements()
	local minSize = self.props.MinSize or Vector2.new(0, 0)
	local padding = self.props.Padding or Vector2.new(0, 0)
	local fitAxis = self.props.FitAxis or 'XY'
	local baseSize = self.props.Size

	local text = self.props.Text or ''
	local font = self.props.Font or Enum.Font.Legacy
	local textSize = self.props.TextSize or 12

	local containerSize = self.ref.current.AbsoluteSize

	local textBounds

	if fitAxis == 'XY' then
		textBounds = Vector2.new(9e6, 9e6)
	elseif fitAxis == 'X' then
		textBounds = Vector2.new(9e6, containerSize.Y - padding.Y * 2)
	elseif fitAxis == 'Y' then
		textBounds = Vector2.new(containerSize.X - padding.X * 2, 9e6)
	end

	local measuredText = TextService:GetTextSize(text, textSize, font, textBounds)

	local computedX = math.max(minSize.X, padding.X * 2 + measuredText.X)
	local computedY = math.max(minSize.Y, padding.Y * 2 + measuredText.Y)

	local totalSize

	if fitAxis == 'XY' then
		totalSize = UDim2.new(0, computedX, 0, computedY)
	elseif fitAxis == 'X' then
		totalSize = UDim2.new(0, computedX, baseSize.Y.Scale, baseSize.Y.Offset)
	elseif fitAxis == 'Y' then
		totalSize = UDim2.new(baseSize.X.Scale, baseSize.X.Offset, 0, computedY)
	end

	self.setSize(totalSize)
end

return FitText