"use strict";
const chalk = require('chalk');
const logger = require('@zkochan/logger');
const observatory = require('observatory');
observatory.settings({ prefix: '  ', width: 74 });
/**
 * Logger.
 *
 * @example
 *     add = logger()
 *
 *     log = add({ name: 'rimraf', rawSpec: 'rimraf@2.5.1' })
 *     log('resolved', pkgData)
 *     log('downloading')
 *     log('downloading', { done: 1, total: 200 })
 *     log('depnedencies')
 *     log('error', err)
 */
function default_1() {
    const tasks = {};
    function getTask(pkg) {
        if (tasks[pkg.rawSpec])
            return tasks[pkg.rawSpec];
        const task = observatory.add((pkg.name ? (pkg.name + ' ') : '') +
            chalk.gray(pkg.rawSpec || ''));
        task.status(chalk.gray('·'));
        tasks[pkg.rawSpec] = task;
        return task;
    }
    const pkgDataMap = {};
    const resMap = {};
    logger.on('progress', (pkg, level, pkgSpec, status, args) => {
        // the `|| {}` is a temporal fix. For some reason package.json is not logged when it is taken from cache
        const pkgData = pkgDataMap[pkgSpec] || {}; // package.json
        const res = resMap[pkgSpec]; // resolution
        // lazy get task
        function t() {
            return getTask(pkg);
        }
        // the first thing it (probably) does is wait in queue to query the npm registry
        if (status === 'resolving') {
            t().status(chalk.yellow('finding ·'));
        }
        else if (status === 'resolved') {
            resMap[pkgSpec] = args;
        }
        else if (status === 'download-queued') {
            if (res.version) {
                t().status(chalk.gray('queued ' + res.version + ' ↓'));
            }
            else {
                t().status(chalk.gray('queued ↓'));
            }
        }
        else if (status === 'downloading' || status === 'download-start') {
            if (res.version) {
                t().status(chalk.yellow('downloading ' + res.version + ' ↓'));
            }
            else {
                t().status(chalk.yellow('downloading ↓'));
            }
            const downloadStatus = args;
            if (downloadStatus && downloadStatus.total && downloadStatus.done < downloadStatus.total) {
                t().details('' + Math.round(downloadStatus.done / downloadStatus.total * 100) + '%');
            }
            else {
                t().details('');
            }
        }
        else if (status === 'done') {
            if (pkgData) {
                t().status(chalk.green('' + pkgData.version + ' ✓'))
                    .details('');
            }
            else {
                t().status(chalk.green('OK ✓'))
                    .details('');
            }
        }
        else if (status === 'package.json') {
            pkgDataMap[pkgSpec] = args;
        }
        else if (status === 'dependencies') {
            t().status(chalk.gray('' + pkgData.version + ' ·'))
                .details('');
        }
        else if (status === 'error') {
            t().status(chalk.red('ERROR ✗'))
                .details('');
        }
        else {
            t().status(status)
                .details('');
        }
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
//# sourceMappingURL=pretty.js.map