var _a;
import { __assign, __awaiter, __generator } from "tslib";
export { generateStyleModule, generateStyleModuleMap, } from './parser/lessParser';
import { ECodeType } from '@cloudbase/cals';
import { evalCodeCodules, } from './parser/base';
import { StateParser, ComputedParser, LifeCycleParser, HandlerParser, NormalModuleParser, } from './parser/index';
var moduleParsers = (_a = {},
    _a[ECodeType.LIFECYCLE] = new LifeCycleParser(),
    _a[ECodeType.COMPUTEDN] = new ComputedParser(),
    _a[ECodeType.STATE] = new StateParser(),
    _a[ECodeType.NORMAL_MODULE] = new NormalModuleParser(),
    _a[ECodeType.HANDLER_FN] = new HandlerParser(),
    _a);
// 用于页面内通信
var lastError = [];
function addError(e, file) {
    var err = { file: file, message: e.message || e.stack };
    lastError.push(err);
}
export var lowcodeContext = {};
// export const widgetsContext = {} as WidgetsContext;
// Just for debug
// Object.defineProperty(window, '__widgetsContext', {
//   get: () => widgetsContext,
// });
Object.defineProperty(window, '__lowcodeContext', {
    get: function () { return lowcodeContext; },
    set: function (value) {
        console.warn('__lowcodeContext can not been set to another value');
    },
});
function quickClone(json) {
    return JSON.parse(JSON.stringify(json));
}
/**
 * Reexecute all low code when updated
 */
export function generateLowcodeModules(_a) {
    var runtimeWindow = _a.runtimeWindow, _b = _a.codeModulesMap, codeModulesMap = _b === void 0 ? {} : _b, _c = _a.onDependencyMiss, onDependencyMiss = _c === void 0 ? function () { } : _c, _d = _a.dependencies, dependencies = _d === void 0 ? {} : _d;
    return __awaiter(this, void 0, void 0, function () {
        var allModules, _loop_1, pageId, result;
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    allModules = {
                        global: quickClone(filterCssCodeModules(codeModulesMap.global)),
                    };
                    _loop_1 = function (pageId) {
                        var modules = pageId === 'global'
                            ? codeModulesMap[pageId]
                            : codeModulesMap[pageId].map(function (module) {
                                var _a, _b;
                                return __assign(__assign({}, module), { path: (_b = (_a = module.path) === null || _a === void 0 ? void 0 : _a.replace) === null || _b === void 0 ? void 0 : _b.call(_a, /^.*?\//, pageId + "/") });
                            });
                        allModules[pageId] = quickClone(filterCssCodeModules(modules));
                    };
                    for (pageId in codeModulesMap) {
                        _loop_1(pageId);
                    }
                    return [4 /*yield*/, applyLowcode(allModules, {
                            dependencies: dependencies,
                            onDependencyMiss: onDependencyMiss,
                            runtimeWindow: runtimeWindow,
                        })];
                case 1:
                    result = _e.sent();
                    Object.assign(lowcodeContext, result);
                    return [2 /*return*/, lowcodeContext];
            }
        });
    });
}
export function applyLowcode(allModules, opts) {
    return __awaiter(this, void 0, void 0, function () {
        var innerEvalContext, type, evaledModules, _loop_2, pageId, type;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    innerEvalContext = {};
                    for (type in moduleParsers) {
                        // eslint-disable-line
                        moduleParsers[type].onStart(innerEvalContext);
                    }
                    opts.onBeforeParse && opts.onBeforeParse(innerEvalContext);
                    lastError = [];
                    // @ts-ignore
                    opts.onCodeError = addError;
                    return [4 /*yield*/, evalCodeCodules(allModules, opts)];
                case 1:
                    evaledModules = _a.sent();
                    _loop_2 = function (pageId) {
                        // eslint-disable-line
                        evaledModules[pageId].forEach(function (m) {
                            var parser = moduleParsers[m.codeType];
                            if (!parser) {
                                if (m.codeType !== 'mp_config') {
                                    console.error("Can not handle code module of type " + m.codeType);
                                }
                                return;
                            }
                            try {
                                parser.parseModule(pageId, m);
                            }
                            catch (e) {
                                console.error('Error parse module', m, e);
                            }
                        });
                    };
                    for (pageId in evaledModules) {
                        _loop_2(pageId);
                    }
                    for (type in moduleParsers) {
                        // eslint-disable-line
                        try {
                            moduleParsers[type].onEnd(innerEvalContext);
                        }
                        catch (e) {
                            console.error('Module parser error', e);
                        }
                    }
                    return [2 /*return*/, innerEvalContext];
            }
        });
    });
}
// 去掉css 代码
function filterCssCodeModules(modules) {
    if (modules === void 0) { modules = []; }
    return modules.filter(function (module) {
        return module.codeType !== ECodeType.STYLE && module.codeType !== ECodeType.THEME;
    });
}
// export function cleanWidgetsContext() {
//   for (let key in widgetsContext) {
//     delete widgetsContext[key];
//   }
// }
export function cleanLowcodeContext() {
    // @ts-ignore For debugging
    for (var key in lowcodeContext) {
        delete lowcodeContext[key];
    }
}
//# sourceMappingURL=lowcode.js.map