Modul:dtp-dial-sin

local export = {}
local m_links = require("Module:links")
local m_languages = require("Module:languages")
local dtp = m_languages.getByCode("dtp")

local variety_list = {
	"Bunduliwan", "Bundu", "Liwan", "Tangaa'", 
	"Kadazan", "Dusun", "Murut", "Rungus", "Sungai", "Bisaya", "Lundayeh",
	"Idahan", "Tidong",
}

local variety_colour = {
	["Bunduliwan"]		= "FAF0F1",
	["Bundu"]			= "FAF0F2",
	["Liwan"]			= "FAF0F2",
	["Tangaa'"]			= "FAF0F2",
	["Kadazan"]			= "FAF5F0",
	["Dusun"]			= "F0F5FA",
	["Murut"]			= "F0FAF3",
	["Rungus"]			= "FAF0F6",
	["Sungai"]			= "FAF9F0",
	["Bisaya"]			= "F0F5FA",
	["Lundayeh"]		= "FAF0FE",
	["Idahan"]			= "F7FAF0",
	["Tidong"]			= "F7FAF0",
}

local special_note = {
	["Bunduliwan"] = "[[w:Bahasa Kadazandusun|Bunduliwan]]",
	["Bundu"] = "Bundu",
	["Liwan"] = "Liwan",
	["Tangaa'"] = "Tangaa'",
}

function export.main(frame)
	local args = frame:getParent().args
	local pagename = mw.title.getCurrentTitle().text
	local target_page = args[1] or pagename
	local resource_page = "Module:dtp/data/dial-sin/" .. target_page
	local variety_data = require("Module:dtp/data/dial")
	local m_syndata
	if mw.title.new(resource_page).exists then
		m_syndata = require(resource_page).list
	else
		return frame:expandTemplate{ title = "Template:dtp-dial/tidak_dicipta", args = { target_page } }
	end
	
	local template = {
		["Bunduliwan"]		=	{},
		["Bundu"]			=	{},
		["Liwan"]			= 	{},
		["Tangaa'"]			=	{},
		["Kadazan"]			=	{},
		["Dusun"]			=	{},
		["Murut"]			=	{},
		["Rungus"]			=	{},
		["Sungai"]			= 	{},
		["Bisaya"]			= 	{},
		["Lundayeh"]		=	{},
		["Idahan"]			= 	{},
		["Tidong"]			=	{},
	}

	local main_title = mw.ustring.gsub(m_links.full_link( { term = target_page, lang = dtp } ), "[0-9%-]", "")
	local syn_table = { [=[
	<div class="NavFrame" style="border:0px; max-width: 40em; text-align:center;"><div class="NavHead" style="font-size:105%; border:1px solid #aaaaaa; margin-left:-1px; background-color:#CCCCFF; text-align:center;" cellpadding="3">Sinonim dialek bagi ]=] ..
		main_title .. ' (“' .. m_syndata["maksud"] .. '”) ' ..
		"[[Template:dtp-dial-peta/" .. target_page .. '|<small>&#91;peta&#93;</small>]]\n' .. [=[</div><div class="NavContent"> 
	{| class="wikitable" style="margin:0; text-align:center; width: 100%"
	|-
	! style="background:#E8ECFA" | Bahasa
	! style="background:#E8ECFA" | Dialek
	! style="background:#E8ECFA" | Perkataan]=] .. [=[
	<div style="float: right; clear: right; font-size:60%"><span class="plainlinks">[]=] ..
		tostring(mw.uri.fullUrl("Module:dtp/data/dial-sin/" .. target_page, { ["action"] = "edit" })) ..
	' sunting]</span></div>' }
	
	m_syndata["maksud"] = nil
	if m_syndata["nota"] then
		note = m_syndata["nota"]
		m_syndata["nota"] = nil
	end
	if m_syndata["Bunduliwan"][1] == "" then
		m_syndata["Bunduliwan"] = { target_page }
	elseif m_syndata["Bunduliwan"][1] == "-" then
		m_syndata["Bunduliwan"] = nil
	end
	
	for location, synonym_set in pairs(m_syndata) do
		if synonym_set[1] ~= "" then
			local formatted_synonyms = {}
			for i, synonym in ipairs(synonym_set) do
				local synonym_note = mw.text.split(synonym, ":")
				local synonym_etym = mw.text.split(synonym_note[1], "_")
				local syn, etym, note = synonym_etym[1], synonym_etym[2], synonym_note[2]
				local synonym_link = m_links.full_link( {
						term = syn .. (etym and string.format("#Etimologi %s", etym) or ""),
						alt = etym and (syn .. string.format("<sub>%s</sub>", etym)),
						lang = dtp
					} )
				note = note and ' <span style="font-size:60%"><i>' .. note .. '</i></span>' or ""
				table.insert(formatted_synonyms, synonym_link .. note)
			end
			local location_data = variety_data[location] or error("Tiada data bagi lokasi " .. location .. "!")
			local location_name = mw.ustring.gsub(location_data.melayu or location, "(%(.*%))", "<small>%1</small>")
			local location_link = location_data.link or location_name
			table.insert(template[location_data.group],
				{ location_data.order, location_name, location_link, formatted_synonyms })
		end
	end
	
	for _, variety in ipairs(variety_list) do
		local colour = variety_colour[variety]
		if #template[variety] > 0 then
			table.sort(template[variety], function(first, second) return first[1] < second[1] end)
			for i, point_data in ipairs(template[variety]) do
				table.insert(syn_table, "\n|-")
				if i == 1 then
					table.insert(syn_table, "\n!rowspan=" .. #template[variety] .. (special_note[variety] and " colspan=2" or "") .. 
					' style="background:#' .. colour .. '"| ' .. (special_note[variety] or variety))
				end
				table.insert(syn_table, ((point_data[2] and not special_note[variety]) and ('\n|style="background:#' .. colour .. '"| ' .. 
					'[[w:' .. point_data[3] .. '|' .. point_data[2] .. ']]') or '') ..
					'\n|style="background:#' .. colour .. '"| ' ..
					table.concat(point_data[4], ", "))
			end
		end
	end

	if note and note ~= "" then
		table.insert(syn_table, '\n|-\n! style="background:#FFF7FB; padding-top:5px; padding-bottom: 5px" | ' ..
			"<small>Nota</small>\n| colspan=2|<small><i>" .. note .. "</i></small>")
	end
	
	table.insert(syn_table, '\n|}</div></div>')
	
	return table.concat(syn_table, "")
end

return export