var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.findSuitableTask=exports.executeTask=exports.initializeTask=exports.getEngineRunner=exports.getEngineByPlatform=exports.registerEngine=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _logger=require("../systemManager/logger");
var _common=require("../common");
var _analytics=_interopRequireDefault(require("../systemManager/analytics"));
var _buildHooks=require("../projectManager/buildHooks");
var _prompt=require("../../cli/prompt");

var REGISTERED_ENGINES=[];
var ENGINES={};
var ENGINE_CORE='engine-core';

var registerEngine=function registerEngine(engine){
ENGINES[engine.getId()]=engine;
REGISTERED_ENGINES.push(engine);
};exports.registerEngine=registerEngine;

var getEngineByPlatform=function getEngineByPlatform(c,platform,ignoreMissingError){
var selectedEngineKey;
if(c.buildConfig&&!!platform){var _c$files$rnv$engines$,_c$files$rnv$engines$2;
selectedEngineKey=c.program.engine||(0,_common.getConfigProp)(c,platform,'engine');
var selectedEngine=(_c$files$rnv$engines$=c.files.rnv.engines.config)==null?void 0:(_c$files$rnv$engines$2=_c$files$rnv$engines$.engines)==null?void 0:_c$files$rnv$engines$2[selectedEngineKey];
if(!selectedEngine&&!ignoreMissingError){
(0,_logger.logDebug)("ERROR: Engine: "+selectedEngineKey+" does not exists or is not registered "+new Error());

}
return selectedEngine;
}
return null;
};exports.getEngineByPlatform=getEngineByPlatform;


var getEngineRunner=function getEngineRunner(c,task){
var selectedEngine=getEngineByPlatform(c,c.platform);var
configExists=c.paths.project.configExists;
if(!selectedEngine){
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];

throw new Error("Cound not find suitable executor for task "+(0,_logger.chalk)().white(task));
}
c.runtime.engineConfig=selectedEngine;
var engine=ENGINES[selectedEngine==null?void 0:selectedEngine.id];
if(!engine){
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];
throw new Error("Cound not find active engine with id "+(selectedEngine==null?void 0:selectedEngine.id)+". Available engines:\n        "+
Object.keys(ENGINES).join(', '));
}
if(engine.hasTask(task,configExists))return engine;
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];

throw new Error("Cound not find suitable executor for task "+(0,_logger.chalk)().white(task));
};exports.getEngineRunner=getEngineRunner;

var executedTasks={};

var initializeTask=function initializeTask(c,task){return _regenerator.default.async(function initializeTask$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('initializeTask',task);
c.runtime.task=task;
executedTasks={};

_analytics.default.captureEvent({
type:task+"Project",
platform:c.platform});return _context.abrupt("return",


executeTask(c,task,null,task));case 5:case"end":return _context.stop();}}},null,null,null,Promise);};exports.initializeTask=initializeTask;


var _executePipe=function _executePipe(c,task,phase){var subCmd;return _regenerator.default.async(function _executePipe$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
subCmd='';
if(c.subCommand){
subCmd=":"+c.subCommand;
}return _context2.abrupt("return",
(0,_buildHooks.executePipe)(c,""+task+subCmd+":"+phase));case 3:case"end":return _context2.stop();}}},null,null,null,Promise);};


var TASK_LIMIT=20;

