local stringify = (require("pandoc.utils")).stringify
-- idea aken from https://forum.obsidian.md/t/rendering-callouts-similarly-in-pandoc/40020/6 and modified to work with Quarto 1.3

local function has_value(tab, val)
	for index, value in ipairs(tab) do
		if value == val then
			return true
		end
	end

	return false
end

function BlockQuote(el)
	local start = el.content[1]

	if start.t == "Para" and start.content[1].t == "Str" and start.content[1].text:match("^%[!%w+%][-+]?$") then
		local firstline = stringify(start.content[1])
		local _, _, ctype = firstline:find("%[!(%w+)%]")
		local titlevar = stringify(start.content):match("^%[!%w+%](.-)$")
		if not ctype then
			ctype = "note"
		end
		if ctype:lower() == "info" then
			ctype = "note"
		end
		if ctype:lower() == "question" then
			ctype = "warning"
		end
		if ctype:lower() == "success" then
			ctype = "tip"
		end

		if not has_value({ "note", "warning", "important", "tip", "caution" }, ctype:lower()) then
			print(ctype .. " is no valid quarto-callout. Change or add special case to lua-script.\nForced to 'note'")
			ctype = "note"
		end

		el.content:remove(1)
		if #el.content == 0 then
			el.content = pandoc.List({ pandoc.Para({}) })
		end
		-- Create Quarto callout
		return quarto.Callout({
			type = ctype:lower(),
			title = titlevar,
			content = el.content,
		})
	else
		return el
	end
end
