This module implements the {{ka-IPA}} template.


local export = {}

local ipa = require("Module:IPA")
local lang = require("Module:languages").getByCode("ka")

local gsub = mw.ustring.gsub
local lower = mw.ustring.lower

local mapping = {
	["ა"] = "a", ["ბ"] = "b", ["გ"] = "ɡ", ["დ"] = "d",
	["ე"] = "e", ["ვ"] = "v", ["ზ"] = "z", ["თ"] = "tʰ",
	["ი"] = "i", ["კ"] = "kʼ", ["ლ"] = "l", ["მ"] = "m",
	["ნ"] = "n", ["ო"] = "o", ["პ"] = "pʼ", ["ჟ"] = "ʒ",
	["რ"] = "ɾ", ["ს"] = "s", ["ტ"] = "tʼ", ["უ"] = "u",
	["ფ"] = "pʰ", ["ქ"] = "kʰ", ["ღ"] = "ʁ", ["ყ"] = "χʼ",
	["შ"] = "ʃ", ["ჩ"] = "t͡ʃʰ", ["ც"] = "t͡sʰ", ["ძ"] = "d͡z",
	["წ"] = "t͡sʼ", ["ჭ"] = "t͡ʃʼ", ["ხ"] = "χ", ["ჯ"] = "d͡ʒ",
	["ჰ"] = "h",
	["ჶ"] = "f" -- special character
}

function export.pronunciation(word)
	-- make text lowercase
	word = lower(word)

	require("Module:script utilities").checkScript(word, "Geor")

	-- /v/ is labialised to the previous consonant
	word = gsub(word, "([ბგდვზთკლმნპჟრსტფქღყშჩცძწჭხჯჰ])ვ", "%1ʷ")

	-- /v/ is devoiced before a devoiced consonant
	word = gsub(word, "ვ([თკპსტფქშჩცწჭხჰ])", "f%1")

	-- /l/ is velarised before a back vowel
	word = gsub(word, "ლ([აოუ])", "ɫ%1")

	-- /n/ is velarised before a velar consonant
	word = gsub(word, "ნ([ქკგ])", "ŋ%1")

	-- word-initial /b, d, ɡ/ are devoiced
	local bdg_initial = { ["ბ"] = "b̥", ["დ"] = "d̥", ["გ"] = "ɡ̊" }
	word = gsub(word, "^([ბდგ])", bdg_initial)
	word = gsub(word, "(%s)([ბდგ])", function(s, c) return s .. bdg_initial[c] end)

	-- word-final /b, d, ɡ/ are devoiced and aspirated
	local bdg_final = { ["ბ"] = "ფ", ["დ"] = "თ", ["გ"] = "ქ" }
	word = gsub(word, "([ბდგ])$", bdg_final)
	word = gsub(word, "([ბდგ])(%s)", function(c, s) return bdg_final[c] .. s end)

	word = gsub(word, "%p", "")
	word = gsub(word, ".", mapping)

	return word
end

function export.show(frame)
	local args = frame:getParent().args
	local pagetitle = mw.title.getCurrentTitle().text

	local p, results = {}, {}

	if args[1] then
		for _, v in ipairs(args) do
			if v == "+" then v = pagetitle end
			table.insert(p, (v ~= "") and v or nil)
		end
	else
		if mw.title.getCurrentTitle().nsText == "Template" then
			p = {"ქართული ენა"}
		else p = { pagetitle } end
	end

	local has_f = false

	for input_index, word in ipairs(p) do
		-- check for character ჶ /f/
		if mw.ustring.find(word, "ჶ") then
			has_f = true
		end

		local phon = export.pronunciation(word)

		table.insert(results, { pron = "[" .. phon .. "]" })

		local qual = args["q"..input_index]
		if qual then
			results[#results].qualifiers = { qual }
		end

	end

	local ret = ipa.format_IPA_full { lang = lang, items = results }

	if has_f then
		ret = ret .. require("Module:utilities").format_categories({"Georgian terms with /f/"}, lang)
	end

	return ret
end

return export