var executeTask=function executeTask(c,task,parentTask,originTask){var pt,inOnlyMode,prt;return _regenerator.default.async(function executeTask$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
pt=parentTask?"=> ["+parentTask+"] ":'';
c._currentTask=task;
(0,_logger.logInitTask)(pt+"=> ["+(0,_logger.chalk)().bold.rgb(170,106,170)(task)+"]");
inOnlyMode=c.program.only;








if(!executedTasks[task])executedTasks[task]=0;if(!(
executedTasks[task]>TASK_LIMIT)){_context3.next=7;break;}return _context3.abrupt("return",
Promise.reject("You reached maximum amount of executions per one task ("+TASK_LIMIT+") task: "+task+".\nThis is to warn you ended up in task loop.\n("+

task+" calls same or another task which calls "+task+" again)\nbut issue migh not be necessarily with this task\n\nTo avoid that test your task code against parentTask and avoid executing same task X from within task X"));case 7:if(




inOnlyMode){_context3.next=10;break;}_context3.next=10;return _regenerator.default.awrap(_executePipe(c,task,'before'));case 10:_context3.next=12;return _regenerator.default.awrap(
getEngineRunner(c,task).executeTask(c,task,parentTask,originTask));case 12:if(
inOnlyMode){_context3.next=15;break;}_context3.next=15;return _regenerator.default.awrap(_executePipe(c,task,'after'));case 15:
executedTasks[task]++;

c._currentTask=parentTask;
prt=parentTask?"<= ["+(0,_logger.chalk)().rgb(170,106,170)(parentTask)+"] ":'';
(0,_logger.logExitTask)(prt+"<= "+task);case 19:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.executeTask=executeTask;


var _getTaskOption=function _getTaskOption(_ref){var taskInstance=_ref.taskInstance,hasMultipleSubTasks=_ref.hasMultipleSubTasks;
if(hasMultipleSubTasks){
return taskInstance.task.split(' ')[0]+"...";
}
if(taskInstance.description&&taskInstance.description!==''){
return taskInstance.task.split(' ')[0]+" "+(0,_logger.chalk)().grey("("+taskInstance.description+")");
}
return""+taskInstance.task.split(' ')[0];
};

var findSuitableTask=function findSuitableTask(c){var suitableTaskInstances,taskInstances,tasks,defaultCmd,tasksCommands,filteredTasks,addendum,_await$inquirerPrompt,command,task,suitableEngines,autocompleteEngines,isAutoComplete,message,supportedSubtasksArr,supportedSubtasks,supportedSubtasksFilter,subTasks,_await$inquirerPrompt2,subCommand,supportedPlatforms,platforms,_await$inquirerPrompt3,platform;return _regenerator.default.async(function findSuitableTask$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:if(
c.command){_context4.next=12;break;}
suitableTaskInstances={};
REGISTERED_ENGINES.forEach(function(engine){
engine.getTasks().forEach(function(taskInstance){
var key=taskInstance.task.split(' ')[0];
var hasMultipleSubTasks=false;
if(taskInstance.task.includes(' '))hasMultipleSubTasks=true;
suitableTaskInstances[key]={
taskInstance:taskInstance,
hasMultipleSubTasks:hasMultipleSubTasks};

});
});

taskInstances=Object.values(suitableTaskInstances);

defaultCmd='new';


addendum='';
if(!c.paths.project.configExists){
filteredTasks=taskInstances.filter(function(v){return v.taskInstance.isGlobalScope;});
tasks=filteredTasks.map(function(v){return _getTaskOption(v);}).sort();
tasksCommands=filteredTasks.map(function(v){return v.taskInstance.task.split(' ')[0];}).sort();
addendum=' (Not a ReNative project. options will be limited)';
}else{
tasks=taskInstances.map(function(v){return _getTaskOption(v);}).sort();
tasksCommands=taskInstances.map(function(v){return v.taskInstance.task.split(' ')[0];}).sort();
defaultCmd=tasks.find(function(v){return v.startsWith('run');});
}_context4.next=9;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
type:'list',
default:defaultCmd,
name:'command',
message:"Pick a command"+addendum,
choices:tasks,
pageSize:15,
logMessage:'Welcome to the brave new world...'}));case 9:_await$inquirerPrompt=_context4.sent;command=_await$inquirerPrompt.command;

c.command=tasksCommands[tasks.indexOf(command)];case 12:

task=c.command;
if(c.subCommand)task+=" "+c.subCommand;

suitableEngines=REGISTERED_ENGINES.filter(function(engine){return engine.hasTask(task,c.paths.project.configExists);});
autocompleteEngines=REGISTERED_ENGINES.filter(function(engine){return engine.getSubTasks(task,true).length;});

isAutoComplete=!suitableEngines.length&&!!c.command&&!autocompleteEngines.length;
message=isAutoComplete?"Autocomplete action for \""+c.command+"\"":"Pick a subCommand for "+c.command;if(

suitableEngines.length){_context4.next=33;break;}

supportedSubtasksArr=[];
REGISTERED_ENGINES.forEach(function(engine){
engine.getSubTasks(task).forEach(function(taskInstance){
var isNotViable=!c.paths.project.configExists&&!taskInstance.isGlobalScope;
if(!isNotViable){var _taskInstance$descrip;
var taskKey=isAutoComplete?taskInstance.task:taskInstance.task.split(' ')[1];

supportedSubtasksArr.push({
desc:(_taskInstance$descrip=taskInstance.description)==null?void 0:_taskInstance$descrip.toLowerCase==null?void 0:_taskInstance$descrip.toLowerCase(),
taskKey:taskKey});

}
});
});
supportedSubtasks={};

supportedSubtasksFilter={};
supportedSubtasksArr.forEach(function(tsk){
var mergedTask=supportedSubtasksFilter[tsk.taskKey];
if(!mergedTask){
supportedSubtasksFilter[tsk.taskKey]=tsk;
}else if(!mergedTask.desc.includes(tsk.desc)){
mergedTask.desc+=", "+tsk.desc;
}
});

Object.values(supportedSubtasksFilter).forEach(function(v){
var desc=v.desc?"("+v.desc+")":'';
var key=v.taskKey+" "+(0,_logger.chalk)().grey(desc);
supportedSubtasks[key]={
taskKey:v.taskKey};

});

subTasks=Object.keys(supportedSubtasks);if(!
subTasks.length){_context4.next=33;break;}_context4.next=29;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'list',
name:'subCommand',
message:message,
choices:subTasks}));case 29:_await$inquirerPrompt2=_context4.sent;subCommand=_await$inquirerPrompt2.subCommand;

