import { __assign, __awaiter, __generator } from "tslib";
import { EResourceType, ECodeType } from '@cloudbase/cals';
import less from 'less';
import postcss from 'postcss';
import pxToRem from 'postcss-pxtorem';
export var remConfig = {
    rootValue: 28,
    propList: ['*'],
    unitPrecision: 5,
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
};
export function generateStyleModule(modules, cssContextWrapper) {
    if (modules === void 0) { modules = []; }
    return __awaiter(this, void 0, void 0, function () {
        var codeModules, themeModuels, styleModuels, processedModules;
        var _this = this;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    codeModules = modules.filter(function (item) { return item.type === EResourceType.CODE; });
                    themeModuels = codeModules.filter(function (item) { return item.codeType === ECodeType.THEME; });
                    styleModuels = codeModules.filter(function (item) { return item.codeType === ECodeType.STYLE; });
                    return [4 /*yield*/, Promise.all(styleModuels.map(function (module) { return __awaiter(_this, void 0, void 0, function () {
                            var cssCode, css, remCss, e_1;
                            var _a, _b;
                            return __generator(this, function (_c) {
                                switch (_c.label) {
                                    case 0:
                                        _c.trys.push([0, 3, , 4]);
                                        cssCode = module.code;
                                        if (cssContextWrapper) {
                                            cssCode = "".concat(cssContextWrapper, " { ").concat(module.code, " }");
                                        }
                                        else if ((_a = themeModuels[0]) === null || _a === void 0 ? void 0 : _a.code) {
                                            cssCode = ((_b = themeModuels[0]) === null || _b === void 0 ? void 0 : _b.code) + cssCode;
                                        }
                                        return [4 /*yield*/, less.render(cssCode, { compress: true })];
                                    case 1:
                                        css = (_c.sent()).css;
                                        return [4 /*yield*/, postcss([pxToRem(remConfig)]).process(css)];
                                    case 2:
                                        remCss = (_c.sent()).css;
                                        return [2 /*return*/, remCss ? __assign(__assign({}, module), { code: remCss }) : null];
                                    case 3:
                                        e_1 = _c.sent();
                                        console.error(module.name, 'applyLessCodeStyle error', e_1);
                                        return [3 /*break*/, 4];
                                    case 4: return [2 /*return*/];
                                }
                            });
                        }); }))];
                case 1:
                    processedModules = _a.sent();
                    return [2 /*return*/, processedModules.filter(function (item) { return !!item; })];
            }
        });
    });
}
export function generateStyleModuleMap(moduleMap) {
    var _a, _b, _c, _d;
    return __awaiter(this, void 0, void 0, function () {
        var resultMap, promises, global, themeModuels, _loop_1, key;
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    resultMap = {};
                    promises = [];
                    global = moduleMap.global;
                    themeModuels = ((_b = (_a = global.resources) === null || _a === void 0 ? void 0 : _a.filter) === null || _b === void 0 ? void 0 : _b.call(_a, function (item) { return item.codeType === ECodeType.THEME; })) ||
                        [];
                    _loop_1 = function (key) {
                        promises.push(generateStyleModule(((_d = (_c = moduleMap[key].resources) === null || _c === void 0 ? void 0 : _c.concat) === null || _d === void 0 ? void 0 : _d.call(_c, key === 'global' ? [] : themeModuels)) || [], moduleMap[key].cssContextWrapper).then(function (res) {
                            if (res) {
                                resultMap[key] = res;
                            }
                        }));
                    };
                    for (key in moduleMap) {
                        _loop_1(key);
                    }
                    return [4 /*yield*/, Promise.all(promises)];
                case 1:
                    _e.sent();
                    return [2 /*return*/, resultMap];
            }
        });
    });
}
//# sourceMappingURL=lessParser.js.map