-- Works backwards, joining the arguments until it resolves to an absolute path.
-- If an absolute path is not resolved, then the current working directory is
-- prepended

function Path:resolve(...: string)
	local paths: Array<string> = { ... }
	local resolvedpath = ""
	local resolveddrive = nil
	local isabsolute = false
	for i = #paths, 1, -1 do
		local path = paths[i]
		if path and path ~= "" then
			local root = resolveddrive and self:getRoot(path)
			if self:isDriveRelative(path) then
				root = root or self:getRoot(path)
				resolveddrive = resolveddrive or root
				path = path:sub(root:len() + 1)
			end
			if not root or resolveddrive:sub(1, 2) == root:sub(1, 2) then
				resolvedpath = self:join(self:normalize(path), resolvedpath)
				if self:isAbsolute(resolvedpath) then
					isabsolute = true
					break
				end
			end
		end
	end
	if not isabsolute then
		if resolveddrive then
			local drivecwd = process.env["=" .. resolveddrive]
			if drivecwd and self:pathsEqual(drivecwd:sub(1, 2), resolveddrive) then
				resolvedpath = self:join(drivecwd, resolvedpath)
			else
				resolvedpath = self:join(resolveddrive, resolvedpath)
			end
		else
			resolvedpath = self:join(process.cwd(), resolvedpath)
		end
	end

	local trailing_slashes = resolvedpath:match("[" .. self.sep .. "]*$")
	if trailing_slashes then
		resolvedpath = resolvedpath:sub(1, -trailing_slashes:len() - 1)
	end

	return resolvedpath
end
