"use strict";
/*
 * @poppinss/cliui
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Task = void 0;
const pretty_hrtime_1 = __importDefault(require("pretty-hrtime"));
/**
 * Task exposes a very simple API to create tasks with states, along with a
 * listener to listen for the task state updates.
 *
 * The task itself has does not render anything to the console. The task
 * renderers does that.
 */
class Task {
    constructor(title) {
        this.title = title;
        this.onUpdateListener = () => { };
        /**
         * Task current state
         */
        this.state = 'idle';
    }
    /**
     * Bind a listener to listen to the state updates of the task
     */
    onUpdate(listener) {
        this.onUpdateListener = listener;
        return this;
    }
    /**
     * Start the task
     */
    start() {
        this.state = 'running';
        this.startTime = process.hrtime();
        this.onUpdateListener && this.onUpdateListener(this);
        return this;
    }
    /**
     * Mark task as completed
     */
    complete(message) {
        this.state = 'succeeded';
        this.duration = (0, pretty_hrtime_1.default)(process.hrtime(this.startTime));
        this.completionMessage = message;
        this.onUpdateListener && this.onUpdateListener(this);
        return this;
    }
    /**
     * Mark task as failed
     */
    fail(error) {
        this.state = 'failed';
        this.duration = (0, pretty_hrtime_1.default)(process.hrtime(this.startTime));
        this.completionMessage = error;
        this.onUpdateListener && this.onUpdateListener(this);
        return this;
    }
}
exports.Task = Task;
