local temp = {};

------------------------------------------------------------------------------------------

temp table is a table of tables where the key is the language name and the value is a table listing the ISO 639
codes associated with that language name.

This function adds language name (as index) and its code (as a table element) using an appropriate index number.

 is the language name from the source data
 is the associated ISO 639 code from the source data
 for the override data is prefixed with 'O'


local function lang_add 
	part = ({																	-- convert string  to a numeric index
		['1']=1, ['2']=2, ['2B']=3, ['3']=4, ['5']=5,							-- for the base ISO 639 parts
		['O1']=6, ['O2']=7, ['O2B']=8, ['O3']=9, ['O5']=10,						-- for the override tables
		['D1']=11, ['D2']=12, ['D2B']=13, ['D3']=14, ['D5']=15,					-- for the deprecated tables

	lang = mw.ustring.lower (lang);												-- convert to lowercase for use as table index

	if not temp[lang] then														-- when no entry for this language
		temp[lang] = {};														-- make a blank entry
	end

	table.insert(temp[lang], string.format)			-- add the code; codes are added in the order that this function is called in iso_639_name_to_code

end

-- TODO: better name
local function iterate_table
	for code, v in pairs (part_data) do											
		for _, lang in ipairs (v) do											-- code can have multiple names so for each one
			lang_add ;									-- create and / or add this name / code pair to the output
		end
	end	
end

--------------------------------------------------------------------

read code-to-name source tables and convert to a name-to-code table.


local function iso_639_name_to_code 
	local out = {};

	local part_data = mw.loadData ('Module:Language/data/ISO 639-3');			-- ISO 639-3 language codes / names
	iterate_table												-- start with part 3 because it has the most codes

	part_data = mw.loadData ('Module:Language/data/ISO 639-5');					-- ISO 639-5 language codes / names
	iterate_table

	part_data = mw.loadData ('Module:Language/data/ISO 639-2');					-- ISO 639-2 language codes / names
	iterate_table

	part_data = mw.loadData ('Module:Language/data/ISO 639-2B');				-- ISO 639-2B language codes / names
	iterate_table

	part_data = mw.loadData ('Module:Language/data/ISO 639-1');					-- ISO 639-1 language codes / names
	iterate_table

	part_data = mw.loadData ('Module:Language/data/ISO 639 override');			-- has override data for all parts
	for _, o_part in ipairs  do						-- for each of the override tables
		local o_part_data = part_data['override_' .. o_part];					-- point to override data
		o_part = 'O' .. o_part;													-- prefix o_part
		iterate_table										-- for each code in the data table and for each language name associated with that code
	end

	part_data = mw.loadData ('Module:Language/data/ISO 639 deprecated');		-- has deprecated data for all parts
	for _, d_part in ipairs  do						-- for each of the deprecated tables
		local d_part_data = part_data['deprecated_' .. d_part];					-- point to deprecated data
		d_part = 'D' .. d_part;													-- prefix d_part
		iterate_table										-- for each code in the data table and for each language name associated with that code
	end

	local function comp 
		return tonumber (a:match ('(%d+)')) < tonumber (b:match ('(%d+)'));
	end

	for lang, codes in pairs (temp) do
		table.sort ;														-- codes are added in the order that lang_add is called above; sort to make pretty
		table.insert (out, table.concat ({'["', lang, '"] = {', table.concat , '}'}));	-- reformat
	end

	table.sort (out);															-- sort in language name order

	local key_str = table.concat ({
		'--[[--------------------------',
		'Key:&#9;',
		'[1]=ISO 639-1&#9;&#9;[6]=ISO 639-1 override&#9;&#9;[11]=ISO 639-1 deprecated&#9;',
		'[2]=ISO 639-2&#9;&#9;[7]=ISO 639-2 override&#9;&#9;[12]=ISO 639-2 deprecated&#9;',
		'[3]=ISO 639-2B&#9;&#9;[8]=ISO 639-2B override&#9;&#9;[13]=ISO 639-2B deprecated&#9;',
		'[4]=ISO 639-3&#9;&#9;[9]=ISO 639-3 override&#9;&#9;[14]=ISO 639-3 deprecated&#9;',
		'[5]=ISO 639-5&#9;&#9;[10]=ISO 639-5 override&#9;&#9;[15]=ISO 639-5 deprecated',
		''
	
	return table.concat ({''});	-- render
end

----------------------------------------------------------------------