"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskWrapper = void 0;
/* eslint-disable no-control-regex */
const through_1 = __importDefault(require("through"));
const listr_interface_1 = require("../interfaces/listr.interface");
const state_constants_1 = require("../interfaces/state.constants");
const index_1 = require("../index");
const prompt_1 = require("../utils/prompt");
/**
 * Extend the task to have more functionality while accesing from the outside.
 */
class TaskWrapper {
    constructor(task, errors, options) {
        this.task = task;
        this.errors = errors;
        this.options = options;
    }
    set title(data) {
        this.task.title$ = data;
    }
    /* istanbul ignore next */
    get title() {
        return this.task.title;
    }
    set output(data) {
        this.task.output$ = data;
    }
    /* istanbul ignore next */
    get output() {
        return this.task.output;
    }
    set state(data) {
        this.task.state$ = data;
    }
    set message(data) {
        this.task.message$ = data;
    }
    newListr(task, options) {
        let tasks;
        if (typeof task === 'function') {
            tasks = task(this);
        }
        else {
            tasks = task;
        }
        return new index_1.Listr(tasks, options);
    }
    report(error) {
        var _a, _b;
        /* istanbul ignore if */
        if (error instanceof listr_interface_1.ListrError) {
            for (const err of error.errors) {
                this.errors.push(err);
                this.message = { error: err.message || ((_a = this.task) === null || _a === void 0 ? void 0 : _a.title) || 'Task with no title.' };
            }
        }
        else {
            this.errors.push(error);
            this.message = { error: error.message || ((_b = this.task) === null || _b === void 0 ? void 0 : _b.title) || 'Task with no title.' };
        }
    }
    /* istanbul ignore next */
    cancelPrompt(throwError = false) {
        return prompt_1.destroyPrompt.bind(this)(throwError);
    }
    skip(message) {
        var _a;
        this.state = state_constants_1.StateConstants.SKIPPED;
        if (message) {
            this.message = { skip: message || ((_a = this.task) === null || _a === void 0 ? void 0 : _a.title) || 'Task with no title.' };
        }
    }
    async prompt(options) {
        var _a;
        return prompt_1.createPrompt.bind(this)(options, { ...(_a = this.options) === null || _a === void 0 ? void 0 : _a.injectWrapper });
    }
    stdout() {
        return through_1.default((chunk) => {
            const pattern = new RegExp('(?:\\u001b|\\u009b)\\[[\\=><~/#&.:=?%@~_-]*[0-9]*[\\a-ln-tqyz=><~/#&.:=?%@~_-]+', 'gmi');
            chunk = chunk.toString();
            chunk = chunk.replace(pattern, '');
            chunk = chunk.replace(new RegExp(/\u0007/, 'gmi'), '');
            if (chunk !== '') {
                this.output = chunk;
            }
        });
    }
    run(ctx) {
        return this.task.run(ctx, this);
    }
}
exports.TaskWrapper = TaskWrapper;
