local export = {}

local m_IPA = require("Module:IPA")
local m_hyph = require("Module:fi-hyphenation")
local m_str_utils = require("Module:string utilities")

local find = m_str_utils.find
local gmatch = m_str_utils.gmatch
local gsub = m_str_utils.gsub
local len = m_str_utils.len
local lower = m_str_utils.lower
local match = m_str_utils.match
local sub = m_str_utils.sub
local U = m_str_utils.char

local langcode = "fi"

local nonsyllabic = U(0x32F) -- inverted breve below
local unreleased = U(0x31A)
local nasalized = U(0x303)
local long = "ː"

local letters_phonemes = {
	["a"] = "ɑ",
	["ä"] = "æ",
	["ö"] = "ø",
	["å"] = "o",
	
	["g"] = "ɡ",
	["q"] = "k",
	["v"] = "ʋ",
	["š"] = "ʃ",
	["ž"] = "ʒ",
	
	["x"] = "ks",
	["zz"] = "ts",
	["ng"] = "ŋː",
	["nk"] = "ŋk",
	["nkk"] = "ŋkː",
	["qu"] = "kʋ",
	["*"] = "ˣ",
	["'"] = ".",
}

local lookahead = 3 -- how many unstressed syllables at most in a single unit, thus max consecutive unstressed syllables

local vowels = "ɑeiouyæø"
local vowel = "[" .. vowels .. "]"
local consonants = "kptɡgbdfʔsnmŋlrhʋʃʒrjçɦx"
local consonant = "[" .. consonants .. "]"
local diacritics = "̝̞̠̪"
local diacritic = "[" .. diacritics .. "]"

local spelled_consonants = "cšvwxzž"
local spelled_consonant = "[" .. consonants .. spelled_consonants .. "]"
local spelled_vowels = "aäö"
local spelled_vowel = "[" .. vowels .. spelled_vowels .. "]"

local tertiary = "ˌ" -- "tertiary stress", a weaker secondary stress (either rhythmic or in some compound words). is there a better way to represent this?
export.tertiary = tertiary

local stress_indicator = "[ ˈˌ" .. tertiary .. "/-]"
local plosives = "kptbdɡ"

local stress_p = "[ˈˌ" .. tertiary .. "]"
local stress_s = "[ˌ" .. tertiary .. "]"
local stress_pd = "[ˈˌ" .. tertiary .. "%.]"
local stress_sd = "[ˌ" .. tertiary .. "%.]"

local replacements_narrow = {
	["ɑ"] = "ɑ̝",
	["e"] = "e̞",
	["ø"] = "ø̞",
	["o"] = "o̞",
	["t"] = "t̪",
	["s"] = "s̠"
}

--	This adds letters_phonemes["e"] = "e", letters_phonemes["i"] = "i", etc.
for letter in gmatch("eiouydhfjklmnprstu", ".") do
	letters_phonemes[letter] = letter
end

--[[	This regex finds the diphthongs in the IPA transcription,
		so that the nonsyllabic diacritic can be added.						]]
-- /_i/ diphthongs can appear in any syllable
local diphthongs_i = {
	"[ɑeouyæø]i"
}
-- /_U/ diphthongs can appear in the initial syllable or later open syllables (no consonantal coda)
local diphthongs_u = {
	"[ɑoei]u",
	"[eiæø]y",
}
-- rising diphthongs can only appear in the initial syllable (of a word, compound word part, etc.)
local diphthongs_rising = {
	"uo",
	"ie",
	"yø",
}

local function apply_post_fixes(p)
	-- initial <gn> is /gn/
	p = gsub(p, "ˈŋn", "ˈɡn")

	-- ŋ is short before consonant (by default)
	p = gsub(p, "ŋ"..long.."("..consonant..")", "ŋ%1")

	-- dissimilation of vowels by sandhi
	p = gsub(p, "("..vowel..diacritic.."*)(["..long..nonsyllabic.."]?)("..stress_s..")%1", "%1%2%3(ʔ)%1")

	return p
