<commit_before>
var path = require('path');
var _ = require('lodash');
var appConfigUtils = require('./webpack-app-module-config-utils.js');
var settingsUtils = require('./settings-utils.js');

var asrCoreId = 'mmir-plugin-encoder-core.js';
var ttsCoreId = 'audiotts.js';

var applyToAllSpeechConfigs = '__apply-to-all-configs__';
var ALL_SPEECH_CONFIGS_TYPE = settingsUtils.getAllSpeechConfigsType();

/**
 * mapping from file-name to load-module-ID in MediaManager/plugin-loading mechanism
 * (to be used in configuration.json)
 */
var moduleIdMap = {
	'webaudioinput.js': 'mmir-plugin-encoder-core.js',
	'mmir-plugin-tts-core-xhr.js': 'audiotts.js'
};

/**
 * for webAudioInput module's specific service/implementations:
 * mapping from file-name to load-module-ID in MediaManager/plugin-loading mechanism
 * (to be used in configuration.json)
 */
var implFileIdMap = {
	'webasrgoogleimpl.js': 'mmir-plugin-asr-google-xhr.js',
	'webasrnuanceimpl.js': 'mmir-plugin-asr-nuance-xhr.js'
};

var deprecatedImplFileIdMap = {
	'webasratntimpl.js': 'webasrAtntImpl.js',
	'webasrgooglev1impl.js': 'webasrGooglev1Impl.js'
};

function isAsrPlugin(pluginId){
	return /^mmir-plugin-asr-/.test(pluginId);
}

function addResolveAliasPaths(alias, paths){
	for(var p in paths){
		paths[p] = path.resolve(paths[p]);
		alias[p] = paths[p];
	}
	return paths;
}

function normalizeModName(modName){
	return modName.replace(/\.js$/i, '');
}

function getPluginEntryFrom(confEntry, pluginConfigList){

	var simpleModName = normalizeModName(confEntry.mod);
	var simpleConfigName = normalizeModName(confEntry.config);
	var ctx = confEntry.ctx || false;
	return pluginConfigList.find(function(entry){
		if(typeof entry === 'string'){
			return normalizeModName(entry) === simpleModName;
		}
		return (
			normalizeModName(entry.mod) === simpleModName &&
			normalizeModName(entry.config) === simpleConfigName &&
			(!ctx || entry.ctx === ctx)
		);
	});
}

function normalizeImplName(implName){
	if(!/\.js$/i.test(implName)){
		implName += '.js';
	}
	return implName.toLowerCase();
}

function normalizeMediaManagerPluginConfig(configList){
	var id, normalized;
	configList.forEach(function(entry, index, list){
		if(typeof entry === 'string'){
			id = implFileIdMap[normalizeImplName(entry)];
			if(id){
				list[index] = id;
			}
		} else {
			if(typeof entry.mod === 'string'){
				id = moduleIdMap[normalizeImplName(entry.mod)];
				if(id){
					entry.mod = id;
				}
			}
			if(typeof entry.config === 'string'){
				normalized = normalizeImplName(entry.config);
				id = moduleIdMap[normalized];
				if(id){
					entry.config = id;
				} else if(deprecatedImplFileIdMap[normalized]){
					console.log('WARN: found deprecated media-plugin '+deprecatedImplFileIdMap[normalized]+' in configuration; this plugin will probably not work ', entry);
				}
			}
		}
	});
}

function getConfigEnv(pluginConfig, pluginInfo, runtimeConfig){

	var env = pluginConfig && pluginConfig.env;
	if(!env && pluginInfo.defaultValues && pluginInfo.defaultValues.env){
		env = pluginInfo.defaultValues.env;
		if(!Array.isArray(env)){
			env = [env];
		}
	}
	if(!env && runtimeConfig.mediaManager && runtimeConfig.mediaManager.plugins){
		env = Object.keys(runtimeConfig.mediaManager.plugins);
	}
	if(!env){
		//FIXME TODO get default env-definitions from MediaManager!
		env = ['browser', 'cordova'];
	}
	if(typeof env === 'string'){
		env = [env];
	}
	// console.log('########### using env', env);
	return env;
}

