This module implements the template {{વળી}}.


local export = {}

local yesno = require('Module:હાના')

local function serial_comma_join(seq)
	if #seq == 0 then
		return ""
	end
	if #seq == 1 then
		return seq[1] -- nothing to join
	end
	if #seq == 2 then
		return seq[1] .. " ''અને'' " .. seq[2]
	end
	return table.concat(seq, ", ", 1, #seq - 1) .. "<span class='serial-comma'>,</span>" ..
		"''<span class='serial-and'> અને</span>'' " ..
		seq[#seq]
end

function export.main(frame)
	local pargs = frame:getParent().args
	local dsc = pargs.sc
	local duni = yesno((pargs.uni == 'પોતે') or pargs.uni) and 'પોતે' or false
	local title = mw.title.getCurrentTitle()

	local items = {}
	for i, arg in ipairs(pargs) do
		local uni = (pargs['એક' .. i] ~= nil) and pargs['એક' .. i] or duni
		if not yesno(uni, uni) then
			uni = false
		end
		local sc = pargs['લિપિ' .. i] or dsc
		
		local s
		local has_piped_link, _, link_text = arg:find("%[%[[^%[%]|]+|(.+)]]")
		if has_piped_link then
			s = ("'''%s'''"):format(arg)
			arg = mw.text.decode(link_text)
		else
			local has_link, _, link_text = arg:find("%[%[([^%[%]|]+)]]")
			if has_link then
				s = ("'''%s'''"):format(arg)
				arg = mw.text.decode(link_text)
			else
				s = ("'''[[%s]]'''"):format(arg)
			end
		end
		local codepoint
		if uni == 'પોતે' then
			codepoint = (mw.ustring.len(arg) == 1) and mw.ustring.codepoint(arg, 1, 1)
		elseif uni then
			codepoint = tonumber(uni)
		end
		if codepoint then
			local m_unidata = require('Module:યુનિકોડ માહિતી')
			s = s .. (" <small>[U+%04X %s]</small>"):format(
				codepoint,
				m_unidata.lookup_name(codepoint):gsub("<", "&lt;")
			)
		end
		if arg ~= title.fullText then
			table.insert(items, s)
		end
	end
	
	if #items == 0 then
		table.insert(items, "{{{1}}}")
	end
	
	return ("<div class=\"disambig-see-also%s\">''આ પણ જુઓ:'' %s</div>"):format(
		(#items == 2) and "-2" or "",
		serial_comma_join(items)
	)
end

return export