"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.pressAnyKeyToContinue = exports.inquirerPrompt = exports.generateOptions = void 0;var _inquirer = _interopRequireDefault(require("inquirer"));
var _logger = require("../core/systemManager/logger");
var _config = _interopRequireDefault(require("../core/configManager/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}

var inquirerPrompt = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params) {var _c$program;var c, msg, type, name;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            c = _config["default"].getConfig();
            msg = params.logMessage || params.warningMessage || params.message;if (!((_c$program =
            c.program) !== null && _c$program !== void 0 && _c$program.ci)) {_context.next = 7;break;}if (!(
            Array.isArray(params.choices) && typeof params["default"] !== 'undefined' && params.choices.includes(params["default"]))) {_context.next = 6;break;}
            (0, _logger.logDebug)("defaulting to choice '".concat(params["default"], "' for prompt '").concat(params.name, "'"));return _context.abrupt("return",
            Promise.resolve(_defineProperty({}, params.name, params["default"])));case 6:return _context.abrupt("return",

            Promise.reject("--ci option does not allow prompts. question: ".concat(msg, ".")));case 7:

            if (msg && params.logMessage) (0, _logger.logTask)(msg, (0, _logger.chalk)().grey);
            if (msg && params.warningMessage) (0, _logger.logWarning)(msg);

            // allow passing in just { type: 'prompt', ... } instead of { type: 'prompt', name: 'prompt', ... }
            type = params.type, name = params.name;
            if (type === 'confirm' && !name) params.name = 'confirm';return _context.abrupt("return",

            _inquirer["default"].prompt(params));case 12:case "end":return _context.stop();}}}, _callee);}));return function inquirerPrompt(_x) {return _ref.apply(this, arguments);};}();exports.inquirerPrompt = inquirerPrompt;


var pressAnyKeyToContinue = function pressAnyKeyToContinue() {
  var params = {
    type: 'input',
    name: 'confirm',
    message: 'Press any key to continue' };

  return _inquirer["default"].prompt(params);
};exports.pressAnyKeyToContinue = pressAnyKeyToContinue;

var generateOptions = function generateOptions(
inputData)



{var isMultiChoice = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;var mapping = arguments.length > 2 ? arguments[2] : undefined;var renderMethod = arguments.length > 3 ? arguments[3] : undefined;
  (0, _logger.logDebug)('generateOptions', isMultiChoice);
  var asString = '';
  var valuesAsObject = {};
  var valuesAsArray = [];
  var keysAsObject = {};
  var keysAsArray = [];
  var optionsAsArray = [];
  var isArray = Array.isArray(inputData);

  var output = {};
  var renderer = renderMethod || _generateOptionString;
  if (isArray) {
    inputData.forEach(function (v, i) {
      var rn = renderer(i, v, mapping, v);
      asString += rn;
      optionsAsArray.push(rn);
      valuesAsArray.push(v);
      if (!mapping) keysAsArray.push(v);
      if (!mapping) valuesAsObject[v] = v;
    });
  } else {
    var i = 0;
    Object.keys(inputData).forEach(function (k) {
      var v = inputData[k];
      var rn = renderer(i, v, mapping, k);
      asString += rn;
      optionsAsArray.push(rn);
      keysAsArray.push(k);
      keysAsObject[k] = true;
      valuesAsObject[k] = v;
      valuesAsArray.push(v);
      i++;
    });
  }
  output.keysAsArray = keysAsArray.sort(_sort);
  output.valuesAsArray = valuesAsArray.sort(_sort);
  output.keysAsObject = keysAsObject;
  output.valuesAsObject = valuesAsObject;
  output.asString = asString;
  output.optionsAsArray = optionsAsArray;
  return output;
};exports.generateOptions = generateOptions;

var _sort = function _sort(a, b) {
  var aStr = '';
  var bStr = '';
  if (typeof a === 'string') {
    // TODO: temp fix for weird issue when a/b are marked as string
    // but toLowerCase() is undefined. need to investigate
    aStr = a.toLowerCase ? a.toLowerCase() : a;
    bStr = b.toLowerCase ? b.toLowerCase() : b;
  } else {
    if (a && a.name) aStr = a.name.toLowerCase();
    if (b && b.name) bStr = b.name.toLowerCase();
  }

  var com = 0;
  if (aStr > bStr) {
    com = 1;
  } else if (aStr < bStr) {
    com = -1;
  }
  return com;
};

var _generateOptionString = function _generateOptionString(i, obj, mapping, defaultVal) {return " [".concat((0, _logger.chalk)().bold.grey(i + 1), "]> ").concat((0, _logger.chalk)().bold.grey(mapping ? '' : defaultVal), " \n");};
//# sourceMappingURL=prompt.js.map