"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable});keys.push.apply(keys,symbols)}return keys}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(Object(source),true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key])})}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source))}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))})}}return target}let _require=require("@corenode/utils"),verbosity=_require.verbosity,objectToArrayMap=_require.objectToArrayMap;
verbosity=verbosity.options({method:"[CLI]"});

const _cli=global.corenode_cli;
const yargs=require("yargs/yargs");const _require2=
require("yargs/helpers"),hideBin=_require2.hideBin;

let cmdKeys=[];var _default=

({commands,options})=>{var _cli$custom,_process$args$_$splic;
let custom=(_cli$custom=_cli===null||_cli===void 0?void 0:_cli.custom)!==null&&_cli$custom!==void 0?_cli$custom:[];
const argvf=(_process$args$_$splic=process.args["_"].splice(2))!==null&&_process$args$_$splic!==void 0?_process$args$_$splic:hideBin(process.argv);
const cli=yargs(hideBin(process.argv));

process.runtime.appendToController("appendCli",entry=>{
custom.push(_objectSpread({},entry));
});

let optionsMap=options!==null&&options!==void 0?options:[];
let commandMap=commands!==null&&commands!==void 0?commands:[];

let optionsTrigger={};

if(Array.isArray(custom)){
custom.forEach(command=>{
commandMap.push(command);
});
}

optionsMap.forEach(opt=>{var _opt$type;
const option=opt.option;
const alias=opt.alias;
const type=(_opt$type=opt.type)!==null&&_opt$type!==void 0?_opt$type:"boolean";
const description=opt.description;
const exec=opt.exec;

optionsTrigger[option]=exec;

if(Array.isArray(alias)){
alias.forEach(a=>{
optionsTrigger[a]=exec;
});
}else{
optionsTrigger[alias]=exec;
}

cli.option(option,{alias,type,description});
});

commandMap.forEach(cmd=>{var _cmd$description;
const command=cmd.command;
const description=(_cmd$description=cmd.description)!==null&&_cmd$description!==void 0?_cmd$description:"";
const args=cmd.args?yargs=>cmd.args(yargs):{};
const exec=cmd.exec?argv=>{
try{
const argumentsMap=objectToArrayMap(argv);
if(argumentsMap){
argumentsMap.forEach(argument=>{
if(argument.value&&typeof optionsTrigger[argument.key]=="function"){
optionsTrigger[argument.key](argument.value);
}
});
}
cmd.exec(argv);
}catch(error){
console.log(`🆘 Error executing command! >`);
console.error(`\n\t`,error);
}
}:()=>console.log(`This command not contains an executable script`);

cmdKeys.push(command);
cli.command(command,description,args,exec);
});

if(argvf.length>0){
if(Array.isArray(cmdKeys)){
let exists=false;

cmdKeys.forEach(key=>{
if(key.includes(argvf[0])){
return exists=true;
}
});

if(!exists){
cli.showHelp();
verbosity.error("Unknown command, use a valid command! \n");
}
}
}else{
try{
global.corenode.events.emit("cli_noCommand");
}catch(error){

}
}

cli.
showHelpOnFail(true).
help().
argv;
};exports.default=_default;