end

local function apply_post_fixes_narrow(p)
	-- t is alveolar in /ts/ and /st/
	p = gsub(p, "t̪("..stress_pd.."?%s*)s̠", "t%1s̠")
	p = gsub(p, "s̠("..stress_pd.."?%s*)t̪", "s̠%1t")
	-- n is dental in /nt/
	p = gsub(p, "n("..stress_pd.."?%s*)t̪", "n̪%1t̪")
	p = gsub(p, "t̪("..stress_pd.."?%s*)n", "t̪%1n̪")
	-- l allophone
	p = gsub(p, "l("..stress_pd.."?%s*)t̪", "l̪%1t̪")
	p = gsub(p, "t̪("..stress_pd.."?%s*)l", "t̪%1l̪")

	-- long j, v after i, u diphthong
	p = gsub(p, "(i"..nonsyllabic..")j("..vowel..")", "%1j("..long..")%2")
	-- /ʋ/ after /u/ usually realized as /w/ (see Suomi, Toivanen and Ylitalo 2008, p. )
	p = gsub(p, "(u"..nonsyllabic..")ʋ("..vowel..")", "%1w("..long..")%2")
	-- cleanup
	p = gsub(p, "("..stress_s..")%.", "%1")
	p = gsub(p, "("..stress_pd..")"..stress_s, "%1")

    -- tautosyllabic nasals nasalize vowels between them (see Suomi, Toivanen and Ylitalo 2008, p. 22)
	--p = gsub(p, "([mnŋ]"..long.."?)("..vowel..")("..diacritic.."*)([mnŋ])(.?)", function (n0, nv, nvd, n1, anchor)
	--	-- this cannot be simplified to "(.?)" => "([^" .. vowels .. "]?)", otherwise a vowel after would match
	--	if not find(anchor, vowel) then
	--		return n0 .. nv .. nasalized .. nvd .. n1 .. anchor
	--	end
	--end)
	
	-- sandhi: nm > mm, np > mp, nb > mb, nk > ŋk, ng > ŋg
	p = gsub(p, "nm", "m" .. long)
	p = gsub(p, "n("..stress_pd.."?%s*)([ɡk])", "ŋ%1%2")
	p = gsub(p, "n("..stress_pd.."?%s*)([mpb])", "m%1%2")
	p = gsub(p, "[nm]("..stress_pd.."?%s*)([f])", "ɱ%1%2")

	-- handle potentially long consonants over secondary stresses
	p = gsub(p, "("..consonant..diacritic.."*)%("..long.."%)("..stress_s..")", "%2%1("..long..")")
	p = gsub(p, "("..stress_s..")("..consonant..diacritic.."*)%("..long.."%)", "(%2)%1%2")
    p = gsub(p, "(ŋ"..diacritic.."*)"..tertiary.."ɡ", "%1"..tertiary.."ŋ")
	
	-- [k] allophone before front vowels (see Suomi, Toivanen and Ylitalo 2008, p. 27)
	p = gsub(p, "k([eiyæø])", "k̟%1")
	
	return p
end

function export.is_light_syllable(syllable)
	return find(lower(syllable), "^[" .. m_hyph.sep_symbols .. "]?" .. spelled_consonant .. "?" .. spelled_vowel .. "%(?%*?%)?$")
end

