local export = {} 

local m_IPA = require("Module:IPA")
local m_a = require("Module:accent qualifier")
local lang = require("Module:languages").getByCode("ha")
local s = mw.ustring.gsub
local C = "[ɡɟkTcʃSfDJjɽrʔmɓbfnɗdtzslwhpŋɲ]"
local V = "[áéíóúɛ́ɔ́àèìòùɛ̀ɔ̀âêîôûɛ̂ɔ̂]"
local i = { --mid-open realizations of <e, o> in borrowings
	{"E", "ɛ́"},
	{"Ḕ", "ɛ̀ː"},
	{"È", "ɛ̀"},
	{"Ê", "E"},--don't change this...
	{"O", "ɔ́"},
	{"Ṑ", "ɔ̀ː"},
	{"Ò", "ɔ̀"},
	{"Ô", "O"},--...see rules 62&63 of c
	{"R", "r̃"}
}
local c = { --phoneMic Correspondences (from spelling)
	{"ʼ", "'"}, 
	{"([gkƙ])%1([wy])", "%1%2%1%2"}, 
	{"tts(h?)", "ts%1ts%1"}, 
	{"ssh", "shsh"}, 
	{"ffy", "fyfy"}, 
	{"cch'", "ch'ch'"}, 
	{"''y", "'y'y"}, 
	{"gw", "ɡʷ"}, 
	{"gy", "ɟ"}, 
	{"kw", "kʷ"}, 
	{"ch'", "Tʼ"}, 
	{"c", "T"}, 
	{"ky", "c"}, 
	{"ƙw", "kʷʼ"}, 
	{"ƙy", "cʼ"}, 
	{"tsh", "Tʼ"}, 
	{"sh", "ʃ"}, 
	{"ts", "Sʼ"}, 
	{"fy", "fʲ"}, 
	{"j", "D"}, 
	{"'y", "J"}, 
	{"y", "j"}, 
	{"ƴ", "J"}, 
	{"g", "ɡ"}, 
	{"ƙ", "kʼ"}, 
	{"r̃", "R"}, 
	{"r", "ɽ"}, 
	{"R", "r"},
	{"'", "ʔ"}, 
	{"a", "á"}, 
	{"ā̀", "àː"},	
	{"ā", "áː"}, 
	{"e", "é"}, 
	{"ḕ", "èː"}, 
	{"ē", "éː"}, 
	{"i", "í"}, 	
	{"ī̀", "ìː"},
	{"ī", "íː"}, 
	{"o", "ó"}, 	
	{"ṑ", "òː"}, 
	{"ō", "óː"}, 
	{"u", "ú"}, 	
	{"ū̀", "ùː"},
	{"ū", "úː"}, 
	{"([âêîôûEO])", "%1ː"}, 
	{"^(" .. V .. ")", "ʔ%1"}, 
	{"(.[ -/])(" .. V .. ")", "%1ʔ%2"}, 
	{"(.)[-/](.)", "%1%2"},
	{"(" .. V .. "ː?" .. C .. "?[ʷʲ]?ʼ?)(" .. C .. "[ʷʲ]?ʼ?)%f" .. V, "%1.%2"}, 
	{"([áàâ]ː?)[íìî]", "%1i"},
	{"([áàâ]ː?)[úùû]", "%1u"},
	{"âː([iu])", "â%1"},
	{"([âêîôûɛ̂ɔ̂])ː(" .. C .. "[ʷʲ]?ʼ?%.)", "%1%2"},
	{"([âêîôûɛ̂ɔ̂])ː(" .. C .. "[ʷʲ]?ʼ?)$", "%1%2"},
	{"T(ʼ?)w", "Tʷ%1"},
	{"^(" .. C .. ")(ʼ?)w", "%1ʷ%2"},
	{"( " .. C .. ")(ʼ?)w", "%1ʷ%2"},
	{"T", "t͡ʃ"},
	{"S", "(t)s"}, 
	{"D", "(d)ʒ"}, 
	{"J", "j̰"},
	{"E", "ɛ̂"},
	{"O", "ɔ̂"},
}
local idk = { --turns a copy of "word" string, got after c, into an actual phoneMic tr (no phoneMic mid-open), whereas the value of "word" preserves the mid-open distinction for the phoneTic tr
	{"ɛ́", "é"},
	{"ɛ̀", "è"},
	{"ɛ̂", "ê"},
	{"ɔ́", "ó"},
	{"ɔ̀", "ò"},
	{"ɔ̂", "ô"}
}
local SKH = { --Standard Kano Hausa phoneTic correspondences (from phoneMic tr)
	{"%(d%)ʒ", "d͡ʒ"},
	{"f", "ɸ"}, 
	{"k(ʼ?[ɛ́ɛ̀ɛ̂éèêíìî])", "c%1"}, 
	{"ɡ([ɛ́ɛ̀ɛ̂éèêíìî])", "ɟ%1"}, 
	{"([kɡ])(ʼ?[ɔ́ɔ̀ɔ̂óòôúùû])", "%1ʷ%2"},
	{"d͡ʒ%.d͡ʒ", "d.d͡ʒ"},
	{"t͡ʃ%.t͡ʃ", "t.t͡ʃ"},
	{"[kɡ]%.([cɟkɡ]ʷ?ʼ?)", "%1.%1"},
	{"%(t%)sʼ", "sʼ"},
	{"n([ -%.]?[bɓmpɸw])", "m%1"},
	{"n([ -%.]?[jcɟ])", "ɲ%1"},
	{"n([ -%.]?)([lrɽ])", "%2%1%2"},
	{"n([ -%.]?[ kɡhʔ])", "ŋ%1"},
	{"[mn]$", "ŋ"},
	{"ŋ([ -%.]?[nɗdtzs])", "n%1"},
	{"á(%.?[jɲ])", "ɛ́%1"},
	{"à(%.?[jɲ])", "ɛ̀%1"},
	{"â(%.?[jɲ])", "ɛ̂%1"},
	{"([jj̰ɟc]ʼ?)á([^ː])", "%1ɛ́%2"},
	{"([jj̰ɟc]ʼ?)à([^ː])", "%1ɛ̀%2"},
	{"([jj̰ɟc]ʼ?)â([^ː])", "%1ɛ̂%2"},
	{"([jj̰ɟc]ʼ?)á$", "%1ɛ́"},
	{"([jj̰ɟc]ʼ?)à$", "%1ɛ̀"},
	{"([jj̰ɟc]ʼ?)â$", "%1ɛ̂"},
	{"([^hʔ])á([ -%.])", "%1ə́%2"},
	{"([^hʔ])à([ -%.])", "%1ə̀%2"},
	{"([^hʔ])â([ -%.])", "%1ə̂%2"},
	{"([^hʔ])á$", "%1ə́"},
	{"([^hʔ])à$", "%1ə̀"},
	{"([^hʔ])â$", "%1ə̂"},
	{"hó(" .. C .. "[ʷʲ]?ʼ?[ -%.])", "hɔ́%1"},
	{"hò(" .. C .. "[ʷʲ]?ʼ?[ -%.])", "hɔ̀%1"},
	{"hô(" .. C .. "[ʷʲ]?ʼ?[ -%.])", "hɔ̂%1"},
	{"hó(" .. C .. "[ʷʲ]?ʼ?)$", "hɔ́%1"},
	{"hò(" .. C .. "[ʷʲ]?ʼ?)$", "hɔ̀%1"},
	{"hô(" .. C .. "[ʷʲ]?ʼ?)$", "hɔ̂%1"},
	{"[éó](" .. C .. "[ʷʲ]?ʼ?[ -%.])", "á%1"},
	{"[èò](" .. C .. "[ʷʲ]?ʼ?[ -%.])", "à%1"},
	{"[êô](" .. C .. "[ʷʲ]?ʼ?[ -%.])", "â%1"},
	{"[éó](" .. C .. "[ʷʲ]?ʼ?)$", "á%1"},
	{"[èò](" .. C .. "[ʷʲ]?ʼ?)$", "à%1"},
	{"[êô](" .. C .. "[ʷʲ]?ʼ?)$", "â%1"},
	{"í([^ː])", "ɪ́%1"},
	{"ì([^ː])", "ɪ̀%1"},
	{"î([^ː])", "ɪ̂%1"},
	{"í$", "ɪ́"},
	{"ì$", "ɪ̀"},
	{"î$", "ɪ̂"},
	{"ú(%.?j)", "í%1"},
	{"ù(%.?j)", "ì%1"},
	{"û(%.?j)", "î%1"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)ú([^ː])", "%1ɪ́%2"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)ù([^ː])", "%1ɪ̀%2"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)û([^ː])", "%1ɪ̂%2"},
	{"ɪ́(%.?w)", "ú%1"},
	{"ɪ̀(%.?w)", "ù%1"},
	{"ɪ̂(%.?w)", "û%1"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)ú$", "%1ɪ́"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)ù$", "%1ɪ̀"},
	{"([ʃʒɽrnɗdtzsl]ʼ?)û$", "%1ɪ̂"},
	{"ú([^ː])", "ʊ́%1"},
	{"ù([^ː])", "ʊ̀%1"},
	{"û([^ː])", "ʊ̂%1"},
	{"ú$", "ʊ́"},
	{"ù$", "ʊ̀"},
	{"û$", "ʊ̂"},
	{"([^hʔ])ái", "%1éi"},
	{"([^hʔ])ài", "%1èi"},
	{"(%.[^hʔ])âi", "%1êi"},
	{"([^hʔ])âi%.", "%1êi."},
	{"M", "m"}
}

