"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateStyleModuleMap = void 0;
const tslib_1 = require("tslib");
const cals_1 = require("@cloudbase/cals");
const index_1 = require("../../index");
const remConfig = {
    rootValue: 28,
    propList: ['*'],
    unitPrecision: 5,
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
};
function generateStyleModule(modules = [], cssContextWrapper, optsions) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const codeModules = modules.filter((item) => item.type === cals_1.EResourceType.CODE);
        const themeModuels = codeModules.filter((item) => item.codeType === cals_1.ECodeType.THEME);
        const styleModuels = codeModules.filter((item) => item.codeType === cals_1.ECodeType.STYLE);
        const { processCssUnit = 'rem' } = optsions || {};
        let processedModules = yield Promise.all(styleModuels.map((module) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            try {
                let cssCode = module.code;
                if (cssContextWrapper) {
                    cssCode = `${cssContextWrapper} { ${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) || ''}\n${cssCode || ''}`;
                }
                const { default: less } = yield Promise.resolve().then(() => tslib_1.__importStar(require(/* webpackChunkName: "extra-package" */ 'less')));
                const { default: postcss } = yield Promise.resolve().then(() => tslib_1.__importStar(require(/* webpackChunkName: "extra-package" */ 'postcss')));
                const { default: pxToRem } = yield Promise.resolve().then(() => tslib_1.__importStar(require(/* webpackChunkName: "extra-package" */ 'postcss-pxtorem')));
                let { css: processedCss } = yield less.render(cssCode, { compress: true });
                if (processCssUnit === 'rem') {
                    processedCss = yield postcss([pxToRem(remConfig)]).process(processedCss).css;
                }
                const { css } = yield postcss([(0, cals_1.PostcssRpx2Calc)({ root: `var(${index_1.CALC_RPX_ROOT_VAL_NAME})` })]).process(processedCss);
                return css ? Object.assign(Object.assign({}, module), { code: css }) : null;
            }
            catch (e) {
                console.error(module.name, 'applyLessCodeStyle error', e);
            }
        })));
        return processedModules.filter((item) => !!item);
    });
}
function generateStyleModuleMap(moduleMap) {
    var _a, _b, _c, _d;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        let resultMap = {};
        let promises = [];
        const { global } = moduleMap;
        const themeModuels = ((_b = (_a = global === null || global === void 0 ? void 0 : global.resources) === null || _a === void 0 ? void 0 : _a.filter) === null || _b === void 0 ? void 0 : _b.call(_a, (item) => item.codeType === cals_1.ECodeType.THEME)) || [];
        for (const key in moduleMap) {
            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, { processCssUnit: moduleMap[key].processCssUnit }).then((res) => {
                if (res) {
                    resultMap[key] = res;
                }
            }));
        }
        yield Promise.all(promises);
        return resultMap;
    });
}
exports.generateStyleModuleMap = generateStyleModuleMap;
//# sourceMappingURL=lessParser.js.map