"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const commonTags = require("common-tags");
const os = require("os");
const StackTracey = require("stacktracey");
const stripIndent = commonTags.stripIndent;
const stripIndents = commonTags.stripIndents;
const EOL = os.EOL;
const highlight = chalk_1.default.yellow;
const colorPath = chalk_1.default.gray;
function reportError(logObj) {
    if (logObj['err']) {
        const err = logObj['err'];
        switch (err.code) {
            case 'UNEXPECTED_STORE':
                return reportUnexpectedStore(err, logObj['message']);
            case 'STORE_BREAKING_CHANGE':
                return reportStoreBreakingChange(err, logObj['message']);
            case 'MODULES_BREAKING_CHANGE':
                return reportModulesBreakingChange(err, logObj['message']);
            case 'MODIFIED_DEPENDENCY':
                return reportModifiedDependency(err, logObj['message']);
            case 'SHRINKWRAP_BREAKING_CHANGE':
                return reportShrinkwrapBreakingChange(err, logObj['message']);
            default:
                return formatGenericError(err.message || logObj['message'], err.stack);
        }
    }
    return formatErrorSummary(logObj['message']);
}
exports.default = reportError;
function reportUnexpectedStore(err, msg) {
    return stripIndent `
    ${formatErrorSummary(err.message)}

    expected: ${highlight(msg['expectedStorePath'])}
    actual: ${highlight(msg['actualStorePath'])}

    If you want to use the new store, run the same command with the ${highlight('--force')} parameter.
  `;
}
function reportStoreBreakingChange(err, msg) {
    let output = stripIndent `
    ${formatErrorSummary(`The store used for the current node_modules is incomatible with the current version of pnpm`)}
    Store path: ${colorPath(msg['storePath'])}

    Try running the same command with the ${highlight('--force')} parameter.
  `;
    if (msg['additionalInformation']) {
        output += EOL + EOL + msg['additionalInformation'];
    }
    output += formatRelatedSources(msg);
    return output;
}
function reportModulesBreakingChange(err, msg) {
    let output = stripIndent `
    ${formatErrorSummary(`The current version of pnpm is not compatible with the available node_modules structure`)}
    node_modules path: ${colorPath(msg['modulesPath'])}

    Run ${highlight('pnpm install --force')} to recreate node_modules.
  `;
    if (msg['additionalInformation']) {
        output += EOL + EOL + msg['additionalInformation'];
    }
    output += formatRelatedSources(msg);
    return output;
}
function formatRelatedSources(msg) {
    let output = '';
    if (!msg['relatedIssue'] && !msg['relatedPR'])
        return output;
    output += EOL;
    if (msg['relatedIssue']) {
        output += EOL + `Related issue: ${colorPath(`https://github.com/pnpm/pnpm/issues/${msg['relatedIssue']}`)}`;
    }
    if (msg['relatedPR']) {
        output += EOL + `Related PR: ${colorPath(`https://github.com/pnpm/pnpm/pull/${msg['relatedPR']}`)}`;
    }
    return output;
}
function formatGenericError(errorMessage, stack) {
    if (stack) {
        return stripIndents `
      ${formatErrorSummary(errorMessage)}
      ${new StackTracey(stack).pretty}
    `;
    }
    return formatErrorSummary(errorMessage);
}
function formatErrorSummary(message) {
    return `${chalk_1.default.bgRed.black('\u2009ERROR\u2009')} ${chalk_1.default.red(message)}`;
}
function reportModifiedDependency(err, msg) {
    return stripIndent `
    ${formatErrorSummary('Packages in the store have been mutated')}

    These packages are modified:
    ${msg['modified'].map((pkgPath) => colorPath(pkgPath)).join(EOL)}

    You can run ${highlight('pnpm install')} to refetch the modified packages
  `;
}
function reportShrinkwrapBreakingChange(err, msg) {
    return stripIndent `
    ${formatErrorSummary(err.message)}

    Run with the ${highlight('--force')} parameter to recreate the shrinkwrap file.
  `;
}
//# sourceMappingURL=reportError.js.map