var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.finishQuestion=exports.getQuestion=exports.askQuestion=exports.generateOptions=void 0;var _chalk=_interopRequireDefault(require("chalk"));

var highlight=_chalk.default.green;

var generateOptions=function generateOptions(inputData){var isMultiChoice=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var mapping=arguments.length>2?arguments[2]:undefined;var renderMethod=arguments.length>3?arguments[3]:undefined;
var asString='';
var valuesAsObject={};
var valuesAsArray=[];
var keysAsObject={};
var keysAsArray=[];
var isArray=Array.isArray(inputData);

var output={
pick:function pick(v,defaultOption){return new Promise(function(resolve,reject){
var selectedOptions=[];
var pickedOpt=v||defaultOption;
if(isMultiChoice){
var wrongOptions=[];
if(pickedOpt){
var choiceArr=v.split(',');
choiceArr.forEach(function(choice){
var selectedOption=choice;
if(isNaN(choice)){
selectedOption=choice;
}else{
selectedOption=keysAsArray[choice-1];
}
selectedOptions.push(selectedOption);
if(!valuesAsObject[selectedOption]){
wrongOptions.push(choice);
}
});
}else{
selectedOptions=keysAsArray;
}
if(wrongOptions.length){
reject(highlight(wrongOptions.join(','))+" ...Really?! \uD83D\uDE48");
}else{
output.selectedOptions=selectedOptions;
resolve(selectedOptions);
}
}else{
var selectedOption=pickedOpt;
if(isNaN(pickedOpt)){
selectedOption=pickedOpt;
}else{
selectedOption=keysAsArray[v-1];
}
if(!valuesAsObject[selectedOption]){
reject(highlight(pickedOpt)+" ...Really?! \uD83D\uDE48");
}else{
output.selectedOption=selectedOption;
resolve(selectedOption);
}
}
});}};

var renderer=renderMethod||_generateOptionString;
if(isArray){
inputData.map(function(v,i){
asString+=renderer(i,v,mapping,v);
valuesAsArray.push(v);
if(!mapping)keysAsArray.push(v);
if(!mapping)valuesAsObject[v]=v;
});
}else{
var i=0;
for(var k in inputData){
var v=inputData[k];
asString+=renderer(i,v,mapping,k);
keysAsArray.push(k);
keysAsObject[k]=true;
valuesAsObject[k]=v;
valuesAsArray.push(v);
i++;
}
}
output.keysAsArray=keysAsArray;
output.valuesAsArray=valuesAsArray;
output.keysAsObject=keysAsObject;
output.valuesAsObject=valuesAsObject;
output.asString=asString;
return output;
};exports.generateOptions=generateOptions;

var _generateOptionString=function _generateOptionString(i,obj,mapping,defaultVal){return"-["+highlight(i+1)+"] "+highlight(mapping?'':defaultVal)+" \n";};

var _currentQuestion;

var askQuestion=function askQuestion(question,obj,key){return new Promise(function(resolve,reject){
if(!_currentQuestion){
_currentQuestion=require('readline').createInterface({
input:process.stdin,
output:process.stdout});

}

_currentQuestion.question(getQuestion(question),function(v){
if(obj&&key)obj[key]=v;
resolve(v===''?null:v);
});
});};exports.askQuestion=askQuestion;

var getQuestion=function getQuestion(msg){return _chalk.default.blue("\n \u2753 "+msg+": ");};exports.getQuestion=getQuestion;

var finishQuestion=function finishQuestion(){return new Promise(function(resolve,reject){
_currentQuestion&&_currentQuestion.close();
_currentQuestion=null;
});};exports.finishQuestion=finishQuestion;