require('strict')

local rawData = mw.title.getCurrentTitle().subpageText == 'sandbox'
	and mw.loadData('Module:IPA/data/sandbox')
	or mw.loadData('Module:IPA/data')
local data = {}

local function getNameAndLink(code)
	local res = require('Module:Lang')._name_from_tag({ code, link = 'yes' })
	local name = res:match('([^%[|%]]+)%]%]$')
	local link = res:match('^%[%[([^|%]]+)')
	return name, link
end

local function augmentData(lect)
	if lect.name and not lect.link then
		lect.generatedLink = lect.name:find(' languages$')
			and lect.name
			or lect.name .. ' language'
	end
	lect.extName, lect.extLink = getNameAndLink(lect.code)
end

for code, lang in pairs(rawData.langs) do
	local langData = {
		code = code,
		name = lang.name,
		text = lang.text,
		link = lang.link,
		key = lang.key
	}
	augmentData(langData)
	table.insert(data, langData)
	if lang.dialects then
		local aliases = {}
		for diaCode, dialect in pairs(lang.dialects) do
			if dialect.aliasOf then
				aliases[dialect.aliasOf] = aliases[dialect.aliasOf] or {}
				table.insert(aliases[dialect.aliasOf], diaCode)
			end
		end
		for _, t in pairs(aliases) do
			table.sort(t)
		end
		for diaCode, dialect in pairs(lang.dialects) do
			if not dialect.aliasOf then
				local diaAliases = aliases[diaCode]
				if dialect.isVariant then
					diaCode = diaCode:lower()
				end
				local diaData = {
					code = code .. '-' .. diaCode,
					aliases = diaAliases,
					name = dialect.name,
					text = dialect.text,
					link = dialect.link,
					key = dialect.key,
					parent = langData
				}
				augmentData(diaData)
				table.insert(data, diaData)
			end
		end
	end
end

table.sort(data, function (a, b) return a.code < b.code end)

return data
"https://si.wikipedia.org/w/index.php?title=Module:IPA/data/export&oldid=661350" වෙතින් සම්ප්‍රවේශනය කෙරිණි