var path = require("path");
var fs = require("fs");

module.exports = function(config) {
	var templateContentsPath = config.templateContentsPath;

	function readTemplateContents (route) {
		// get folder contents based on route
		var checkFolder = fs.readdirSync(route);
		var templateContents = {};
		
		// loop through items in current folder
		checkFolder.forEach(function(folder) {
			// assign the updated path a variable for the recursive call
			var newPath = path.join(route, folder);

			// if the folder contains files
			if (fs.lstatSync(newPath).isFile()) {
				// append file contents on the templateContents object
				templateContents[path.basename(newPath, ".json")] = JSON.parse(fs.readFileSync(newPath, "utf8"));
				return;
			}

			// build the templateContents JSON on each iteration
			templateContents[folder] = readTemplateContents(newPath);
		});
		return templateContents;
	}

	return readTemplateContents(templateContentsPath);	
};