var _ = require('underscore');
var parse = require('./parse');
var opener = require('./open');

/**
 *
 * @param ids
 * @param options
 * @param callback
 */
function amdify(ids, options, callback) {
	opener(ids, options.steal || {}, function (error, rootSteals, opener) {
		amdify.amdifySteals(rootSteals, _.extend({
			opener: opener
		}, options), callback);
	});
}

_.extend(amdify, {
	defaults: {
		wrapper: 'define(<% if(dependencies.length) { %>["<%= dependencies.join(\'\\", \\"\') %>"], <% } %><%= parsed %>);'
	},

	/**
	 * For a given Steal object, convert the id into an AMD module name. Use mappings
	 * if given (e.g. to map ever can/ to something else)
	 *
	 * @param {Object} stl The Steal object
	 * @param {Object} mappings An object containing all mappings
	 * @returns {String} The mapped name
	 */
	convertName: function(stl, mappings) {
		var src = ('' + stl.options.id).replace('.' + stl.options.ext, '');
		var parts = src.split('/');
		var mapping;

		if (parts[parts.length - 1] == parts[parts.length - 2]) {
			parts.pop();
			src = parts.join('/');
		}

		for(var key in mappings) {
			mapping = mappings[key];
			// Match for mappings that end with `/`
			if (/\/$/.test(key) && src.indexOf(key) === 0) {
				return src.replace(key, mapping)
			}
			// Direct match
			if (src === key) {
				return mapping;
			}
		}

		return src;
	},

	amdifySteals: function (steals, options, callback) {
		var results = {};

		if (!callback) {
			callback = options;
			options = {};
		}

		options = _.extend({}, amdify.defaults, options);

		// Go through all dependencies
		opener.visit(steals, function (stl, id, visited, index) {
			if(stl.options.text) {
				var dependencies = stl.dependencies.map(function (dep) {
					return amdify.convertName(dep, options.map);
				});

				// The last dependency is the module itself, we don't need that
				dependencies.pop();

				if (!stl.options.parsed) {
					// Extract only the function() {} part from a steal call
					// and add it to our current steal object
					stl.options.parsed = parse(stl.options.text);
				}

				results[amdify.convertName(stl)] = _.template(options.wrapper, {
					parsed: stl.options.parsed
						.replace(/(\s?)\/\/!steal-remove-start((.|\n)*?)\/\/!steal-remove-end/gim, '')
						.replace(/(\s?)\/\/!pluginify-remove-start((.|\n)*?)\/\/!pluginify-remove-end/gim, ''),
					dependencies: dependencies
				});
			}
		});

		callback(null, results);
	}
});

module.exports = amdify;

@@ -1231,22 +1231,21 @@
 ) %7B%0A%09%09%09%09
-return
+src =
  src.rep
@@ -1313,22 +1313,21 @@
 ) %7B%0A%09%09%09%09
-return
+src =
  mapping
@@ -2113,16 +2113,29 @@
 Name(stl
+, options.map
 )%5D = _.t

Fix AMDify