-- Encapsulate change lines until either a common newline or the end.

local ChangeBuffer = {}
ChangeBuffer.__index = ChangeBuffer
function ChangeBuffer.new(op: number, changeColor: DiffOptionsColor): ChangeBuffer
	local self = (setmetatable({}, ChangeBuffer) :: any) :: ChangeBuffer
	self.op = op
	self.line = {}
	self.lines = {}
	self.changeColor = changeColor

	return self
end

function ChangeBuffer:pushSubstring(substring: string): ()
	self:pushDiff(Diff.new(self.op, substring))
end

function ChangeBuffer:pushLine(): ()
	-- Assume call only if line has at least one diff,
	-- therefore an empty line must have a diff which has an empty string.

	-- If line has multiple diffs, then assume it has a common diff,
	-- therefore change diffs have change color;
	-- otherwise then it has line color only.
	table.insert(
		self.lines,
		if #self.line ~= 1
			then Diff.new(self.op, concatenateRelevantDiffs(self.op, self.line, self.changeColor))
			else
				if self.line[1][1] == self.op
					then self.line[1] -- can use instance
					else Diff.new(self.op, self.line[1][2]) -- was common diff
	)
	self.line = {}
end

function ChangeBuffer:isLineEmpty(): boolean
	return #self.line == 0
end
