local getArgs = require ('Module:Arguments').getArgs;
local unicode = require ("Module:Unicode data");								-- for is_latin
local lang = require ('Module:Lang');

local namespace = mw.title.getCurrentTitle.namespace;							-- used for categorization

local err_texts = {																-- because wrapper templates have Latn/non-Latn text ordered according to the wrapper template's name
	['lang-sh-cyrl-latn'] = true,												-- this template has |1=cyrl script |2=latn script

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

assembles an error message from template name, message text, help link, and error category.


local function error_msg_make 
	local out = {};
	local category = 'Lang and lang-xx';

	table.insert (out, table.concat ({'');
	
	if (0 == namespace or 10 == namespace) and not nocat then					-- categorize in article space (and template space to take care of broken usages)
		table.insert (out, table.concat ({'Category:Lang and lang-xx template errors'}));
	end

	return table.concat (out);
end
	

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

allowed separators are comma, semicolon, virgule, or a quoted string of text


local function separator_get (sep_param_val)
	if not sep_param_val then return ', ' end;									-- not specified, use default
	if ',' == sep_param_val then return ', ' end;
	if ';' == sep_param_val then return '; ' end;
	if '/' == sep_param_val then return '/' end;
	local str;
	if sep_param_val:match ('^%b\"\"$') then									-- if quoted string and uses double quotes
		str = sep_param_val:match ('%b\"\"'):gsub :gsub ;
		return str;																-- return the contents of the quote
	elseif sep_param_val:match ('^%b\'\'$') then								-- if quoted string and uses single quotes
		str = sep_param_val:match ('%b\'\''):gsub :gsub ;
		return str;																-- return the contents of the quote
	end
	return ', ';																-- default in case can't extract quoted string
end

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

Selects the appropriate enumerated parameters for 

 - arguments table from frame
 - arguments table to be supplied to _lang (whichever text renders second)
 - arguments table to be supplied to _lang_xx_inherit or _lang_xx_italic (whichever text renders first)


local function param_select 
	local enum_xx = swap and '2' or '1';
	local enum = swap and '1' or '2';
	
	lang_xx_args_t.script = args_t['script' .. enum_xx];						-- these for  that renders first (uses )
	lang_xx_args_t.region = args_t['region' .. enum_xx];
	lang_xx_args_t.variant = args_t['variant' .. enum_xx];
	lang_xx_args_t.italic = args_t['italic' .. enum_xx];
	lang_xx_args_t.size = args_t['size' .. enum_xx];
	
	lang_args_t.italic = args_t['italic' .. enum];								-- these for  that renders second (uses )
	lang_args_t.size = args_t['size' .. enum];
end

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

make ietf tag for _lang (second rendered )


local function ietf_tag_make 
	local ietf_tag_t = {tag};													-- initialize with 
	local enum = swap and '1' or '2';											-- when  is rendered second by _lang
	
	for _, param in ipairs  do
		if args_t[param] then table.insert  end		-- build erated subtags
	end

	return table.concat ;										-- concatenate subtags with hyphen separator and done
end

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

mimics except that  (which is always the
Latin-script form). Intended for languages where two scripts are 'official' or 'native and equal in status' (sh
is and sr may be a candidate for this; are there others?)

	
	 - (required) Latin-script text (always)
	 - (required) non-Latin-script text (always)

parameters supported by both of and templates are passed along:
	
parameters supported only by that are not enumerated:
	
enumerated parameters:



this function calls:
	Module:Lang functions
		_lang_xx_inherit - when non-Latin  renders first
		_lang_xx_italic - when Latin  renders first
		_lang - to render 
	Module:Unicode data function:
		is_Latin - error checking to make sure that  is Latin-script text


local function _lang_x2 (args_t)
	local tag = args_t.tag or args_t[1];
	local text1 = args_t.text1 or args_t[2];
	local text2 = args_t.text2 or args_t[3];
	local translation = args_t.translation or args_t[4];
	local template_name = args_t.template_name or 'lang-x2';
	local nocat = ('yes' == args_t.nocat) or ('no' == args_t.cat);				-- boolean

	if not (tag and text1 and text2) then
		return error_msg_make ;
	end
	if tag:find  then
		return error_msg_make ;
	end
	if not unicode.is_Latin (text1) then
--error (template_name)
		return error_msg_make ;
	end
	if unicode.is_Latin (text2) then
		return error_msg_make ;
	end
	
	local swap = 'yes' == args_t.swap;											-- boolean
	local out = {};

	local ietf_tags = ietf_tag_make ;						-- for calls to 
																				-- create base-form arguments tables	
	local lang_xx_args_t = {['code']=tag, ['label']=args_t.label, ['link']=args_t.link, ['cat']=args_t.cat, ['nocat']=args_t.nocat, ['cat']=args_t.cat};	-- for whichever  renders first
	local lang_args_t = {['code']=ietf_tags, ['cat']=args_t.cat, ['nocat']=args_t.nocat, ['cat']=args_t.cat};		-- for whichever  renders second
	
	param_select ;					-- load 

	if swap then																-- non-Latin script  renders first
		lang_xx_args_t.text = text2;
		lang_args_t.text = text1;
		table.insert (out, lang._lang_xx_inherit (lang_xx_args_t));				-- render non-Latin script  upright
	else																		-- Latin script  renders first
		lang_xx_args_t.text = text1;
		lang_args_t.text = text2;
		table.insert (out, lang._lang_xx_italic (lang_xx_args_t));				-- render Latin script  in italics
	end

	table.insert (out, separator_get (args_t.separator));						-- insert the separator
	table.insert (out, lang._lang (lang_args_t));								-- and render the other of 
	
	if translation then															-- if positional parameter 4 (translation of )
		table.insert (out, lang._translation_make );	-- add translation to rendering
	end

	return table.concat (out)
end

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

implements ; template entry point


local function lang_x2 (frame)
	local args_t = getArgs (frame);
	return _lang_x2 (args_t);
end

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

return {
	lang_x2 = lang_x2,