-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local pref = lib.mem.ref(int8)

local mappings = {
	{url = 'profile', title = 'account profile', render = 'profile'};
	{url = 'avi', title = 'avatar', render = 'avatar'};
	{url = 'ui', title = 'user interface', render = 'ui'};
	{url = 'sec', title = 'security', render = 'sec_overlay'};
	{url = 'rel', title = 'relationships', render = 'rel'};
	{url = 'qnt', title = 'quarantine', render = 'quarantine'};
	{url = 'acl', title = 'access control shortcuts', render = 'acl'};
	{url = 'rooms', title = 'chatrooms', render = 'rooms'};
	{url = 'circles', title = 'circles', render = 'circles'};

	{url = 'srv', title = 'server settings', render = 'srv'};
	{url = 'brand', title = 'instance branding', render = 'rebrand'};
	{url = 'badge', title = 'user badges', render = 'badge'};
	{url = 'emoji', title = 'custom emoji packs', render = 'emojo'};
	{url = 'censor', title = 'censorship &amp; badthink suppression', render = 'rebrand'};
	{url = 'users', title = 'user accounting', render = 'users'};

}

local path = symbol(lib.mem.ptr(pref))
local co = symbol(&lib.srv.convo)
local panel = symbol(pstr)
local invoker = quote co:complain(404,'not found','no such control panel is available in this version of parsav') end

for i, m in ipairs(mappings) do
	if lib.render.conf[m.render] then
		invoker = quote
			if path(1):cmp(lib.str.lit([m.url])) then
				var body = [lib.render.conf[m.render]] (co, path)
				var a: lib.str.acc a:init(body.ct+48)
				if not body then
					a:lpush(['<h1>' .. m.title .. ' :: error</h1>' ..
						'<p>the requested resource is not available.</p>'])
					panel = a:finalize()
				else
					a:lpush(['<h1>' .. m.title .. '</h1>']):ppush(body)
					panel = a:finalize()
					body:free()
				end
			else [invoker] end
		end
	end
end

local terra 
render_conf([co], [path], notify: pstr)
	var menu: lib.str.acc menu:init(64):lpush('<hr>') defer menu:free()

	-- build menu
	do var p = co.who.rights.powers
		if p:affect_users() then menu:lpush '<a href="/conf/users">users</a>' end
		if p.censor() then menu:lpush '<a href="/conf/censor">badthink alerts</a>' end
		if p.config() then menu:lpush([
			'<a href="/conf/srv">server &amp; policy</a>' ..
			'<a href="/conf/badge">badges</a>' ..
			'<a href="/conf/emoji">emoji packs</a>'
		]) end
		if p.rebrand() then menu:lpush '<a href="/conf/brand">instance branding</a>' end
	end

	-- select the appropriate panel
	var [panel] = pstr { ptr = ''; ct = 0 }
	if path.ct >= 2 then [invoker] end

	-- avoid the hr if we didn't add any elements
	var mptr = pstr { ptr = menu.buf, ct = menu.sz }
	if menu.sz <= 4 then mptr.ct = 0 end -- 🙄
	var pg = data.view.conf {
		menu = mptr;
		panel = panel;
	}

	var pgt: pstr
	if notify:ref() then
		var fnpg: lib.str.acc
		fnpg:compose('<div class="flashmsg">', notify, '</div>')
		pg:append(&fnpg)
		pgt = fnpg:finalize()
	else pgt = pg:tostr() end
	defer pgt:free()

	co:stdpage([lib.srv.convo.page] {
		title = 'configure'; body = pgt;
		class = lib.str.plit 'conf';
		cache = false;
	})

	if panel.ct ~= 0 then panel:free() end
end

return render_conf
