local getArgs = require('Module:Arguments').getArgs

--[[--------------------------------------------------------------------------------

local function err_msg_make (msg)
	return ''
end

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

local or required entry point

return true if  is a mediawiki recognized code; false else

 - language code to validate; expected to be lowercase without leading/trailing whitespace
 - language code for target language; expected to be valid; expected to be lowercase without leading/trailing whitespace

specifying  may be a pointless exercise because of cldr fallback. For example,
	mw.language.fetchLanguageName  -> Crimean Turkish
because the Albanian language definitions do not have an Albanian-language version of the language name


local function _is_code 
	code = mw.language.fetchLanguageName ;
	return '' ~= code;
end

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

validates target_lang_code as a know language code; returns two values
	when target_lang_code is valid, first return value holds target_lang_code; second return value is nil
	when target_lang_code is invalid, first retrun value is nil; second return value has error message

if target_lang_code argument is nil, (missing or empty in the invoke) use local wiki's language code


local function __validate_target_lang_code (target_lang_code)
	local msg;
	
	if target_lang_code then													-- not missing or empty
		if not _is_code (target_lang_code) then									-- validate target_lang_code
			msg = err_msg_make ('target language code: ' .. target_lang_code);
			target_lang_code = nil;												-- unset as invalid
		end
	end
	if not target_lang_code then												-- if nil because missing or empty or because invlaid and we set it nil
		target_lang_code = mw.getContentLanguage:getCode;					-- use local wiki's language code
	end

	return target_lang_code, msg;												-- target_lang_code is valid or nil; msg is nil or has an error message
end

--[[------------------------------------------------------------------------------------------

module entry point

args[1]: language code -> 
args[2]: optional target language code; same as  in ; defaults to the local wiki language

return true if  is a mediawiki recognized code; nil else

]]

local function is_code (frame)
	local args = getArgs (frame, {
		valueFunc = function 
			return (value and '' ~= value) and value:lower:gsub ('^%s*(.-)%s*$', '%1') or nil;
		end
	local code = args[1];
	local target_lang_code = __validate_target_lang_code (args[2]);

	return code and _is_code  and true or nil;
end

--[[----------------------------------------------------------------------------

module entry point

args[1]: language code
args[2]: optional target language code; same as  in ; defaults to the local wiki language

return language-name if language-code is a mediawiki recognized code; error message string else

returned language name not guarenteed to be in target_lang_code (if specified), because mw language lists are incomplete

]]

local function name_from_code (frame)
	local args = getArgs (frame, {
		valueFunc = function 
			return (value and '' ~= value) and value:lower:gsub ('%s*(.-)%s*', '%1') or nil;
		end
	local code = args[1];
	if not code then
		return err_msg_make ('코드: (비었음)');
	end
	
	local target_lang_code, msg = __validate_target_lang_code (args[2]);
	if msg then
		return msg;
	end

	local name = mw.language.fetchLanguageName ;		-- returns empty string if code not found
	return '' ~= name and name or err_msg_make ('언어 코드: ' .. code);		-- return language name or error message
end

--[[----------------------------------------------------------------------------

local entry point

args[1]: language name
args[2]: optional target language code; instruct this function to fetch language name list in 'this' language

return language-code if language-name is a mediawiki recognized name and target language code is valid; error message string else

second return value is a boolean used by is_name; true when name is found; false else

]]

local function _code_from_name (args)
	local name = args[1];
	if not name then
		return err_msg_make ('이름: (비었음)');
	end

	local target_lang_code, msg = __validate_target_lang_code (args[2]);

	if msg then
		return msg;
	end

	local code_name_list = mw.language.fetchLanguageNames ;	-- get language code / name list in target_lang_code language indexed by language code
	local name_code_list = {};													-- to hold language name / code list indexed by name

	for k, v in pairs (code_name_list) do										-- spin through the code / name list and
		name_code_list[v:lower] = k;											-- make a name / code list
	end

	if name_code_list[name] then
		return name_code_list[name], true;										-- returns code when name is found and true for is_name
	else
		return err_msg_make ('언어 이름: ' .. name), false;					-- return error message when name not found and false for is_name
	end
end

--[[----------------------------------------------------------------------------

module entry point

args[1]: language name
args[2]: optional target language code; instruct this function to fetch language name list in 'this' language

return language-code if language-name is a mediawiki recognized name and target language code is valid; error message string else

]]

local function code_from_name (frame)
	local args = getArgs (frame, {
		valueFunc = function 
			return (value and '' ~= value) and value:lower:gsub ('^%s*(.-)%s*$', '%1') or nil;
		end
	
	local result, _ = _code_from_name (args);									-- suppress true/false return used by is_name
	return result;
end

--[[------------------------------------------------------------------------------------------

return true if  is a mediawiki recognized language name; false else

args[1]: language name
args[2]: optional target language code; instructs _code_from_name to fetch language name list in 'this' language
			defaults to local wiki's language; when this parameter not valid, language name is assumed to be not valid

]]

local function is_name (frame)
	local args = getArgs (frame, {
		valueFunc = function 
			return (value and '' ~= value) and value:lower:gsub ('%s*(.-)%s*', '%1') or nil;
		end
	
	local _, result = _code_from_name (args);									-- suppress code return used by code_from_name
	return result and true or nil;
end

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

return {
	code_from_name = code_from_name,
	is_code = is_code,
	is_name = is_name,
	name_from_code = name_from_code,

	_is_code = _is_code,														-- entry point from another module