local export = {} 

local m_IPA = require("Module:IPA")
local m_a = require("Module:accent qualifier")
local lang = require("Module:languages").getByCode("ssy")
local s = mw.ustring.gsub
local title = mw.title.getCurrentTitle().text
local C = "[bcdfghjklmnpqrstwxyzʃçɖɽɲħ-]"
local V = "[aeiouäëïöü]"
local M = "[ʼʷ]"
local c = {
	{"ch", "çʼ"}, {"dh", "ɖ" }, {"gn", "ɲ" },
	{"x", "ħ" }, {"kh", "x" }, {"qh", "xʼ" },
	{"rh", "ɽ"}, {"sh", "ʃ"}, {"th", "tʼ" },
	{"ts", "sʼ" }, {"q", "kʼ" }, {"kw", "kʷ" },
	{"(" .. C .. "?)(" .. M .. "?)ä", "ˈ%1%2a"},
	{"(" .. C .. "?)(" .. M .. "?)ë", "ˈ%1%2e"},
	{"(" .. C .. "?)(" .. M .. "?)ï", "ˈ%1%2i"},
	{"(" .. C .. "?)(" .. M .. "?)ö", "ˈ%1%2o"},
	{"(" .. C .. "?)(" .. M .. "?)ü", "ˈ%1%2u"},
	{"(.)%1", "%1ː"},
	{"c", "ʕ"}, {"ç", "t͡ʃ" },
	{"g", "ɡ"}, {"j", "d͡ʒ"},
	{"c", "ʕ"}, {"y", "j"},
	{"-(" .. V .. ")", "ʔ%1"},
	{"-", "" },
} 

function export.pronunciation_phonemic(word)
	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.syllabify(term) --split for hyphenation
	local H, i = {}, 0
 	for a in string.gmatch(s(term, "([aeiou]" .. C .. "?)(" .. C .. ")%f[aeiou]", "%1.%2"), "[^%.-/]+") do
 		i = i+1
 		H[i] = a
 	end
 	return H
end

function export.show(frame)
	local args = frame:getParent().args
	local p, results, results_SA = {}, {}, {}
	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)
		table.insert(results, {pron = "/" .. word .. "/"})
	end

	local H = ""
	if mw.ustring.match(title, "^[^%-].+[^%-]$") then
		H = export.syllabify(title)
		table.insert(H, 1, "ssy") -- language code argument, ie. MOD:hyphenation format, next L
 		H = "\n* " .. require("Module:hyphenation").hyphenate(H)
	end
	
	return "* " .. m_IPA.format_IPA_full(lang, results) .. H
end 

return export