"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.destroyPrompt = exports.createPrompt = void 0;
const listr_interface_1 = require("../interfaces/listr.interface");
const state_constants_1 = require("../interfaces/state.constants");
const task_wrapper_1 = require("../lib/task-wrapper");
/**
 * Create a new prompt with Enquirer externally.
 * This extends enquirer so you dont have to give a name to single prompts and such so it is also
 * useful to use externally.
 * @param this
 * @param options
 * @param settings
 */
async function createPrompt(options, settings) {
    // override cancel callback
    let cancelCallback;
    /* istanbul ignore if */
    if (settings === null || settings === void 0 ? void 0 : settings.cancelCallback) {
        cancelCallback = settings.cancelCallback;
    } /* istanbul ignore next */
    else {
        cancelCallback = defaultCancelCallback;
    }
    // assign default if there is single prompt
    if (!Array.isArray(options)) {
        options = [{ ...options, name: 'default' }];
    } /* istanbul ignore next */
    else if (options.length === 1) {
        options = options.reduce((o, option) => {
            return [...o, Object.assign(option, { name: 'default' })];
        }, []);
    }
    // assign default enquirer options
    options = options.reduce((o, option) => {
        var _a;
        return [
            ...o,
            Object.assign(option, {
                // this is for outside calls, if it is not called from taskwrapper with bind
                stdout: this instanceof task_wrapper_1.TaskWrapper ? (_a = settings === null || settings === void 0 ? void 0 : settings.stdout) !== null && _a !== void 0 ? _a : this.stdout() : process.stdout,
                onCancel: cancelCallback.bind(this, settings)
            })
        ];
    }, []);
    let enquirer;
    if (settings === null || settings === void 0 ? void 0 : settings.enquirer) {
        // injected enquirer
        enquirer = settings.enquirer;
    }
    else {
        try {
            enquirer = new (await Promise.resolve().then(() => __importStar(require('enquirer')))).default();
        } /* istanbul ignore next */
        catch (e) {
            this.task.prompt = new listr_interface_1.PromptError('Enquirer is a peer dependency that must be installed seperately.');
            throw new Error(e);
        }
    }
    // i use this externally as well, this is a bandaid
    if (this instanceof task_wrapper_1.TaskWrapper) {
        // Capture the prompt instance so we can use it later
        enquirer.on('prompt', (prompt) => this.task.prompt = prompt);
        // Clear the prompt instance once it's submitted
        // Can't use on cancel, since that might hold a PromptError object
        enquirer.on('submit', () => this.task.prompt = undefined);
        this.task.subscribe((event) => {
            if (event.type === 'STATE' && event.data === state_constants_1.StateConstants.SKIPPED) {
                if (this.task.prompt && !(this.task.prompt instanceof listr_interface_1.PromptError)) {
                    this.task.prompt.submit();
                }
            }
        });
    }
    const response = (await enquirer.prompt(options));
    // return default name if it is single prompt
    if (options.length === 1) {
        return response.default;
    }
    else {
        return response;
    }
}
exports.createPrompt = createPrompt;
/* istanbul ignore next */
function destroyPrompt(throwError = false) {
    if (!this.task.prompt || this.task.prompt instanceof listr_interface_1.PromptError) {
        // If there's no prompt, can't cancel
        return;
    }
    if (throwError) {
        this.task.prompt.cancel();
    }
    else {
        this.task.prompt.submit();
    }
}
exports.destroyPrompt = destroyPrompt;
function defaultCancelCallback(settings) {
    const errorMsg = 'Cancelled prompt.';
    if (this instanceof task_wrapper_1.TaskWrapper) {
        this.task.prompt = new listr_interface_1.PromptError(errorMsg);
    } /* istanbul ignore next */
    else if ((settings === null || settings === void 0 ? void 0 : settings.error) !== false) {
        throw new Error(errorMsg);
    } /* istanbul ignore next */
    else {
        return errorMsg;
    }
}
