"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zen_push_1 = require("@zkochan/zen-push");
const chalk = require("chalk");
const most = require("most");
const path = require("path");
const prettyTime = require("pretty-time");
const stripAnsi = require("strip-ansi");
const constants_1 = require("../constants");
const outputConstants_1 = require("./outputConstants");
const formatPrefix_1 = require("./utils/formatPrefix");
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
// When streaming processes are spawned, use this color for prefix
const colorWheel = ['cyan', 'magenta', 'blue', 'yellow', 'green', 'red'];
const NUM_COLORS = colorWheel.length;
// Ever-increasing index ensures colors are always sequential
let currentColor = 0;
exports.default = (log$, opts) => {
    // When the reporter is not append-only, the length of output is limited
    // in order to reduce flickering
    if (opts.appendOnly) {
        const streamLifecycleOutput = createStreamLifecycleOutput(opts.cwd);
        return log$.lifecycle
            .map((log) => most.of({
            msg: streamLifecycleOutput(log),
        }));
    }
    const lifecycleMessages = {};
    const lifecycleStreamByDepPath = {};
    const lifecyclePushStream = new zen_push_1.default();
    // TODO: handle promise of .forEach?!
    log$.lifecycle // tslint:disable-line
        .forEach((log) => {
        const key = `${log.stage}:${log.depPath}`;
        lifecycleMessages[key] = lifecycleMessages[key] || {
            collapsed: log.wd.includes(NODE_MODULES),
            output: [],
            startTime: process.hrtime(),
            status: formatIndentedStatus(chalk.magentaBright('Running...')),
        };
        const exit = typeof log['exitCode'] === 'number';
        let msg;
        if (lifecycleMessages[key].collapsed) {
            msg = renderCollapsedScriptOutput(log, lifecycleMessages[key], { cwd: opts.cwd, exit, maxWidth: opts.width });
        }
        else {
            msg = renderScriptOutput(log, lifecycleMessages[key], { cwd: opts.cwd, exit, maxWidth: opts.width });
        }
        if (exit) {
            delete lifecycleMessages[key];
        }
        if (!lifecycleStreamByDepPath[key]) {
            lifecycleStreamByDepPath[key] = new zen_push_1.default();
            lifecyclePushStream.next(most.from(lifecycleStreamByDepPath[key].observable));
        }
        lifecycleStreamByDepPath[key].next({ msg });
        if (exit) {
            lifecycleStreamByDepPath[key].complete();
        }
    });
    return most.from(lifecyclePushStream.observable);
};
function renderCollapsedScriptOutput(log, messageCache, opts) {
    messageCache['label'] = messageCache['label'] ||
        `${highlightLastFolder(formatPrefix_1.formatPrefixNoTrim(opts.cwd, log.wd))}: Running ${log.stage} script`;
    if (!opts.exit) {
        updateMessageCache(log, messageCache, opts);
        return `${messageCache['label']}...`;
    }
    const time = prettyTime(process.hrtime(messageCache.startTime));
    if (log['exitCode'] === 0) {
        return `${messageCache['label']}, done in ${time}`;
    }
    if (log['optional'] === true) {
        return `${messageCache['label']}, failed in ${time} (skipped as optional)`;
    }
    return `${messageCache['label']}, failed in ${time}${constants_1.EOL}${renderScriptOutput(log, messageCache, opts)}`;
}
function renderScriptOutput(log, messageCache, opts) {
    updateMessageCache(log, messageCache, opts);
    if (opts.exit && log['exitCode'] !== 0) {
        return [
            messageCache.script,
            ...messageCache.output,
            messageCache.status,
        ].join(constants_1.EOL);
    }
    if (messageCache.output.length > 10) {
        return [
            messageCache.script,
            `[${messageCache.output.length - 10} lines collapsed]`,
            ...messageCache.output.slice(messageCache.output.length - 10),
            messageCache.status,
        ].join(constants_1.EOL);
    }
    return [
        messageCache.script,
        ...messageCache.output,
        messageCache.status,
    ].join(constants_1.EOL);
}
function updateMessageCache(log, messageCache, opts) {
    if (log['script']) {
        const prefix = `${formatPrefix_1.default(opts.cwd, log.wd)} ${outputConstants_1.hlValue(log.stage)}`;
        const maxLineWidth = opts.maxWidth - prefix.length - 2 + ANSI_ESCAPES_LENGTH_OF_PREFIX;
        messageCache.script = `${prefix}$ ${cutLine(log['script'], maxLineWidth)}`;
    }
    else if (opts.exit) {
        const time = prettyTime(process.hrtime(messageCache.startTime));
        if (log['exitCode'] === 0) {
            messageCache.status = formatIndentedStatus(chalk.magentaBright(`Done in ${time}`));
        }
        else {
            messageCache.status = formatIndentedStatus(chalk.red(`Failed in ${time}`));
        }
    }
    else {
        messageCache.output.push(formatIndentedOutput(opts.maxWidth, log));
    }
}
function formatIndentedStatus(status) {
    return `${chalk.magentaBright('└─')} ${status}`;
}
function highlightLastFolder(p) {
    const lastSlash = p.lastIndexOf('/') + 1;
    return `${chalk.gray(p.substr(0, lastSlash))}${p.substr(lastSlash)}`;
}
const ANSI_ESCAPES_LENGTH_OF_PREFIX = outputConstants_1.hlValue(' ').length - 1;
function createStreamLifecycleOutput(cwd) {
    currentColor = 0;
    const colorByPrefix = new Map();
    return streamLifecycleOutput.bind(null, colorByPrefix, cwd);
}
function streamLifecycleOutput(colorByPkg, cwd, logObj) {
    const prefix = formatLifecycleScriptPrefix(colorByPkg, cwd, logObj.wd, logObj.stage);
    if (typeof logObj['exitCode'] === 'number') {
        if (logObj['exitCode'] === 0) {
            return `${prefix}: Done`;
        }
        else {
            return `${prefix}: Failed`;
        }
    }
    if (logObj['script']) {
        return `${prefix}$ ${logObj['script']}`;
    }
    const line = formatLine(Infinity, logObj);
    return `${prefix}: ${line}`;
}
function formatIndentedOutput(maxWidth, logObj) {
    return `${chalk.magentaBright('│')} ${formatLine(maxWidth - 2, logObj)}`;
}
function formatLifecycleScriptPrefix(colorByPkg, cwd, wd, stage) {
    if (!colorByPkg.has(wd)) {
        const colorName = colorWheel[currentColor % NUM_COLORS];
        colorByPkg.set(wd, chalk[colorName]);
        currentColor += 1;
    }
    const color = colorByPkg.get(wd);
    return `${color(formatPrefix_1.default(cwd, wd))} ${outputConstants_1.hlValue(stage)}`;
}
function formatLine(maxWidth, logObj) {
    const line = cutLine(logObj['line'], maxWidth);
    // TODO: strip only the non-color/style ansi escape codes
    if (logObj['stdio'] === 'stderr') {
        return chalk.gray(line);
    }
    return line;
}
function cutLine(line, maxLength) {
    return stripAnsi(line).substr(0, maxLength);
}
//# sourceMappingURL=reportLifecycleScripts.js.map