function export.has_later_heavy_syllable(hyph, start)
	local stop = math.min(start + lookahead, #hyph - 1)
	for index = start, stop do
		if not export.is_light_syllable(hyph[index]) then
			return true
		end
	end
	return false	
end

-- applied *before* IPA conversion
local function add_secondary_stress(word)
	-- keep_sep_symbols = true
	local hyph = m_hyph.generate_hyphenation(word, true)
	local res = ""
	local last_index = #hyph
	
	-- find stressed syllables and add secondary stress before each syllable
	for index, syllable in ipairs(hyph) do
		local stressed = false
		local has_symbol = find(syllable, "^[" .. m_hyph.sep_symbols .. "ˈˌ" .. tertiary .. "]")
		
		if has_symbol then
			-- check if symbol indicates stress
			stressed = find(syllable, "^" .. stress_indicator)
			has_symbol = stressed
		end
			
		if not stressed then
			if index == 1 then
				stressed = true
			elseif not prev_stress and index < last_index then
				-- shift stress if current syllable light and a heavy syllable occurs later (except as the last syllable)
				stressed = index == last_index - 1 or not export.is_light_syllable(syllable) or not export.has_later_heavy_syllable(hyph, index + 1)
			end
			
			if stressed then
				last_stressed = index
			end
		end
		
		-- check if next syllable already stressed
		-- if is, do not stress this syllable
		if stressed and index < last_index then
			stressed = stressed and not find(hyph[index + 1], "^" .. stress_indicator)
		end

		if index > 1 and stressed and not has_symbol then
			res = res .. "-$"
		end
		res = res .. syllable

		prev_stress = stressed
	end

	local noninitial = {}
	local index = 1
	res = gsub(res, "-([$]?)",
		function (dollar)
			index = index + 1
			noninitial[index] = #dollar > 0
			return #dollar > 0 and tertiary or "-"
		end)
	
	return res, noninitial
end

local function handle_diphthongs(IPA, strict_initial)
	-- Add nonsyllabic diacritic after last vowel of diphthong.
	for _, diphthong_regex in pairs(diphthongs_i) do
		IPA = gsub(IPA, diphthong_regex, "%0" .. nonsyllabic)
	end

	local only_initial = stress_indicator .. "[^" .. vowels .. "]*"
	if strict_initial then
		only_initial = "^[^" .. vowels .. "]*"
	end

	for _, diphthong_regex in pairs(diphthongs_rising) do
		-- initial syllables
		IPA = gsub(IPA, only_initial .. diphthong_regex, "%0" .. nonsyllabic)
	end

	for _, diphthong_regex in pairs(diphthongs_u) do
		-- initial syllables
		IPA = gsub(IPA, only_initial .. diphthong_regex, "%0" .. nonsyllabic)

		local open_noninitial = 
			function(diphthong, after)
				if find(after, "^" .. consonant .. diacritic .. "*" .. vowel) then
					-- consonant after diphthong
					-- must be followed by vowel so that it's part of the
					-- following syllable, else it's in this syllable
					-- and thus this syllabie is closed

					return diphthong .. nonsyllabic .. after
				elseif find(after, "^" .. consonant) then
					-- consonant after diphthong
					-- must be in this syllable

					return diphthong .. after
				end
				-- no consonant after diphthong => open
				return diphthong .. nonsyllabic .. after
			end

		-- open non-initial syllables
		IPA = gsub(IPA, "(" .. diphthong_regex .. ")([^" .. nonsyllabic .. "].+)", open_noninitial)
		IPA = gsub(IPA, "(" .. diphthong_regex .. ")($)", open_noninitial)
	end

	return IPA
end

local function IPA_word(term, is_narrow, has_initial)
	local rest = term
	local phonemes = {}
	
	while len(rest) > 0 do
		-- Find the longest string of letters that matches a recognised sequence in the list
		local longestmatch = ""
		
		for letter, phoneme in pairs(letters_phonemes) do
			if sub(rest, 1, len(letter)) == letter and len(letter) > len(longestmatch) then
				longestmatch = letter
			end
		end
		
		-- Convert the string to IPA
		if len(longestmatch) > 0 then
			table.insert(phonemes, letters_phonemes[longestmatch])
			rest = sub(rest, len(longestmatch) + 1)
		else
			table.insert(phonemes, sub(rest, 1, 1))
			rest = sub(rest, 2)
		end
	end
	
	local result = table.concat(phonemes)
	
	if is_narrow then
		-- articulation of h (Suomi, Toivanen & Ylitalo 2008, p. 28)
		result = gsub(result, "(.?)h(.?)",
			function (before, after)
				local h
				if after ~= "" and after ~= "h" then
					if before ~= "" and vowels:find(before) then
						if consonants:find(after) then
							-- vihma, yhtiö
							if before == "i" or before == "y" then
								h = "ç"
							-- mahti, kohme, tuhka
							elseif before == "ɑ" or before == "o" or before == "u" then
								h = "x"
							end
						-- maha
						elseif vowels:find(after) then
							h = "ɦ"
						end
					end
				end
				
				if h then
					return before .. h .. after
				end
			end)
		
		-- double letter replacement and diphthongs must be handled earlier here
		result = gsub(result, "(%a)%1", "%1" .. long)
		if has_initial then
			result = handle_diphthongs(result, true)
		end
	
		for letter, phoneme in pairs(replacements_narrow) do
			result = gsub(result, letter, phoneme)
		end
	end
	
	return result
end

function export.IPA_wordparts(term, is_narrow)
	term = lower(term)
	local notinitial = {} -- true if the component is not an initial component
	local hyphenstress = "ˌ" -- secondary by default
	local is_prefix = false
	local is_suffix = false

	if find(term, "%/") then
		hyphenstress = tertiary -- tertiary if we have slashes
	end
	
	if is_narrow then
		term, notinitial = add_secondary_stress(term)
	end
	
	local found
	term, found = gsub(term, "^%-+", "")
	is_suffix = found > 0
	term, found = gsub(term, "%-+$", "")
	is_prefix = found > 0
	
	-- make sure we keep slashes to figure out if secondary or tertiary
	term = gsub(term, "%/", "-%1")
	local wordparts = mw.text.split(term, "-", true)

	for key, val in ipairs(wordparts) do
		local stress = key > 1 and hyphenstress or "ˈ"
		local part = val

		if find(part, "^%/") then
			stress = "ˌ" -- always secondary
			part = part:sub(2)
		end
		if find(part, "ˈ") then
			stress = ""
		end

		wordparts[key] = stress .. IPA_word(part, is_narrow, not notinitial[key])
	end
	
	IPA = table.concat(wordparts, "")
	
	if is_narrow then
		-- handle * in narrow transcription
		IPA = gsub(IPA, "ˣ(%)?%s*"..stress_p.."?)((.?)" .. diacritic .. "*)",
			function (post, after, potential_consonant)
				if potential_consonant == "" then
					if find(post, "^%)") then
						return "ʔ" .. post .. after
					else
						return post .. "(ʔ)" .. after
					end
				elseif consonants:find(potential_consonant) then
					if #post > 0 then
						local amark = ""
						if plosives:find(sub(after, 1, 1)) then
							amark = unreleased
						end
						return after .. amark .. post .. after
					else
						return post .. after .. long
					end
				else
					return post .. "ʔ" .. after
				end
			end)		
	else
		--	Replace double letters (vowels or consonants) with single letter plus length sign.
		IPA = gsub(IPA, "(%a)%1", "%1" .. long)
		IPA = handle_diphthongs(IPA, false)
	end
	
	IPA = apply_post_fixes(IPA)
	
	if is_narrow then
		IPA = apply_post_fixes_narrow(IPA)
	end
	
	if is_prefix then
		IPA = IPA .. "-"
	end
	if is_suffix then
		IPA = "-" .. IPA
	end
	
	return IPA
end

function export.IPA(term)
	if type(term) == "table" then
		term = term:getParent().args[1]
	end
	
	local title = mw.title.getCurrentTitle().text
	
	if not term then
		term = title
	elseif term == "*" then
		term = title .. "*"
	end
	
	local no_count = match(term, " ")
	
	IPA_narrow = export.IPA_wordparts(term, true)
	IPA = export.IPA_wordparts(term, false)
	return m_IPA.format_IPA_full {
		lang = require("Module:languages").getByCode(langcode),
		items = {{pron = "/" .. IPA .. "/"}, {pron = "[" .. IPA_narrow .. "]"}},
		no_count = no_count,
	}
end

return export