"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printErrors = void 0;
const chalk_1 = __importDefault(require("@modern-js/utils/chalk"));
const logger_1 = require("../../../logger");
function printErrors(errors) {
    if (errors.length === 0) {
        logger_1.logger.success('The syntax check success.');
        return;
    }
    const errs = errors.map(error => ({
        source: `${error.source.path}:${error.source.line}:${error.source.column}`,
        output: error.output
            ? `${error.output.path}:${error.output.line}:${error.output.column}`
            : undefined,
        reason: error.message,
        code: error.source.code,
    }));
    const longest = Math.max(...Object.keys(errs[0]).map(err => err.length));
    logger_1.logger.error('[Syntax Checker] Find some syntax errors after production build:\n');
    errs.forEach((err, index) => {
        console.info(`${chalk_1.default.red(`  ERROR#${index + 1}`)}:`);
        printMain(err, longest);
    });
    throw new Error(`[Syntax Checker] The current build fails due to an incompatible syntax, which can be fixed in the following ways:

  - If you want to downgrade the syntax, you can compile the corresponding module through the \`source.include\` config.
  - If you don't want to downgrade the syntax, you can adjust the project's browserslist to match the syntax.`);
}
exports.printErrors = printErrors;
function printMain(error, logest) {
    const fillWhiteSpace = (s, logest) => {
        if (s.length < logest) {
            const rightPadding = ' '.repeat(logest - s.length);
            return s + rightPadding;
        }
        return s;
    };
    Object.entries(error).forEach(([key, content]) => {
        if (!content) {
            return;
        }
        const title = chalk_1.default.red(fillWhiteSpace(key, logest));
        console.info(`  ${title} - ${content}`);
    });
    console.info('');
}
