"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadEsModule = exports.loadCmdModule = exports.taskCtrl = void 0;
const tslib_1 = require("tslib");
const loader_1 = require("./loader");
const module_1 = require("./module");
const task_ctrl_1 = tslib_1.__importDefault(require("./task-ctrl"));
const index_1 = require("../index");
const defaultOpts = {
    npmFilePrefix: 'https://cdn.jsdelivr.net/npm/',
    npmFilePrefixByScope: {},
    projFilePrefix: '/',
    defineFuncName: 'define',
    extensions: ['.js', '.json'],
    onDependencyMiss: (pkgName, version) => {
        console.error(`The npm package ${pkgName}@${version} is not in the dependency list`);
    },
    debug: false,
    runtime: index_1.runtime,
    onModuleError: (e) => {
        console.error('lowcode module error ', e);
    },
    onError: (e) => {
        try {
            console.error('Error loading cmd modules', e);
        }
        catch (e) { }
    },
    externals: {},
};
exports.taskCtrl = new task_ctrl_1.default();
function loadCmdModuleInteral(entries, opts = {}, taskId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        opts = Object.assign(Object.assign({}, defaultOpts), opts);
        const loaded = yield (0, loader_1.loadEntry)(entries, opts, taskId);
        const result = {};
        if (!exports.taskCtrl.isActive(taskId)) {
            return result;
        }
        const defined = (0, module_1.defineModules)(loaded, opts);
        entries.map((ent) => {
            try {
                result[ent] = defined[ent].execute();
            }
            catch (e) {
                opts.onModuleError(e);
            }
        });
        return result;
    });
}
/**
 * Add Wrapper to be cancelable
 */
function loadCmdModule(entries, opts = {}) {
    const taskId = exports.taskCtrl.add();
    const promise = loadCmdModuleInteral(entries, opts, taskId);
    return {
        promise,
        cancel() {
            exports.taskCtrl.cancel(taskId);
        },
    };
}
exports.loadCmdModule = loadCmdModule;
function loadEsModule(entries, opts = {}) {
    const taskId = exports.taskCtrl.add();
    const promise = loadCmdModuleInteral(entries, opts, taskId);
    return {
        promise,
        cancel() {
            exports.taskCtrl.cancel(taskId);
        },
    };
}
exports.loadEsModule = loadEsModule;
//# sourceMappingURL=index.js.map