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


local terra 
render_conf_circles(co: &lib.srv.convo, path: lib.mem.ptr(pref)): pstr
	if path.ct == 2 then
		var circs = co.srv:circle_search(&co.srv.pool, co.who.id, 0)
		var ui: data.view.conf_circles
		if circs.ct == 0 then
			ui.newattr = ' open';
			ui.circles = '';
		else
			ui.newattr = '';
			var circlst = co:stra(86)
			for i = 0, circs.ct do
				circlst:lpush '<li><a href="/conf/circles/':shpush(circs(i).cid):lpush'">'
					   :ppush(circs(i).name)
					   :lpush '</a></li>'
			end
			ui.circles = circlst:finalize()
		end

		return ui:poolstr(&co.srv.pool)
	elseif path.ct == 3 then
		var cid, cid_ok = lib.math.shorthand.parse(path(2).ptr, path(2).ct)
		if not cid_ok then return pstr.null() end
		var circ = co.srv:circle_search(&co.srv.pool, co.who.id, cid)
		if not circ then return pstr.null() end
		var actrs = co.srv:circle_members_fetch_cid(&co.srv.pool, cid)

		var acta = co:stra(86)
		if actrs:ref() then
			for i=0, actrs.ct do
				var a = co:uid2actor(actrs(i))
				if a ~= nil then
					acta:lpush '<li><a class="username" href="/':push(a.xid,0):lpush '#rel">'
					lib.render.nym(a, 0, &acta, false)
					acta:lpush '</a></li>'
				end
			end
		end

		var ui = data.view.conf_circle_view {
			circle = circ().name;
			actors = acta:finalize();
		}

		return ui:poolstr(&co.srv.pool)
	else return pstr.null() end
end

return render_conf_circles