function export.pronunciation_phonemic(word, ideo)
	if ideo == "1" then
		word = s(word, "f$", "p")
	end
	for a = 1, #i do
		word = s(word, i[a][1], i[a][2])
	end
	word = mw.ustring.lower(word)
	for a = 1, #c do
		word = s(word, c[a][1], c[a][2])
	end
	return word
end 

function export.idk(word) --see var
	for a = 1, #idk do
		word = s(word, idk[a][1], idk[a][2])
	end
	return word
end 

function export.pronunciation_phonetic(word, dialect, ideo)
	if dialect == "SKH" then --see var
		if ideo == "1" then
			word = s(word, "m$", "M")
		end
		for a = 1, #SKH do
			word = s(word, SKH[a][1], SKH[a][2])
		end
	end
	return word
end 

function export.show(frame)
	local args = frame:getParent().args
	local p, results, results_ha = {}, {}, {}
	if not args[1] or (args[1] == "") then
		error("Please put the word as the first positional parameter!")
	else
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
	end
	for _, word in ipairs(p) do
		word = export.pronunciation_phonemic(word, args["ideo"])
		table.insert(results, {pron = "/" .. export.idk(word) .. "/"}) --see var
		table.insert(results_ha, {pron = "[" .. export.pronunciation_phonetic(word, "SKH", args["ideo"]) .. "]"})
	end
	if args["niger"] == "1" then
		return m_IPA.format_IPA_full { lang = lang, items = results }
	else
		return m_IPA.format_IPA_full { lang = lang, items = results } .. "\n** " .. m_a.format_qualifiers(lang, {"Standard Kano Hausa"}) .. " " .. m_IPA.format_IPA_full { lang = lang, items = results_ha }
end 
end 

return export