local paradigms = require('Module:es-conj/data/paradigms')
 
local export = {}

function export.create(frame, args, forms, combined)
	local ending = args[1]
	local pattern = args[2]
	local stems = args[3]
	local ref = args[4]

	local ending_data = deepcopy(require("Module:es-conj/data/"..ending))
	local pattern_data = deepcopy(require("Module:es-conj/data/"..ending))
	if pattern ~= ending then
		pattern_data = deepcopy(require("Module:es-conj/data/"..ending.."/"..pattern))
	end

	local defective = pattern_data.defective
	if not defective then
		defective = {}
	end
	
	local defective_t = {}
	for k1,v1 in pairs(defective) do
		defective_t[v1] = true
	end
	
	if (ending ~= pattern) then
		local template_name = paradigms[ending][pattern]
		
		if (mw.ustring.sub(template_name, 1, 3) == ending) then
			template_name = mw.ustring.sub(template_name, 5, mw.ustring.len(template_name))
		end
		
		if (mw.ustring.sub(template_name, 1, 1) == '(') then
			template_name = mw.ustring.sub(template_name, 2, mw.ustring.len(template_name)-1)
		end
		
		local footer = frame:expandTemplate({title = "Wt/sco/Spanish conjugation type",
									         args = {[1] = ending, [2] = template_name}})
	
		forms["footer"] = "Other verbs with this conjugation: "..footer
	else
		forms["footer"] = ""
	end
	
	if (defective_t[69]) then
		forms["d69"] = defective_t[69]
	end
	if (defective_t[70]) then
		forms["d70"] = defective_t[70]
	end
	if (defective_t[71]) then
		forms["d71"] = defective_t[71]
	end
	if (defective_t[72]) then
		forms["d72"] = defective_t[72]
	end
	if (defective_t[73]) then
		forms["d73"] = defective_t[73]
	end
	
	if (ending ~= pattern) then
		for k1,v1 in pairs(pattern_data.patterns) do
			forms["i"..k1] = true
		end
	end
	
	if (ending ~= pattern) then
		forms["data_url"] = ending .. '/' .. mw.ustring.gsub(pattern, "%s+", "_")
	else
		forms["data_url"] = ending
	end
	
	local result =  frame:expandTemplate({title = "Module:es-conj/templates/es-conj",
		                         args = forms})
		                         
	if combined then
		if ref then
			result = result.."\n"..frame:expandTemplate({title = "Module:es-conj/templates/es-conj-combined-refl", args = combined})
		else
			result = result.."\n"..frame:expandTemplate({title = "Module:es-conj/templates/es-conj-combined", args = combined})
		end
	end
	
	return result
	
end

return export