local l=require(script.Parent.WriteFmt)local w={}w.ClassName="\070\111\114\109\097\116\116\101\114"w.__index=w function w.new()return setmetatable({_Buffer={},_Display="";_Indentation="",_IndentLevel=0;_IsLocked=false,_StartOfLine=true},w)end function w.AsString(l)return l._Display end function w.AsTuple(l)return table.unpack(l._Buffer)end function w.AddTable(w,l)table.insert(w._Buffer,l)return w end function w.Lock(l)l._IsLocked=true return l end function w.Unlock(l)l._IsLocked=false return l end function w.AddToBuffer(w,l)if w._IsLocked==false then if type(l)=="\115\116\114\105\110\103"and type(w._Buffer[#w._Buffer])=="\115\116\114\105\110\103"then w._Buffer[#w._Buffer]..=l else table.insert(w._Buffer,l)end end return w end function w.Indent(l)l._IndentLevel+=1 l._Indentation=string.rep("    ",l._IndentLevel)return l end function w.Unindent(l)l._IndentLevel=math.max(0,l._IndentLevel-1)l._Indentation=string.rep("    ",l._IndentLevel)return l end function w.Write(U,w,...)l(U,w,...)return U end function w.WriteLine(U,w,...)l(U,w,...)U:AddToBuffer("\n")U._Display..="\n"U._StartOfLine=true return U end function w.WriteLineRaw(w,l)(w:WriteRaw(l)):AddToBuffer("\n")w._Display..="\n"w._StartOfLine=true return w end function w.WriteRaw(w,l)if#l>0 then if w._StartOfLine and#w._Indentation>0 then w._StartOfLine=false w:AddToBuffer(w._Indentation)w._Display..=w._Indentation end w._StartOfLine=false w:AddToBuffer(l)w._Display..=l end return w end return w
local WriteFmt = require(script.Parent.WriteFmt)

local Formatter = {}
Formatter.ClassName = "Formatter"
Formatter.__index = Formatter

function Formatter.new()
	return setmetatable({
		_Buffer = {};
		_Display = "";
		_Indentation = "";
		_IndentLevel = 0;
		_IsLocked = false;
		_StartOfLine = true;
	}, Formatter)
end

function Formatter:AsString()
	return self._Display
end

function Formatter:AsTuple()
	return table.unpack(self._Buffer)
end

function Formatter:AddTable(Table)
	table.insert(self._Buffer, Table)
	return self
end

function Formatter:Lock()
	self._IsLocked = true
	return self
end

function Formatter:Unlock()
	self._IsLocked = false
	return self
end

function Formatter:AddToBuffer(Value)
	if self._IsLocked == false then
		if type(Value) == "string" and type(self._Buffer[#self._Buffer]) == "string" then
			self._Buffer[#self._Buffer] ..= Value
		else
			table.insert(self._Buffer, Value)
		end
	end

	return self
end

function Formatter:Indent()
	self._IndentLevel += 1
	self._Indentation = string.rep("    ", self._IndentLevel)
	return self
end

function Formatter:Unindent()
	self._IndentLevel = math.max(0, self._IndentLevel - 1)
	self._Indentation = string.rep("    ", self._IndentLevel)
	return self
end

function Formatter:Write(Template, ...)
	WriteFmt(self, Template, ...)
	return self
end

function Formatter:WriteLine(Template, ...)
	WriteFmt(self, Template, ...)
	self:AddToBuffer("\n")
	self._Display ..= "\n"
	self._StartOfLine = true
	return self
end

function Formatter:WriteLineRaw(Value)
	self:WriteRaw(Value):AddToBuffer("\n")
	self._Display ..= "\n"
	self._StartOfLine = true
	return self
end

function Formatter:WriteRaw(Value)
	if #Value > 0 then
		if self._StartOfLine and #self._Indentation > 0 then
			self._StartOfLine = false

			self:AddToBuffer(self._Indentation)
			self._Display ..= self._Indentation
		end

		self._StartOfLine = false

		self:AddToBuffer(Value)
		self._Display ..= Value
	end

	return self
end

return Formatter
