"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionsConfirmResolver = exports.OptionsResolver = exports.OptionsChoiceResolver = void 0;
/*
 *
 * Copyright 2018-present NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
const clime_1 = require("clime");
const prompts = require("prompts");
const formatter_service_1 = require("./services/formatter.service");
/**
 * Passed as option to Prompt,
 * it enables a clean behaviour when an user abords a prompt serie
 */
const onCancel = () => process.exit();
const OptionsChoiceResolver = async (options, key, promptText, choices, type = 'select', validation) => {
    var _a, _b;
    if (options[key] !== undefined) {
        const title = `${options[key]}`;
        if (validation !== undefined) {
            const test = validation.validate(title);
            if (typeof test === 'string') {
                console.log(formatter_service_1.FormatterService.error(test));
                return process.exit();
            }
        }
        const value = (_a = choices.find((item) => item.title === title)) === null || _a === void 0 ? void 0 : _a.value;
        if (value === undefined) {
            value == ((_b = choices.find((item) => `${item.value}` === title)) === null || _b === void 0 ? void 0 : _b.value);
        }
        if (value === undefined) {
            throw new clime_1.ExpectedError(`'${key}' option '${options[key]}' is not a valid choice!`);
        }
        return value;
    }
    else {
        return (await prompts({
            type,
            name: key,
            message: promptText,
            choices,
            validate: validation !== undefined ? (result) => validation.validate(result) : () => true,
        }, { onCancel }))[key];
    }
};
exports.OptionsChoiceResolver = OptionsChoiceResolver;
const OptionsResolver = async (options, key, secondSource, promptText, type = 'text', validation) => {
    let value;
    if (options[key] !== undefined) {
        value = typeof options[key] === 'string' ? options[key].trim() : options[key];
        if (validation !== undefined) {
            const test = validation.validate(value);
            if (typeof test === 'string') {
                console.log(formatter_service_1.FormatterService.error(test));
                return process.exit();
            }
        }
    }
    else if (secondSource()) {
        const resolvedSecondSource = secondSource();
        value = resolvedSecondSource ? resolvedSecondSource : '';
    }
    else {
        value = (await prompts({
            type,
            name: key,
            message: promptText,
            validate: validation !== undefined ? (result) => validation.validate(result) : () => true,
        }, { onCancel }))[key];
    }
    return value;
};
exports.OptionsResolver = OptionsResolver;
const OptionsConfirmResolver = async (options, key, promptText, type = 'confirm', initial = true, name = 'value') => options[key]
    ? options[key]
    : (await prompts({
        type,
        name,
        message: promptText,
        initial,
    }, { onCancel }))[name];
exports.OptionsConfirmResolver = OptionsConfirmResolver;
