local export = {}
local pos_functions = {}
local links = require('Module:links')
local labels = require('Module:labels')

local lang = require('Module:languages').getByCode('pi')
local PAGENAME = mw.title.getCurrentTitle().text
local currentScript = lang:findBestScript(PAGENAME)

local translit = require('Module:pi-Latn-translit').tr

local Array = require "Module:array"

function export.alt(frame)

	local args = frame:getParent().args

	local currentScriptCode = currentScript:getCode()
	local availableScripts = lang:getScripts()
	local latinForm = args['Latn'] or currentScriptCode == 'Latn' and PAGENAME or error('未檢測到拉丁文字形式。')
	local scriptCode

	local terms
	local first
	local output = Array('<div class="NavFrame" style="max-width:40em"><div class="NavHead" style="background:#d9ebff">不同字體寫法</div><div class="NavContent" style="text-align:left"><ul>')
	for _, script in ipairs(availableScripts) do
		scriptCode = script:getCode()
		terms = { args[scriptCode], args[scriptCode..'2'], args[scriptCode..'3'],
			 args[scriptCode..'4'], args[scriptCode..'5'], args[scriptCode..'6'],
			 args[scriptCode..'7'], args[scriptCode..'8']}

		local t1, t2, t3, t4
		t1 = translit(latinForm, scriptCode, {}) -- not affect Latn itself

		if scriptCode == 'Thai' or scriptCode == 'Laoo' then
			t2 = translit(latinForm, scriptCode, {impl='no'})
		end
-- For Lao script without implicit vowels, 'y' can also be 'ຢ', but seemingly
-- not if a Buddhist Institute consonant is used.
		if scriptCode == 'Laoo' then
			if not mw.ustring.find(t2, '[ຆຌຏຐຑຒຓຘຠຨຩ]') then
				t3 = mw.ustring.gsub(t2, 'ຍ', 'ຢ')
			end
		end

		if scriptCode == 'Mymr' then
			t2 = translit(latinForm, scriptCode, {variation='1'}) --Mon
			t3 = translit(latinForm, scriptCode, {variation='2'}) --Old Shan
			t4 = translit(latinForm, scriptCode, {variation='3'}) --New Shan
		end

		if #terms == 0 then
			terms[1] = t1
			if t2 and (t1 ~= t2) then table.insert(terms, t2) end
			if t3 and (t1 ~= t3) and (t2 ~= t3) then table.insert(terms, t3) end
			if t4 and (t1 ~= t4) and (t2 ~= t4) and (t3 ~= t4) then table.insert(terms, t4) end
		elseif terms[1] ~= t1 and terms[2] ~= t1  and terms[3] ~= t1  and terms[4] ~= t1 then
			output:insert('[[Category:轉寫不一致的巴利語詞]]')
		end

		first = true
		for _,term in ipairs(terms) do
			if term ~= nil and term ~= PAGENAME then
				if first then
					first = false
					output:insert('<li>')
				else
					output:insert(' 或 ')
				end
				output:insert(links.full_link({lang = lang, sc = script, term = term, tr = '-'}))
			end
		end
		if not first then
				output:insert(' ' .. labels.show_labels {
					lang = lang,
					labels = { script:getDisplayForm() },
				} .. '</li>')
		end
	
	end
	output:insert('</ul></div></div>')

	return output:concat()

end

function export.show(frame)
	local params = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true, default = "-"},
	}
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	local is_noun
	if poscat == "名詞" or poscat == '專有名詞' or poscat == "代詞" or poscat == "名詞變格形" then
		is_noun = true
		params[1] = {list = "g", default = "?"}
		params["m"] = {list = true}
		params["f"] = {list = true}
		params["n"] = {list = true}
	end
	if poscat == "動詞" then
		params[1] = {default = "?"}
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)

	local heads = args.head
	if #heads == 0 then
		heads = {PAGENAME}
	end
	local genders = {}
	if is_noun then
		genders = args[1]
	end

	local data = {lang = lang, sc = currentScript, pos_category = poscat, categories = {},
		sccat = true, heads = heads, genders = genders, inflections = {}, translits = args.tr
	}
	
	if is_noun then
		if #args.m > 0 then
			args.m.label = "陽性"
			table.insert(data.inflections, args.m)
		end
		if #args.f > 0 then
			args.f.label = "陰性"
			table.insert(data.inflections, args.f)
		end
		if #args.n > 0 then
			args.n.label = "中性"
			table.insert(data.inflections, args.n)
		end
		if #args.m > 0 or #args.f > 0 or #args.n > 0 then
			table.insert(data.categories, "Pali nouns with other-gender equivalents")
		end
	end
    if poscat == "動詞" then
        table.insert(data.inflections, {label = "詞根", args[1]})
    end
	
	return require("Module:headword").full_headword(data)
end

return export