"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lessEscapePlugin = void 0;
const utils_1 = require("../utils");
/**
 * Checks improper LESS escaping
 * @param cssOutput CSS string output
 * @private
 */
function checkImproperLessEscaping(cssOutput) {
    const cssOutputArray = cssOutput.split('\n');
    const foundImproperLessEscaping = cssOutputArray.reduce((acc, val) => {
        if (val.includes('calc(') &&
            !val.includes('~') &&
            !val.includes('--')) {
            const spacer = '  ';
            acc.push(spacer.concat(val.trim()));
        }
        return acc;
    }, []);
    if (foundImproperLessEscaping && foundImproperLessEscaping.length) {
        return foundImproperLessEscaping.join('\n');
    }
}
let improperLessEscapedFiles = new Set();
/**
 * Processes LESS data and warns if there is an improper LESS escaping
 * @param pluginName Plugin name
 */
function lessEscapePlugin(pluginName) {
    return {
        process: (src, extra) => {
            const improperLessEscaping = checkImproperLessEscaping(src);
            if (improperLessEscaping &&
                !improperLessEscapedFiles.has(extra.fileInfo.filename)) {
                const spacer = ' ';
                console.log(utils_1.cwarn `⇢ [${pluginName}] LESS not properly escaped in ${extra.fileInfo.filename}:`, utils_1.cwarn `\n${improperLessEscaping}\n\n`, utils_1.cwarn `${spacer}Please escape LESS the following way:\n`, utils_1.cwarn `${spacer}width: ~"calc(100% - 200px)"; or width: ~"calc(100vw - " @yourVariable ~")";\n`);
                improperLessEscapedFiles.add(extra.fileInfo.filename);
            }
            else {
                // This is needed to avoid having warnings for the already checked files (watch)
                if (improperLessEscapedFiles.has(extra.fileInfo.filename)) {
                    improperLessEscapedFiles.delete(extra.fileInfo.filename);
                }
            }
            return src;
        },
    };
}
exports.lessEscapePlugin = lessEscapePlugin;
//# sourceMappingURL=LessEscapePlugin.js.map