local export = {}
local m_IPA = require("Module:IPA")

local lang = require("Module:languages").getByCode("kmr")

local function track(page)
	require("Module:debug").track("kmr-pron/" .. page)
	return true
end

local letters1_phonemes = {
	["a"] = "ɑː",
    ["b"] = "b",
	["c"] = "d͡ʒ",
    ["ç"] = "t͡ʃ",
	["d"] = "d",
	["e"] = "ɛ",
	["ê"] = "eː",
	["f"] = "f",
	["g"] = "ɡ",
	["h"] = "h",
	["i"] = "ɪ",
	["î"] = "iː",
	["j"] = "ʒ",
	["k"] = "k",
	["l"] = "l",
	["m"] = "m",
	["n"] = "n",
	["o"] = "oː",
	["p"] = "p",
	["q"] = "q",
	["r"] = "ɾ",
	["s"] = "s",
	["ş"] = "ʃ",
	["t"] = "t",
	["u"] = "ʊ",
	["û"] = "uː",
	["v"] = "v",
	["w"] = "w",
	["x"] = "x",
	["y"] = "j",
	["z"] = "z",
	
-- Tîpên taybet (soranî)
	["ẍ"] = "ɣ", -- mînak: aẍa
	["ḧ"] = "ħ",
	["ʿ"] = "ʕ",
	["'"] = "ˤ",
--	["ll"] = "ɫ", -- an jî /lˤ/; bi erebî (ڵ)
	["rr"] = "r", -- bi erebî (ڕ)
	
-- Kurdiya başûr (kirmaşanî)
	["ł"] = "ɫ", -- bnr. "ll"; mînak bał, diɫ
	["ň"] = "ŋ", -- mînak: maň, seň
    ["ř"] = "r", -- bnr. 'rr'; mînak: piř
	["ü"] = "yː" -- mînak: tü
}
-- Jihevqetandin
local vowels = "aeiîuûêo"
local vowel = "[" .. vowels .. "]"
local consonant = "[^" .. vowels .. ". -]"
local syllabify_pattern = "(" .. vowel .. ")(" .. consonant .. "?)(" .. consonant .. "?)(" .. vowel .. ")"

local function syllabify(word)
	word = mw.ustring.gsub(word, "%-(" .. consonant .. ")%-(" .. consonant .. ")", "%1.%2")
	word = string.gsub(word, "%-", ".")
	
	-- Add syllable breaks.
	for count = 1, 2 do
		word = mw.ustring.gsub(
			word,
			syllabify_pattern,
			function(a, b, c, d)
				if c == "" and b ~= "" then
					c, b = b, ""
				end
				
				return a .. b .. "." .. c .. d
			end
		)
	end
	
	word = mw.ustring.gsub(word, '([aeêiîouûy])([^aeêiîouûy. ])([^aeêiîouûy. ])([^aeêiîouûy. ])', "%1%2%3.%4")
	
	return word
end

function export.phonemic(word)
	word = mw.ustring.lower(word) -- nivîsa biçûk
	word = syllabify(word)
	word = mw.ustring.gsub(word, '.', letters1_phonemes)
	
	word = mw.ustring.gsub(word,'xw','xʷ') -- mînak: maň, seň
    word = mw.ustring.gsub(word,'nɡ','ŋɡ') -- minak: ferheng
    word = mw.ustring.gsub(word,'nk','ŋk') -- minak: bank
    word = mw.ustring.gsub(word, '^ɾ', "r") -- mînak: roj
    word = mw.ustring.gsub(word,'ɾɾ','r') -- mînak: pirr; bi erebî (ڕ)
    word = mw.ustring.gsub(word, 'll', 'ɫ') -- an jî /lˁ/;  mînak: bi soranî gull (گوڵ) (bi kurmancî gul); bi erebî (ڵ)
    word = mw.ustring.gsub(word,'ɘj','iː') -- iy → îy
	
	-- + Giranî (stress) li ser kîteya dawî
	tab_word = mw.text.split(word, '%.')
	if (#tab_word ~= 1) then
		tab_word[#tab_word] = "ˈ" .. tab_word[#tab_word]
	end
	word = table.concat(tab_word, '')
	
	ipa = word
	
	-- Add default stress mark if not present
	if not mw.ustring.find(ipa, "") then
		ipa = "" .. ipa
	end
	
	return ipa
end

function export.show(frame)
	local terms = {}

	local args = frame:getParent().args
	local currentTitle = mw.title.getCurrentTitle().text

	for _, term in ipairs(args) do
		if term == currentTitle then track("redundant") end
		table.insert(terms, term)
	end

	if #terms == 0 then
		terms = {currentTitle}
	end

	local results = {}

	for _, term in ipairs(terms) do
		table.insert(results, {pron = "/" .. export.phonemic(term) .. "/"})
	end

	return "*" .. m_IPA.format_IPA_full { lang = lang, items = results }
end

return export