"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.PnpmSyncUtilities = void 0;
const pnpm_sync_lib_1 = require("pnpm-sync-lib");
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const RushConstants_1 = require("../logic/RushConstants");
class PnpmSyncUtilities {
    static _addLinePrefix(message) {
        return message
            .split('\n')
            .map((x) => (x.trim() ? terminal_1.Colorize.cyan(`pnpm-sync: `) + x : x))
            .join('\n');
    }
    static processLogMessage(options, terminal) {
        const message = options.message;
        const details = options.details;
        // Special formatting for interested messages
        switch (details.messageIdentifier) {
            case pnpm_sync_lib_1.LogMessageIdentifier.PREPARE_FINISHING:
                terminal.writeVerboseLine(PnpmSyncUtilities._addLinePrefix(`Regenerated ${RushConstants_1.RushConstants.pnpmSyncFilename} in ${Math.round(details.executionTimeInMs)} ms`));
                return;
            case pnpm_sync_lib_1.LogMessageIdentifier.COPY_FINISHING:
                {
                    const customMessage = `Synced ${details.fileCount} ` +
                        (details.fileCount === 1 ? 'file' : 'files') +
                        ` in ${Math.round(details.executionTimeInMs)} ms`;
                    terminal.writeVerboseLine(PnpmSyncUtilities._addLinePrefix(customMessage));
                }
                return;
            case pnpm_sync_lib_1.LogMessageIdentifier.PREPARE_REPLACING_FILE:
                {
                    const customMessage = `Expecting ${RushConstants_1.RushConstants.pnpmSyncFilename} version ${details.expectedVersion}, ` +
                        `but found version ${details.actualVersion}`;
                    terminal.writeVerboseLine(PnpmSyncUtilities._addLinePrefix(message));
                    terminal.writeVerboseLine(PnpmSyncUtilities._addLinePrefix(customMessage));
                }
                return;
            case pnpm_sync_lib_1.LogMessageIdentifier.COPY_ERROR_INCOMPATIBLE_SYNC_FILE: {
                terminal.writeErrorLine(PnpmSyncUtilities._addLinePrefix(`The workspace was installed using an incompatible version of pnpm-sync.\n` +
                    `Please run "rush install" or "rush update" again.`));
                terminal.writeLine(PnpmSyncUtilities._addLinePrefix(`Expecting ${RushConstants_1.RushConstants.pnpmSyncFilename} version ${details.expectedVersion}, ` +
                    `but found version ${details.actualVersion}\n` +
                    `Affected folder: ${details.pnpmSyncJsonPath}`));
                throw new node_core_library_1.AlreadyReportedError();
            }
        }
        // Default handling for other messages
        switch (options.messageKind) {
            case pnpm_sync_lib_1.LogMessageKind.ERROR:
                terminal.writeErrorLine(terminal_1.Colorize.red('ERROR: pnpm-sync: ' + message));
                throw new node_core_library_1.AlreadyReportedError();
            case pnpm_sync_lib_1.LogMessageKind.WARNING:
                terminal.writeWarningLine(terminal_1.Colorize.yellow('pnpm-sync: ' + message));
                return;
            case pnpm_sync_lib_1.LogMessageKind.INFO:
            case pnpm_sync_lib_1.LogMessageKind.VERBOSE:
                terminal.writeDebugLine(PnpmSyncUtilities._addLinePrefix(message));
                return;
        }
    }
}
exports.PnpmSyncUtilities = PnpmSyncUtilities;
//# sourceMappingURL=PnpmSyncUtilities.js.map