"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDepWarningsDashboard = exports.getDepWarningsPR = exports.getDepWarningsOnboardingPR = exports.getErrors = exports.getWarnings = void 0;
const logger_1 = require("../../logger");
const emoji_1 = require("../../util/emoji");
function getWarnings(config) {
    if (!config.warnings?.length) {
        return '';
    }
    let warningText = `\n# Warnings (${config.warnings.length})\n\n`;
    warningText += `Please correct - or verify that you can safely ignore - these warnings before you merge this PR.\n\n`;
    for (const w of config.warnings) {
        warningText += `-   \`${w.topic}\`: ${w.message}\n`;
    }
    warningText += '\n---\n';
    return warningText;
}
exports.getWarnings = getWarnings;
function getErrors(config) {
    if (!config.errors?.length) {
        return '';
    }
    let errorText = `\n# Errors (${config.errors.length})\n\n`;
    errorText += `Renovate has found errors that you should fix (in this branch) before finishing this PR.\n\n`;
    for (const e of config.errors) {
        errorText += `-   \`${e.topic}\`: ${e.message}\n`;
    }
    errorText += '\n---\n';
    return errorText;
}
exports.getErrors = getErrors;
function getDepWarnings(packageFiles) {
    const warnings = [];
    const warningFiles = [];
    for (const files of Object.values(packageFiles ?? {})) {
        for (const file of files ?? []) {
            // TODO: remove condition when type is fixed (#7154)
            if (file.packageFile) {
                for (const dep of file.deps ?? []) {
                    for (const w of dep.warnings ?? []) {
                        const message = w.message;
                        if (!warnings.includes(message)) {
                            warnings.push(message);
                        }
                        if (!warningFiles.includes(file.packageFile)) {
                            warningFiles.push(file.packageFile);
                        }
                    }
                }
            }
        }
    }
    return { warnings, warningFiles };
}
function getDepWarningsOnboardingPR(packageFiles) {
    const { warnings, warningFiles } = getDepWarnings(packageFiles);
    let warningText = '';
    if (!warnings.length) {
        return '';
    }
    logger_1.logger.debug({ warnings, warningFiles }, 'Found package lookup warnings');
    warningText = (0, emoji_1.emojify)(`\n---\n\n### :warning: Dependency Lookup Warnings :warning:\n\n`);
    warningText += `Please correct - or verify that you can safely ignore - these lookup failures before you merge this PR.\n\n`;
    for (const w of warnings) {
        warningText += `-   \`${w}\`\n`;
    }
    warningText +=
        '\nFiles affected: ' +
            warningFiles.map((f) => '`' + f + '`').join(', ') +
            '\n\n';
    return warningText;
}
exports.getDepWarningsOnboardingPR = getDepWarningsOnboardingPR;
function getDepWarningsPR(packageFiles, dependencyDashboard) {
    const { warnings, warningFiles } = getDepWarnings(packageFiles);
    let warningText = '';
    if (!warnings.length) {
        return '';
    }
    logger_1.logger.debug({ warnings, warningFiles }, 'Found package lookup warnings');
    warningText = (0, emoji_1.emojify)(`\n---\n\n### :warning: Dependency Lookup Warnings :warning:\n\n`);
    warningText += 'Warnings were logged while processing this repo. ';
    if (dependencyDashboard) {
        warningText += `Please check the Dependency Dashboard for more information.\n\n`;
    }
    else {
        warningText += `Please check the logs for more information.\n\n`;
    }
    return warningText;
}
exports.getDepWarningsPR = getDepWarningsPR;
function getDepWarningsDashboard(packageFiles) {
    const { warnings, warningFiles } = getDepWarnings(packageFiles);
    if (!warnings.length) {
        return '';
    }
    const depWarnings = warnings
        .map((w) => w.replace('Failed to look up dependency ', ''))
        .map((dep) => '`' + dep + '`')
        .join(', ');
    logger_1.logger.debug({ warnings, warningFiles }, 'Found package lookup warnings');
    let warningText = (0, emoji_1.emojify)(`\n---\n\n### :warning: Dependency Lookup Warnings :warning:\n\n`);
    warningText += `-   Renovate failed to look up the following dependencies: `;
    warningText += depWarnings;
    warningText += '.\n\nFiles affected: ';
    warningText += warningFiles.map((f) => '`' + f + '`').join(', ');
    warningText += '\n\n---\n\n';
    return warningText;
}
exports.getDepWarningsDashboard = getDepWarningsDashboard;
//# sourceMappingURL=errors-warnings.js.map