"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");Object.defineProperty(exports,"__esModule",{value:true});exports.installModule=installModule;exports.default=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _listr=_interopRequireDefault(require("listr"));
var _fs=_interopRequireDefault(require("fs"));
var _path=_interopRequireDefault(require("path"));
var _nodeFetch=_interopRequireDefault(require("node-fetch"));

var _corenode=require("corenode");
var _utils=require("@corenode/utils");

var _temporalDir=_interopRequireDefault(require("corenode/dist/libs/temporalDir"));
var timing=_interopRequireWildcard(require("corenode/dist/libs/performance"));var _runtimeEnv$remoteMod;const fallbackRemoteModulesSource="https://corenode.ragestudio.net/std/modules";

const runtimeEnv=(0,_corenode.getProjectEnv)();
const remoteModulesSource=(_runtimeEnv$remoteMod=runtimeEnv.remoteModulesSource)!==null&&_runtimeEnv$remoteMod!==void 0?_runtimeEnv$remoteMod:fallbackRemoteModulesSource;

function outputResume(payload){const _ref=
payload||null,installPath=_ref.installPath,pkg=_ref.pkg;
console.group();
console.log(`\n📦  Installed package (${pkg}) ${installPath?`on > ${installPath}`:""}`);
timing.performances[pkg]?console.log(`⏱  Operation tooks ${timing.stop(pkg)}ms \n`):null;
console.groupEnd();
}

function installModule(params){const
pkg=params.pkg,_params$version=params.version,version=_params$version===void 0?"lastest":_params$version;

if(!pkg){
return console.error(`🚫 Nothing to install!`);
}

timing.start(pkg);

const downloadPath=_temporalDir.default.createNew(pkg);

const moduleFilename=`${pkg}.module`;
const moduleFile=_path.default.resolve(downloadPath,moduleFilename);
const moduleURI=`${remoteModulesSource}/${pkg}/${version}/index.module`;

let moduleCodec="utf-8";

let tasks=[
{
title:"\uD83D\uDCE1 Checking remote",
task:()=>{
_utils.verbosity.dump("checking remote");
return new Promise((resolve,reject)=>{
(0,_nodeFetch.default)(moduleURI).then(done=>{
return resolve(true);
}).catch(err=>{
_utils.verbosity.options({dumpFile:"only"}).error(err);
return reject(err.message);
});
});
}},

{
title:"\uD83D\uDE9A Fetching module",
task:()=>{

_utils.verbosity.dump("Fetching module");
return new Promise((resolve,reject)=>{
(0,_utils.addressFileStream)(moduleURI,moduleFilename,downloadPath).then(done=>{
return resolve();
}).catch(err=>{
_utils.verbosity.options({dumpFile:"only"}).error(err);
return reject(`Failed downloading module`);
});
});
}},

{
title:"\uD83D\uDD29 Processing module",
task:()=>{
_utils.verbosity.dump("Processing module");
return new Promise((resolve,reject)=>{
const _module=require(moduleFile);
(0,_corenode.writeModule)(_module.pkg,null,_fs.default.readFileSync(moduleFile,moduleCodec)).then(function(){var _ref2=(0,_asyncToGenerator2.default)(function*(modulePath){
if(_module.runtimeTemplate){
const templateFile=_path.default.resolve(modulePath,`.template.corenode`);
try{
if(_fs.default.existsSync(templateFile)){


}
}catch(error){
_utils.verbosity.options({dumpFile:true}).error(`Error processing runtime template >`,error);
}
}

if(_module.node_modules){
if(Boolean(params===null||params===void 0?void 0:params.ignoreDeps)){
_utils.verbosity.options({dumpFile:true}).warn(`Ignoring dependencies installation`);
}else{
(0,_utils.objectToArrayMap)(_module.node_modules).forEach(dep=>{
const isInstalled=(0,_corenode.isDependencyInstalled)(dep.key)?true:false;
if(!isInstalled){
(0,_corenode.addDependency)(dep,true);
}
});
}
}

(0,_corenode.loadRegistry)({write:true});
resolve();
});return function(_x){return _ref2.apply(this,arguments)}}()).catch(err=>{
reject(err);
});
});
}}];



new _listr.default(tasks,{collapse:false,concurrent:false}).run().then(res=>{
params!==null&&params!==void 0&&params.keepTmp?null:_temporalDir.default.clean();
outputResume({downloadPath,pkg});
}).
catch(err=>{
_utils.verbosity.options({dumpFile:true}).error(`Failed installation Task >`,err);
});
}var _default=

installModule;exports.default=_default;