/**
 * create plugin-entry in mediaManager.plugin configuration
 *
 * @param  {MMIRPluginConfig} pluginConfig the (user supplied) plugin-configuration
 * @param  {MMIRPluginInfo} pluginConfigInfo the plugin-info
 * @param  {string} pluginId the plugin-ID
 * @return {MediaManagerPluginEntry} the plugin-entry for mediaManager.plugin
 */
function createConfigEntry(pluginConfig, pluginConfigInfo, pluginId){
	var isAsr = isAsrPlugin(pluginId);
	var mod = pluginConfigInfo.defaultValues && pluginConfigInfo.defaultValues.mod;
	// console.log('#### config entry for '+pluginConfigInfo.pluginName+' default mod? ', JSON.stringify(mod));//DEBUG
	if(!mod){
		// console.log('#### config entry for '+pluginConfigInfo.pluginName+' default mod? ', mod);//DEBUG
		mod = isAsr? asrCoreId : ttsCoreId;
	}
	var config = normalizeImplName(pluginId);//TODO
	var ctx = (pluginConfig && pluginConfig.ctx) || void(0);

	//{ "mod": "mmir-plugin-encoder-core.js", "config": "mmir-plugin-asr-nuance-xhr.js"}
	return {mod: mod, config: config, ctx: ctx};
}

/**
 * apply config-values from pluginConfig to runtimeConfig into entry pluginConfigInfo.pluginName
 *
 * @param  {MMIRPluginConfig} pluginConfig the plugin-configuration
 * @param  {Configuration} runtimeConfig the main configuration (JSON-like) object
 * @param  {Settings} settings the application settings (dicitionaries, speech-configs etc)
 * @param  {MMIRPluginInfo} pluginConfigInfo the plugin-info
 */