if(isAutoComplete){
task=supportedSubtasks[subCommand].taskKey;
c.command=task.split(' ')[0];
c.subCommand=task.split(' ')[1];
if(c.subCommand){
task=c.command+" "+c.subCommand;
}else{
task=""+c.command;
}
}else{
c.subCommand=supportedSubtasks[subCommand].taskKey;
task=c.command+" "+c.subCommand;
}


suitableEngines=REGISTERED_ENGINES.filter(function(engine){return engine.hasTask(task,c.paths.project.configExists);});case 33:if(



suitableEngines.length){_context4.next=38;break;}
(0,_logger.logError)("could not find suitable task for "+(0,_logger.chalk)().white(c.command));
c.command=null;
c.subCommand=null;return _context4.abrupt("return",
findSuitableTask(c));case 38:if(


c.platform){_context4.next=48;break;}
supportedPlatforms={};
suitableEngines.forEach(function(engine){
engine.getTask(task).platforms.forEach(function(plat){
supportedPlatforms[plat]=true;
});
});
platforms=Object.keys(supportedPlatforms);if(!

platforms.length){_context4.next=48;break;}_context4.next=45;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'list',
name:'platform',
message:"Pick a platform for "+task,
choices:platforms}));case 45:_await$inquirerPrompt3=_context4.sent;platform=_await$inquirerPrompt3.platform;

c.platform=platform;case 48:


c.runtime.engine=getEngineRunner(c,task);
(0,_logger.logInfo)("Current Engine: "+(0,_logger.chalk)().bold.white(
c.runtime.engine.getId()));return _context4.abrupt("return",

c.runtime.engine.getTask(task));case 51:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.findSuitableTask=findSuitableTask;
//# sourceMappingURL=index.js.map