var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.PIPES=void 0;

var _chalk=_interopRequireDefault(require("chalk"));
var _fs=_interopRequireDefault(require("fs"));
var _readline=_interopRequireDefault(require("readline"));
var _constants=require("../constants");
var _common=require("../common");



var _prompt=require("../systemTools/prompt");
var _buildHooks=require("../projectTools/buildHooks");
var _fileutils=require("../systemTools/fileutils");

var LIST='list';
var ADD='add';
var UPDATE='update';

var PIPES={
PLUGIN_LIST_BEFORE:'plugin:list:before',
PLUGIN_LIST_AFTER:'plugin:list:after',
PLUGIN_ADD_BEFORE:'plugin:add:before',
PLUGIN_ADD_AFTER:'plugin:add:after',
PLUGIN_UPDATE_BEFORE:'plugin:update:before',
PLUGIN_UPDATE_AFTER:'plugin:update:after'};exports.PIPES=PIPES;






var run=function run(c){
(0,_common.logTask)('run');

switch(c.subCommand){
case LIST:
return(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_LIST_BEFORE).
then(function(){return _runList(c);}).
then(function(){return(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_LIST_AFTER);});
case ADD:
(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_ADD_BEFORE);
return _runAdd(c).then(function(){return(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_ADD_AFTER);});
case UPDATE:
(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_UPDATE_BEFORE);
return _runUpdate(c).then(function(){return(0,_buildHooks.executePipe)(c,PIPES.PLUGIN_UPDATE_AFTER);});
default:
return Promise.reject("cli:plugin: Sub-Command "+_chalk.default.white.bold(c.subCommand)+" not supported!");}

};





var _runList=function _runList(c){return new Promise(function(resolve){
(0,_common.logTask)('_runList');

var o=_getPluginList(c);

console.log(o.asString);

resolve();
});};

var _getPluginList=function _getPluginList(c){var isUpdate=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var
plugins=c.files.rnv.pluginTemplates.config.plugins;
var output={
asString:'',
plugins:[],
json:plugins};


var i=1;

Object.keys(plugins).forEach(function(k){
var p=plugins[k];

var platforms='';
_constants.SUPPORTED_PLATFORMS.forEach(function(v){
if(p[v])platforms+=v+", ";
});
if(platforms.length)platforms=platforms.slice(0,platforms.length-2);
var installedPlugin=c.buildConfig&&c.buildConfig.plugins&&c.buildConfig.plugins[k];
var installedString=installedPlugin?_chalk.default.red('installed'):_chalk.default.green('not installed');
if(isUpdate&&installedPlugin){
output.plugins.push(k);
var versionString;
if(installedPlugin.version!==p.version){
versionString="("+_chalk.default.red(installedPlugin.version)+") => ("+_chalk.default.green(p.version)+")";
}else{
versionString="("+_chalk.default.green(installedPlugin.version)+")";
}
output.asString+="-["+i+"] "+_chalk.default.white(k)+" "+versionString+"\n";
i++;
}else if(!isUpdate){
output.plugins.push(k);
output.asString+="-["+i+"] "+_chalk.default.white(k)+" ("+_chalk.default.blue(p.version)+") ["+platforms+"] - "+installedString+"\n";
i++;
}
});

return output;
};

var _runAdd=function _runAdd(c){return new Promise(function(resolve){
(0,_common.logTask)('_runAdd');

var o=_getPluginList(c);

console.log(o.asString);

var readlineInterface=_readline.default.createInterface({
input:process.stdin,
output:process.stdout});


readlineInterface.question((0,_prompt.getQuestion)('Type plugin numbers you want to add (comma separated)'),function(v){
var choices=v.split(',');

var selectedPlugins={};
var msg='Installing: \n';

choices.forEach(function(choice){
var i=parseInt(choice,10)-1;
var key=o.plugins[i];
if(key){
selectedPlugins[key]=o.json[key];
msg+="- "+_chalk.default.white(key)+" v("+_chalk.default.green(o.json[key].version)+")\n";
}
});
console.log(msg);

Object.keys(selectedPlugins).forEach(function(key){

c.files.project.config.plugins[key]='source:rnv';


_checkAndAddDependantPlugins(c,selectedPlugins[key]);
});

(0,_fileutils.writeObjectSync)(c.paths.project.config,c.files.project.config);

(0,_common.logSuccess)('Plugins installed successfully!');

resolve();
});
});};

var _checkAndAddDependantPlugins=function _checkAndAddDependantPlugins(c,plugin){
var templatePlugins=c.files.rnv.pluginTemplates.config.plugins;
if(plugin.dependsOn){
plugin.dependsOn.forEach(function(v){
if(templatePlugins[v]){
console.log("Added dependant plugin "+v);
c.buildConfig.plugins[v]=templatePlugins[v];
}
});
}
};

var _runUpdate=function _runUpdate(c){return new Promise(function(resolve){
(0,_common.logTask)('_runUpdate');

var o=_getPluginList(c,true);

console.log(o.asString);

(0,_prompt.askQuestion)('Above installed plugins will be updated with RNV. press (y) to confirm').
then(function(v){
(0,_prompt.finishQuestion)();var
plugins=c.buildConfig.plugins;
Object.keys(plugins).forEach(function(key){

c.files.project.config.plugins[key]=o.json[key];
});

(0,_fileutils.writeObjectSync)(c.paths.project.config,c.files.project.config);

(0,_common.logSuccess)('Plugins updated successfully!');

resolve();
});
});};var _default=



run;exports.default=_default;