"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.evalCodeCodules = void 0;
var tslib_1 = require("tslib");
var cmd_loader_1 = require("../cmd-loader");
var Babel = (0, tslib_1.__importStar)(require("@babel/standalone"));
var index_1 = require("../index");
var cancelPrevModLoader;
function evalCodeCodules(modulesByScope, opts) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var runtimeWindow, entries, preload, _loop_1, scope, _a, promise, cancel, loaded, result, _loop_2, scope;
        return (0, tslib_1.__generator)(this, function (_b) {
            switch (_b.label) {
                case 0:
                    runtimeWindow = opts.runtimeWindow || index_1.runtime.previewWindow;
                    if (!runtimeWindow) {
                        return [2 /*return*/];
                    }
                    opts.onCodeError = opts.onCodeError || defaultCodeErrorHandler;
                    entries = [];
                    preload = {};
                    _loop_1 = function (scope) {
                        // eslint-disable-line
                        var modules = modulesByScope[scope];
                        modules.forEach(function (m) {
                            var file = getModuleFilePath(scope, m.path);
                            try {
                                preload[file] = transformCode(m.code);
                                entries.push(file);
                            }
                            catch (e) {
                                opts.onCodeError(e, file);
                                runtimeWindow.console.error(file + ": " + e);
                            }
                        });
                    };
                    for (scope in modulesByScope) {
                        _loop_1(scope);
                    }
                    if (!opts.disableAutoCancelPrev && cancelPrevModLoader) {
                        cancelPrevModLoader();
                    }
                    _a = (0, cmd_loader_1.loadCmdModule)(entries, {
                        // const { promise, cancel } = loadEsModule(entries, {
                        preload: preload,
                        dependencies: opts.dependencies,
                        onDependencyMiss: opts.onDependencyMiss,
                        runtime: runtimeWindow,
                        onModuleError: function (e) {
                            runtimeWindow.console.error(e.stack);
                        },
                        onError: function (e) {
                            runtimeWindow.console.error(e.stack);
                        },
                    }), promise = _a.promise, cancel = _a.cancel;
                    !opts.disableAutoCancelPrev && (cancelPrevModLoader = cancel);
                    return [4 /*yield*/, promise];
                case 1:
                    loaded = _b.sent();
                    result = {};
                    _loop_2 = function (scope) {
                        // eslint-disable-line
                        var modules = modulesByScope[scope];
                        result[scope] = modules.map(function (m) {
                            var file = getModuleFilePath(scope, m.path);
                            return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, m), { exports: loaded[file] });
                        });
                    };
                    for (scope in modulesByScope) {
                        _loop_2(scope);
                    }
                    return [2 /*return*/, result];
            }
        });
    });
}
exports.evalCodeCodules = evalCodeCodules;
function transformCode(srcCode) {
    // return srcCode;
    // eslint-disable-next-line no-undef
    return Babel.transform(srcCode, {
        // plugins: [[transformModulesCommonjs]],
        presets: ['env'],
    }).code;
}
function getModuleFilePath(pageId, path) {
    var file = "/" + path;
    if (pageId === 'global') {
        return file.replace('/global', '');
    }
    return file;
}
function defaultCodeErrorHandler(e, file) {
    console.error('Lowcode syntax error', file, e);
}
//# sourceMappingURL=base.js.map