-- Compare two arrays of strings line-by-line.

function diffLinesRaw(aLines: { [number]: string }, bLines: { [number]: string }): Array<Diff>
	local aLength = #aLines
	local bLength = #bLines

	local isCommon = function(aIndex: number, bIndex: number)
		return aLines[aIndex + 1] == bLines[bIndex + 1]
	end

	local diffs = {}
	local aIndex = 0
	local bIndex = 0

	local foundSubsequence = function(nCommon: number, aCommon: number, bCommon: number)
		while aIndex ~= aCommon do
			table.insert(diffs, Diff.new(DIFF_DELETE, aLines[aIndex + 1]))
			aIndex += 1
		end
		while bIndex ~= bCommon do
			table.insert(diffs, Diff.new(DIFF_INSERT, bLines[bIndex + 1]))
			bIndex += 1
		end
		while nCommon ~= 0 do
			table.insert(diffs, Diff.new(DIFF_EQUAL, bLines[bIndex + 1]))
			nCommon -= 1
			aIndex += 1
			bIndex += 1
		end
	end

	diff(aLength, bLength, isCommon, foundSubsequence)

	-- After the last common subsequence, push remaining change items.
	while aIndex ~= aLength do
		table.insert(diffs, Diff.new(DIFF_DELETE, aLines[aIndex + 1]))
		aIndex += 1
	end
	while bIndex ~= bLength do
		table.insert(diffs, Diff.new(DIFF_INSERT, bLines[bIndex + 1]))
		bIndex += 1
	end

	return diffs
end

return {
	printDiffLines = printDiffLines,
	diffLinesUnified = diffLinesUnified,
	diffLinesUnified2 = diffLinesUnified2,
	diffLinesRaw = diffLinesRaw,
}
