local function root(path)
	local f = debug.getinfo(1, "S").source:sub(2)
	return vim.fn.fnamemodify(f, ":p:h:h") .. "/.tests/" .. (path or "")
end

local function exists(path)
	if vim.uv then
		return vim.uv.fs_stat(path)
	elseif vim.loop then
		return vim.loop.fs_stat(path)
	else
		return false
	end
end

local function load(plugin, branch)
	local name = plugin:match(".*/(.*)")
	local package_root = root("site/pack/deps/start/")
	if not exists(package_root .. name) then
		print("Installing " .. plugin)

		local cmd = { "git", "clone", "--depth=1" }
		if branch then
			table.insert(cmd, "--branch=" .. branch)
		end
		table.insert(cmd, "https://github.com/" .. plugin .. ".git")
		table.insert(cmd, package_root .. "/" .. name)

		vim.fn.mkdir(package_root, "p")
		vim.fn.system(cmd)
	end
	vim.cmd("packadd " .. name)
end

local site = root("site")

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.opt.runtimepath:append(root())
vim.opt.packpath = { site }

load("nvim-lua/plenary.nvim")
load("nvim-treesitter/nvim-treesitter", "main")

local nvim_ts = require("nvim-treesitter")
nvim_ts.setup({ install_dir = site })
nvim_ts.install({ "markdown", "markdown_inline" }, { summary = true }):wait(300000)
