Documentation for this module may be created at မော်ဂျူး:etymology/specialized/doc

local export = {}


-- This function handles all the messiness of different types of specialized borrowings. It should insert any
-- borrowing-type-specific categories into `categories` unless `nocat` is given, and return the text to display
-- before the source + term (or "" for no text).
function export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat)
	local function non_glossary_link(entry, text)
		text = text or entry
		if not nocap then
			text = mw.getContentLanguage():ucfirst(text)
		end
		return "[[" .. entry .. "|" .. text .. "]]"
	end


	local function glossary_link(entry, text)
		text = text or entry
		return non_glossary_link("နောက်ဆက်တွဲ:ခက်ဆစ်#" .. entry, text)
	end

	local function inscat(cat)
		table.insert(categories, lang:getCanonicalName() .. " " .. cat)
	end

	local text, category
	if bortype == "calque" then
		text = glossary_link("calque") .. " ၏ "
		category = "terms calqued from "
	elseif bortype == "partial-calque" then
		text = glossary_link("partial calque") .. " ၏ "
		category = "terms partially calqued from "
	elseif bortype == "semantic-loan" then
		text = glossary_link("semantic loan") .. " မှ "
		category = "semantic loans from "
	elseif bortype == "transliteration" then
		text = glossary_link("transliteration") .. " ၏ "
		category = "transliterations of SOURCE terms"
		-- It seems the intent of the former code was to insert two categories, but it never worked.
		-- if not nocat then
		--	inscat(" terms borrowed from " .. source:getDisplayForm())
		-- end
	elseif bortype == "phono-semantic-matching" then
		text = glossary_link("phono-semantic matching") .. " ၏ "
		category = "phono-semantic matchings from "
	else
		local lang_is_source = lang:getCode() == source:getCode()
		if not nocat then
			if lang_is_source then
				inscat(" twice-borrowed terms")
			else
				inscat(" terms borrowed from " .. source:getDisplayForm())
				category = bortype .. " borrowings from "
			end
		end
	
		if bortype == "learned" then
			text = glossary_link("learned borrowing") .. " မှ "
		elseif bortype == "semi-learned" then
			text = glossary_link("semi-learned borrowing") .. " မှ "
		elseif bortype == "orthographic" then
			text = glossary_link("orthographic borrowing") .. " မှ "
		elseif bortype == "unadapted" then
			text = glossary_link("unadapted borrowing") .. " မှ "
		else
			error("Internal error: Unrecognized bortype: " .. bortype)
		end
	end

	if category and not nocat then
		local sourcedisp = source:getDisplayForm()
		if category:find("SOURCE") then
			category = category:gsub("SOURCE", sourcedisp)
		else
			category = category .. sourcedisp
		end
		inscat(category)
	end

	return text
end


function export.specialized_borrowing(bortype, lang, terminfo, sort_key, nocap, notext, nocat)
	local m_etymology = require("Module:etymology")
	local categories = {}
	local source = terminfo.lang
	local text = export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat)
	
	text = notext and "" or text
	return text .. m_etymology.format_etyl(lang, source, sort_key, categories, nocat) ..
		m_etymology.process_and_create_link(terminfo, template_name)
end


function export.specialized_multi_borrowing(bortype, lang, sources, terminfo, sort_key, nocap, notext, nocat, conj)
	local categories = {}
	local text

	for _, source in ipairs(sources) do
		text = export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat)
	end
	
	text = notext and "" or text
	return text .. require("Module:etymology/multi").format_sources(lang, sources, terminfo, sort_key, categories, nocat, conj) ..
		require("Module:etymology").process_and_create_link(terminfo, template_name)
end


return export