function applyPluginSpeechConfig(pluginConfig, settings, pluginConfigInfo){

	var speechConfs = pluginConfigInfo.speechConfig;
	if(speechConfs){

		var applyList = [];<commit_msg>BUGFIX need to look up re-mapping for mod-value of config-entries<commit_after>
var path = require('path');
var _ = require('lodash');
var appConfigUtils = require('./webpack-app-module-config-utils.js');
var settingsUtils = require('./settings-utils.js');

var asrCoreId = 'mmir-plugin-encoder-core.js';
var ttsCoreId = 'audiotts.js';

var applyToAllSpeechConfigs = '__apply-to-all-configs__';
var ALL_SPEECH_CONFIGS_TYPE = settingsUtils.getAllSpeechConfigsType();

/**
 * mapping from file-name to load-module-ID in MediaManager/plugin-loading mechanism
 * (to be used in configuration.json)
 */
var moduleIdMap = {
	'webaudioinput.js': 'mmir-plugin-encoder-core.js',
	'mmir-plugin-tts-core-xhr.js': 'audiotts.js'
};

/**
 * for webAudioInput module's specific service/implementations:
 * mapping from file-name to load-module-ID in MediaManager/plugin-loading mechanism
 * (to be used in configuration.json)
 */
var implFileIdMap = {
	'webasrgoogleimpl.js': 'mmir-plugin-asr-google-xhr.js',
	'webasrnuanceimpl.js': 'mmir-plugin-asr-nuance-xhr.js'
};

var deprecatedImplFileIdMap = {
	'webasratntimpl.js': 'webasrAtntImpl.js',
	'webasrgooglev1impl.js': 'webasrGooglev1Impl.js'
};

function isAsrPlugin(pluginId){
	return /^mmir-plugin-asr-/.test(pluginId);
}

function addResolveAliasPaths(alias, paths){
	for(var p in paths){
		paths[p] = path.resolve(paths[p]);
		alias[p] = paths[p];
	}
	return paths;
}

function normalizeModName(modName){
	return modName.replace(/\.js$/i, '');
}

function getPluginEntryFrom(confEntry, pluginConfigList){

	var simpleModName = normalizeModName(confEntry.mod);
	var simpleConfigName = normalizeModName(confEntry.config);
	var ctx = confEntry.ctx || false;
	return pluginConfigList.find(function(entry){
		if(typeof entry === 'string'){
			return normalizeModName(entry) === simpleModName;
		}
		return (
			normalizeModName(entry.mod) === simpleModName &&
			normalizeModName(entry.config) === simpleConfigName &&
			(!ctx || entry.ctx === ctx)
		);
	});
}

function normalizeImplName(implName){
	if(!/\.js$/i.test(implName)){
		implName += '.js';
	}
	return implName.toLowerCase();
}

function normalizeMediaManagerPluginConfig(configList){
	var id, normalized;
	configList.forEach(function(entry, index, list){
		if(typeof entry === 'string'){
			id = implFileIdMap[normalizeImplName(entry)];
			if(id){
				list[index] = id;
			}
		} else {
			if(typeof entry.mod === 'string'){
				id = moduleIdMap[normalizeImplName(entry.mod)];
				if(id){
					entry.mod = id;
				}
			}
			if(typeof entry.config === 'string'){
				normalized = normalizeImplName(entry.config);
				id = moduleIdMap[normalized];
				if(id){
					entry.config = id;
				} else if(deprecatedImplFileIdMap[normalized]){
					console.log('WARN: found deprecated media-plugin '+deprecatedImplFileIdMap[normalized]+' in configuration; this plugin will probably not work ', entry);
				}
			}
		}
	});
}

function getConfigEnv(pluginConfig, pluginInfo, runtimeConfig){

	var env = pluginConfig && pluginConfig.env;
	if(!env && pluginInfo.defaultValues && pluginInfo.defaultValues.env){
		env = pluginInfo.defaultValues.env;
		if(!Array.isArray(env)){
			env = [env];
		}
	}
	if(!env && runtimeConfig.mediaManager && runtimeConfig.mediaManager.plugins){
		env = Object.keys(runtimeConfig.mediaManager.plugins);
	}
	if(!env){
		//FIXME TODO get default env-definitions from MediaManager!
		env = ['browser', 'cordova'];
	}
	if(typeof env === 'string'){
		env = [env];
	}
	// console.log('########### using env', env);
	return env;
}

/**
 * create plugin-entry in mediaManager.plugin configuration
 *
 * @param  {MMIRPluginConfig} pluginConfig the (user supplied) plugin-configuration
 * @param  {MMIRPluginInfo} pluginConfigInfo the plugin-info
 * @param  {string} pluginId the plugin-ID
 * @return {MediaManagerPluginEntry} the plugin-entry for mediaManager.plugin
 */
function createConfigEntry(pluginConfig, pluginConfigInfo, pluginId){
	var isAsr = isAsrPlugin(pluginId);
	var mod = pluginConfigInfo.defaultValues && pluginConfigInfo.defaultValues.mod;
	// console.log('#### config entry for '+pluginConfigInfo.pluginName+' default mod? ', JSON.stringify(mod));//DEBUG
	if(!mod){
		// console.log('#### config entry for '+pluginConfigInfo.pluginName+' default mod? ', mod);//DEBUG
		mod = isAsr? asrCoreId : ttsCoreId;
	} else {
		mod = moduleIdMap[normalizeImplName(mod)] || mod;
	}
	var config = normalizeImplName(pluginId);//TODO should only create plugin-ID-config, if necessary (i.e. for "sub-module implementations", not for "main module plugins" like android-speech plugin)
	var ctx = (pluginConfig && pluginConfig.ctx) || void(0);

	//{ "mod": "mmir-plugin-encoder-core.js", "config": "mmir-plugin-asr-nuance-xhr.js"}
	return {mod: mod, config: config, ctx: ctx};
}

/**
 * apply config-values from pluginConfig to runtimeConfig into entry pluginConfigInfo.pluginName
 *
 * @param  {MMIRPluginConfig} pluginConfig the plugin-configuration
 * @param  {Configuration} runtimeConfig the main configuration (JSON-like) object
 * @param  {Settings} settings the application settings (dicitionaries, speech-configs etc)
 * @param  {MMIRPluginInfo} pluginConfigInfo the plugin-info
 */
function applyPluginSpeechConfig(pluginConfig, settings, pluginConfigInfo){

	var speechConfs = pluginConfigInfo.speechConfig;
	if(speechConfs){

		var applyList = [];
{'fraction_non_alphanumeric': 0.14513663290655457, 'fraction_numerical': 0.0005732849226065354, 'mean_word_length': 4.906884875846501, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1038882', 'n_tokens_mistral': 3593, 'n_tokens_neox': 3378, 'n_words': 729}