Modul:Polparty

local getArgs = require('Modul:Arguments').getArgs
local StringUtils = require('Modul:StringUtils')
local partyData = mw.loadData('Modul:Polparty/countrydata')
local wikidata = require('Modul:Wikidata')
local p = {}

local function computeGenitiveForm(nom)
	local out = nil
	local firstWord, firstSep = StringUtils._substringBefore({nom, ' ', '-'})
	local remainder = StringUtils._removeStart({nom, firstWord .. firstSep})
	if mw.ustring.lower(firstWord) == 'partidul' or mw.ustring.lower(firstWord) == 'forumul' or mw.ustring.lower(firstWord) == 'frontul' then
		firstWord = firstWord .. 'ui'
		out = firstWord .. firstSep .. remainder
	else
		local feminineFound = false
		if mw.ustring.lower(firstWord) == 'uniunea' then
			firstWord = StringUtils._removeEnd({firstWord, 'ea'}) .. 'ii'
			feminineFound = true
		elseif mw.ustring.lower(firstWord) == 'liga' then
			firstWord = StringUtils._removeEnd({firstWord, 'a'}) .. 'ii'
			feminineFound = true
		elseif mw.ustring.lower(firstWord) == 'partida' then
			firstWord = StringUtils._removeEnd({firstWord, 'a'}) .. 'ei'
			feminineFound = true
		end
		if feminineFound then
			out = firstWord .. firstSep
			repeat
				local nextWord, nextSep = StringUtils._substringBefore({remainder, ' ', '-'})
				remainder = StringUtils._removeStart({remainder, nextWord .. nextSep})
				if StringUtils._endsWith({nextWord, 'ă'}) then
					nextWord = StringUtils._removeEnd({nextWord, 'ă'}) .. 'e'
				end
				out = out .. nextWord .. nextSep
			until mw.ustring.len(remainder) == 0
		end
	end
	return out or nom
end

--[[
	Format:
		first letter:
			a - abbreviation
			f - full name
		second letter:
			n - nominative
			g - genitive
		third letter
			s - simple
			a - acronym (only if first if f)
]]
local function formatLabel(party, displayFormat)
	local abbrev = displayFormat:sub(1, 1)
	local case = displayFormat:sub(2, 2)
	local extra = displayFormat:sub(3, 3)
	local label = ''
	
	if case == 'g' then
		if abbrev == 'a' then label = label .. '-ului'
		else label = computeGenitiveForm(party.nom or party.link or wikidata.findLabel(party.q))
		end
	else
		if abbrev == 'a' then
			label = party.abbr
		else
			label = party.nom or party.link or party.abbr
		end
	end
	local out
	if party.q then 
		local illWd = require('Modul:Ill-wd').fromArgs
		out = illWd(party.q, label, party.link, true)
	else
		out = '[[' .. (party.link or party.nom)
		if label == party.link then
			out = out .. ']]'
		else
			out = out .. '|' .. label .. ']]'
		end
	end
		
	if extra == 'a' and abbrev ~= 'a' then
		out = out .. ' (' .. party.abbr .. ')'
	end
	return out
end

local function fromWikidata(q, displayFormat)
	local party = {}
	local qid = StringUtils._prependIfMissing({tostring(q), 'Q'})
	party.nom = wikidata.findLabel(qid)
	party.link = mw.wikibase.sitelink(qid)
	party.q = qid
	
	local partyWd = mw.wikibase.getEntity(qid)
	if partyWd.claims and partyWd.claims['P1813'] then
		local roAbbr = nil
		local roAbbrRank = 0
		local enAbbr = nil
		local enAbbrRank = 0
		for _,eachShortNameClaim in ipairs(partyWd.claims['P1813']) do
			if eachShortNameClaim.type == 'statement' and eachShortNameClaim.mainsnak.snaktype == 'value' then
					
				local eachShortNameRank = mw.wikibase.entity.claimRanks['RANK_' .. mw.ustring.upper(eachShortNameClaim.rank)]
				if eachShortNameClaim.mainsnak.datavalue.value.language == 'ro' and roAbbrRank < eachShortNameRank then
					roAbbrRank = eachShortNameRank
					roAbbr = eachShortNameClaim.mainsnak.datavalue.value.text
				end
				if eachShortNameClaim.mainsnak.datavalue.value.language == 'en' and enAbbrRank < eachShortNameRank then
					enAbbrRank = eachShortNameRank
					enAbbr = eachShortNameClaim.mainsnak.datavalue.value.text
				end
			end
		end
		party.abbr = roAbbr or enAbbr
	end
	return formatLabel(party, displayFormat)
end

local function fromArgs(country, partyCode, displayFormat)
	if nil == partyData or nil == partyData[country] then return nil end
	local party = partyData[country][partyCode]
	return formatLabel(party, displayFormat)
end

local function fromFrame(frame)
	local args = getArgs(frame)
	local country = args[1]
	local partyCode = args[2]
	local displayFormat = args['format'] or 'ans'
	local q = args['q']
	if q then
		return fromWikidata(q, displayFormat)
	else
		return fromArgs(country, partyCode, displayFormat)
	end
end

local function colorFromFrame(frame)
	local args = getArgs(frame)
	local country = args[1]
	local partyCode = args[2]

	return partyData[country][partyCode].color
end

p.fromArgs = fromArgs
p.fromWikidata = fromWikidata
p.fromFrame = fromFrame
p.colorFromFrame = colorFromFrame
--p.computeGenitiveForm = computeGenitiveForm
return p