Module:languages/javascript-interface

local export = {}

function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

--used by translation adder for language autocompletion
function export.GetSingleLanguageByLangaugePrefix(prefix)
	local byName = mw.loadData("Module:languages/by name")
	local found_code = nil
	local found_name = nil
	for name, code in pairs(byName) do
		if (string.starts(name, prefix.args[1])) then 
			if found_code == nil or found_code == code then
				found_code = code
				found_name = name
			else
				return ""
			end
		end
	end
	if found_code ~= nil then
		return found_code..":"..found_name
	else 
		return "" end
end

--translation adder may prefetch all data and do processing on client side
function export.GetAllData()
	local byName = mw.loadData("Module:languages/by name")
	return require("Module:JSON").toJSON(byName)
end

function export.AllCanonicalToCode()
	local byCanonicalName = mw.loadData("Module:languages/canonical names")
	return require("Module:JSON").toJSON(byCanonicalName)
end

function export.AllLangcodeToScripts()
	local resultData = {}

	local function iterate(list)
		for code, data in pairs(list) do
			resultData[code] = data.scripts
		end
	end

	iterate(mw.loadData("Module:languages/data2"))

	for letter in mw.ustring.gmatch("abcdefghijklmnopqrstuvwxyz", "(.)") do
		iterate(mw.loadData("Module:languages/data3/" .. letter))
	end

	iterate(mw.loadData("Module:languages/datax"))

	return require("Module:JSON").toJSON(resultData)
end



function export.GetLanguagesWithAutomaticTransliteration()
	local resultData = {}

	local function iterate(list)
		for code, data in pairs(list) do
			if data.override_translit == true then
				resultData[code] = true
			end
		end
	end

	iterate(mw.loadData("Module:languages/data2"))

	for letter in mw.ustring.gmatch("abcdefghijklmnopqrstuvwxyz", "(.)") do
		iterate(mw.loadData("Module:languages/data3/" .. letter))
	end

	iterate(mw.loadData("Module:languages/datax"))

	return require("Module:JSON").toJSON(resultData)
end

function export.AllWiktionaryCodeToWikimediaCode()
	local resultData = {}

	local function iterate(list)
		for code, data in pairs(list) do
			if data.wikimedia_codes ~= nil then
				resultData[code] = data.wikimedia_codes
			end
		end
	end

	iterate(mw.loadData("Module:languages/data2"))

	for letter in mw.ustring.gmatch("abcdefghijklmnopqrstuvwxyz", "(.)") do
		iterate(mw.loadData("Module:languages/data3/" .. letter))
	end

	iterate(mw.loadData("Module:languages/datax"))

	return require("Module:JSON").toJSON(resultData)
end


return export