"format cjs";

// TODO: cleanup removing package.json
var utils = require('./npm-utils');
var convert = require("./npm-convert");
var crawl = require('./npm-crawl');
var npmLoad = require("./npm-load");
var isNode = typeof process === "object" &&
	{}.toString.call(process) === "[object process]";

// SYSTEMJS PLUGIN EXPORTS =================

/**
 * @function translate
 * @description Convert the package.json file into a System.config call.
 * @signature `translate(load)`
 * @param {Object} load Load object
 * @return {Promise} a promise to resolve with the load's new source.
 */
exports.translate = function(load){
	var loader = this;

	// This could be an empty string if the fetch failed.
	if(load.source == "") {
		return "define([]);";
	}

	var resavePackageInfo = isNode && loader.isEnv &&
		!loader.isEnv("production");
	var prevPackages = loader.npmContext && loader.npmContext.pkgInfo;
	var context = {
		packages: [],
		loader: this,
		// places we load package.jsons from
		paths: {},
		// paths that are currently be loaded
		loadingPaths: {},
		versions: {},
		fetchCache: {},
		deferredConversions: {},
		npmLoad: npmLoad,
		crawl: crawl,
		resavePackageInfo: resavePackageInfo,
		forwardSlashMap: {},
		// default file structure for npm 3 and higher
		isFlatFileStructure: true
	};
	this.npmContext = context;
	var pkg = {origFileUrl: load.address, fileUrl: utils.relativeURI(loader.baseURL, load.address)};
	crawl.processPkgSource(context, pkg, load.source);

	var pkgName = pkg.name;
	// backwards compatible for < npm 3
	if(pkg.system && pkg.system.npmAlgorithm === "nested") {
		context.isFlatFileStructure = false;
	} else {
		pkg.system = pkg.system || {};
		pkg.system.npmAlgorithm = "flat";
	}

	return crawl.deps(context, pkg, true).then(function(){
		// clean up packages so everything is unique
		var names = {};
		var packages = context.pkgInfo = [];
		utils.forEach(context.packages, function(pkg, index){
			if(!packages[pkg.name+"@"+pkg.version]) {
				if(pkg.browser){
					delete pkg.browser.transform;
				}
				if(pkg.system && pkg.system.npmAlgorithm === "nested" && context.isFlatFileStructure === true) {
					utils.warn("Different npmAlgorithm between '" + pkgName +"' " +
						"and '" + pkg.name + "'. Do not mix npmAlgorithm between npm modules!")
				}
				packages.push({
					name: pkg.name,
					version: pkg.version,
					fileUrl: utils.relativeURI(context.loader.baseURL, pkg.fileUrl),
					main: pkg.main,
					system: convert.system(context, pkg, pkg.system, index === 0),
					globalBrowser: convert.browser(pkg, pkg.globalBrowser),
					browser: convert.browser(pkg, pkg.browser || pkg.browserify),
					jspm: convert.jspm(pkg, pkg.jspm),
					jam: convert.jspm(pkg, pkg.jam)
				});
				packages[pkg.name+"@"+pkg.version] = true;
			}
		});

		var source = npmLoad.makeSource(context, pkg);

		npmLoad.addExistingPackages(context, prevPackages);

		return source;
	});
};

@@ -1480,33 +1480,8 @@
 );%0A%0A
-%09var pkgName = pkg.name;%0A
 %09// 
@@ -2020,262 +2020,8 @@
 %09%09%7D%0A
-%09%09%09%09if(pkg.system && pkg.system.npmAlgorithm === %22nested%22 && context.isFlatFileStructure === true) %7B%0A%09%09%09%09%09utils.warn(%22Different npmAlgorithm between '%22 + pkgName +%22' %22 +%0A%09%09%09%09%09%09%22and '%22 + pkg.name + %22'. Do not mix npmAlgorithm between npm modules!%22)%0A%09%09%09%09%7D%0A
 %09%09%09%09

remove warning