"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Separator = exports.select = exports.search = exports.password = exports.input = exports.confirm = void 0;
exports.wrapPrompt = wrapPrompt;
/**
 * @fileoverview User prompt utilities for interactive scripts.
 * Provides inquirer.js integration with spinner support and context handling.
 */
const abort_signal_1 = require("./constants/abort-signal");
const spinner_1 = require("./constants/spinner");
const abortSignal = abort_signal_1;
const spinner = spinner_1;
/**
 * Wrap an inquirer prompt with spinner handling and signal injection.
 */
/*@__NO_SIDE_EFFECTS__*/
function wrapPrompt(inquirerPrompt) {
    return async (...args) => {
        const origContext = args.length > 1 ? args[1] : undefined;
        const { spinner: contextSpinner, ...contextWithoutSpinner } = origContext ?? {};
        const spinnerInstance = contextSpinner !== undefined ? contextSpinner : spinner;
        const signal = abortSignal;
        if (origContext) {
            args[1] = {
                signal,
                ...contextWithoutSpinner,
            };
        }
        else {
            args[1] = { signal };
        }
        const wasSpinning = !!spinnerInstance?.isSpinning;
        spinnerInstance?.stop();
        let result;
        try {
            result = await inquirerPrompt(...args);
        }
        catch (e) {
            if (e instanceof TypeError) {
                throw e;
            }
        }
        if (wasSpinning) {
            spinnerInstance.start();
        }
        return typeof result === 'string' ? result.trim() : result;
    };
}
// c8 ignore start - Third-party inquirer library requires and exports not testable in isolation.
const confirmExport = /*@__PURE__*/ require('../external/@inquirer/confirm');
const inputExport = /*@__PURE__*/ require('../external/@inquirer/input');
const passwordExport = /*@__PURE__*/ require('../external/@inquirer/password');
const searchExport = /*@__PURE__*/ require('../external/@inquirer/search');
const selectExport = /*@__PURE__*/ require('../external/@inquirer/select');
const confirmRaw = confirmExport.default ?? confirmExport;
const inputRaw = inputExport.default ?? inputExport;
const passwordRaw = passwordExport.default ?? passwordExport;
const searchRaw = searchExport.default ?? searchExport;
const selectRaw = selectExport.default ?? selectExport;
const ActualSeparator = selectExport.Separator;
exports.Separator = ActualSeparator;
// c8 ignore stop
exports.confirm = wrapPrompt(confirmRaw);
exports.input = wrapPrompt(inputRaw);
exports.password = wrapPrompt(passwordRaw);
exports.search = wrapPrompt(searchRaw);
exports.select = wrapPrompt(selectRaw);
//# sourceMappingURL=prompts.js.map