"use strict";
// tslint:disable restrict-plus-operands
Object.defineProperty(exports, "__esModule", { value: true });
const chalk = require("chalk");
const supportsColor = require("supports-color");
const deps_1 = require("../deps");
const base_1 = require("./base");
/* eslint-disable-next-line node/no-missing-require */
const spinners = require('./spinners');
function color(s) {
    if (!supportsColor)
        return s;
    const has256 = supportsColor.stdout ? supportsColor.stdout.has256 : (process.env.TERM || '').includes('256');
    return has256 ? `\u001B[38;5;104m${s}${deps_1.default.ansiStyles.reset.open}` : chalk.magenta(s);
}
class SpinnerAction extends base_1.ActionBase {
    constructor() {
        super();
        this.type = 'spinner';
        this.frames = spinners[process.platform === 'win32' ? 'line' : 'dots2'].frames;
        this.frameIndex = 0;
    }
    _start() {
        this._reset();
        if (this.spinner)
            clearInterval(this.spinner);
        this._render();
        this.spinner = setInterval(icon => this._render.bind(this)(icon), process.platform === 'win32' ? 500 : 100, 'spinner');
        const interval = this.spinner;
        interval.unref();
    }
    _stop(status) {
        if (this.task)
            this.task.status = status;
        if (this.spinner)
            clearInterval(this.spinner);
        this._render();
        this.output = undefined;
    }
    _pause(icon) {
        if (this.spinner)
            clearInterval(this.spinner);
        this._reset();
        if (icon)
            this._render(` ${icon}`);
        this.output = undefined;
    }
    _frame() {
        const frame = this.frames[this.frameIndex];
        this.frameIndex = ++this.frameIndex % this.frames.length;
        return color(frame);
    }
    _render(icon) {
        const task = this.task;
        if (!task)
            return;
        this._reset();
        this._flushStdout();
        const frame = icon === 'spinner' ? ` ${this._frame()}` : icon || '';
        const status = task.status ? ` ${task.status}` : '';
        this.output = `${task.action}...${frame}${status}\n`;
        this._write(this.std, this.output);
    }
    _reset() {
        if (!this.output)
            return;
        const lines = this._lines(this.output);
        this._write(this.std, deps_1.default.ansiEscapes.cursorLeft + deps_1.default.ansiEscapes.cursorUp(lines) + deps_1.default.ansiEscapes.eraseDown);
        this.output = undefined;
    }
    _lines(s) {
        return deps_1.default
            .stripAnsi(s)
            .split('\n')
            .map(l => Math.ceil(l.length / deps_1.default.screen.errtermwidth))
            .reduce((c, i) => c + i, 0);
    }
}
exports.default = SpinnerAction;
