local export = {}

function export.show(word,alternative,do_debug)
	local debug = {}
	
	if type(word) == 'table' then
		do_debug = word.args[4]
		word = word.args[1]
	end
	local orig_word = word
	word = mw.ustring.lower(word or mw.title.getCurrentTitle().text)
	word = mw.ustring.gsub(word,"[^abcdefghijklmnopqrstuvwxyzáéíóú.]","")
	
	table.insert(debug,word)
			
	--digraphs and such
	word = mw.ustring.gsub(word,"c","ɕ") --fixed later
	word = mw.ustring.gsub(word,"ɕh","θ") --fixed later
	word = mw.ustring.gsub(word,"gu([aeiou])",(alternative and 'ɡv%1' or 'ɡw%1'))
	word = mw.ustring.gsub(word,"j",(alternative and 'ɟ' or 'ʒ')) --fixed later
	word = mw.ustring.gsub(word,"qu([aeiou])",(alternative and 'kv%1' or 'kw%1'))
	word = mw.ustring.gsub(word,"sh","ʃ")
	word = mw.ustring.gsub(word,"x",(alternative and 'ɡz' or 'ks'))
	word = mw.ustring.gsub(word,"y","j")
	
    word = mw.ustring.gsub(word,"ez$",(alternative and 'es' or 'ez'))
	
	word = mw.ustring.gsub(word,'g','ɡ')

	table.insert(debug,word)
	
	--syllable division
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([aeiouáéíóú])","%1.%2%3")
	end
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4")
	end
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4%5")
	end
	word = mw.ustring.gsub(word,"([pbktdɡ])%.([lr])",".%1%2")
	word = mw.ustring.gsub(word,"([^aeiouáéíóú.])%.s([^aeiouáéíóú.])","%1s.%2")
	word = mw.ustring.gsub(word,"([aeoáéíóú])([aeoáéíóú])","%1.%2")
	word = mw.ustring.gsub(word,"([ií])([ií])","%1.%2")
	word = mw.ustring.gsub(word,"([uú])([uú])","%1.%2")

	table.insert(debug,word)
	
	--diphthongs
	word = mw.ustring.gsub(word,"([ae])u","%1w")
	
	table.insert(debug,word)
	
	--accentuation
	local syllables = mw.text.split(word,"%.")
	if mw.ustring.find(word,"[áéíóú]") then
		for i=1,#syllables do
			if mw.ustring.find(syllables[i],"[áéíóú]") then syllables[i] = "ˈ"..syllables[i] end
		end
	else
		if mw.ustring.find(word,"[^abcdefghijklmnopqstuvwxyz]$") then
			syllables[#syllables] = "ˈ"..syllables[#syllables]
		else
			if #syllables > 1 then syllables[#syllables-1] = "ˈ"..syllables[#syllables-1] end
		end
	end

	table.insert(debug,word)
	
	local remove_accent = {['á']='a', ['é']='e', ['í']='i', ['ó']='o', ['ú']='u'}
	for i=1,#syllables do
		syllables[i] = mw.ustring.gsub(syllables[i],'[áéíóú]',remove_accent)
	end
	word = table.concat(syllables)	
	--secondary stress
	word = mw.ustring.gsub(word,'ˈ(.+)ˈ','ˌ%1ˈ')
	word = mw.ustring.gsub(word,'ˈ(.+)ˌ','ˌ%1ˌ')
	word = mw.ustring.gsub(word,'ˌ(.+)ˈ(.+)ˈ','ˌ%1ˌ%2ˈ')

	--softening i and u
	word = mw.ustring.gsub(word,'i([aeou])','i̯%1')
	
	--softening open and closed e and o
	word = mw.ustring.gsub(word,'e([bcdfghjklmnprstvwxyzɕθɟ])%.','ɛ%1')
	word = mw.ustring.gsub(word,'e([bcdfghjklmnprstvwxyzɕθɟ])([bcdfghjklmnprstvwxyzɕθɟ])%.','ɛ%1%2')
	word = mw.ustring.gsub(word,'e([bcdfghjklmnprstvwxyzɕθɟ])$','ɛ%1')
	word = mw.ustring.gsub(word,'e([bcdfghjklmnprstvwxyzɕθɟ])([bcdfghjklmnprstvwxyzɕθɟ])$','ɛ%1%2')
	word = mw.ustring.gsub(word,'o([bcdfghjklmnprstvwxyzɕθɟ])%.','ɔ%1')
	word = mw.ustring.gsub(word,'o([bcdfghjklmnprstvwxyzɕθɟ])([bcdfghjklmnprstvwxyzɕθɟ])%.','ɔ%1%2')
	word = mw.ustring.gsub(word,'o([bcdfghjklmnprstvwxyzɕθɟ])$','ɔ%1')
	word = mw.ustring.gsub(word,'o([bcdfghjklmnprstvwxyzɕθɟ])([bcdfghjklmnprstvwxyzɕθɟ])$','ɔ%1%2')

	
	table.insert(debug,word)
			
	--fixing fakes
	word = mw.ustring.gsub(word,'ɕ','t͡s') --fake c to real c
	word = mw.ustring.gsub(word,'θ','t͡ʃ') --fake ch to real ch
	word = mw.ustring.gsub(word,'ɟ','d͡ʒ') --fake j to real j

	word = mw.ustring.gsub(word,'([ɡkae])w','%1u̯')
	
	if do_debug == 'yes' then
		return word .. table.concat(debug,"")
	else
		return word
	end
end

function export.alternative(frame)
	return export.show(frame,true)
end

return export