"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.taskRnvPluginAdd = exports["default"] = void 0;var _inquirer = _interopRequireDefault(require("inquirer"));
var _ora = _interopRequireDefault(require("ora"));
var _configManager = require("../../core/configManager");
var _constants = require("../../core/constants");
var _logger = require("../../core/systemManager/logger");




var _pluginManager = require("../../core/pluginManager");
var _taskManager = require("../../core/taskManager");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": 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);});};}


/* eslint-disable no-await-in-loop */
var taskRnvPluginAdd = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, parentTask, originTask) {var selPluginKey, o, selPlugin, selectedPlugins, installMessage, _yield$inquirer$promp, plugin, questionPlugins, pluginKeys, i, pluginKey, _plugin, pluginProps, finalProps, i2, _yield$inquirer$promp2, propValue, spinner;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logTask)('taskRnvPluginAdd');_context.next = 3;return (

              (0, _taskManager.executeTask)(c, _constants.TASK_PROJECT_CONFIGURE, _constants.TASK_PLUGIN_ADD, originTask));case 3:if (!

            c.runtime.isWrapper) {_context.next = 5;break;}return _context.abrupt("return",
            Promise.reject('Adding plugins in wrapper project is not supported.'));case 5:


            selPluginKey = c.program.rawArgs[4];

            o = (0, _pluginManager.getPluginList)(c);

            selPlugin = selPluginKey && o.allPlugins[selPluginKey];
            selectedPlugins = {};
            installMessage = [];if (

            selPlugin) {_context.next = 19;break;}_context.next = 13;return (
              _inquirer["default"].prompt({
                name: 'plugin',
                type: 'rawlist',
                message: 'Select the plugins you want to add',
                choices: o.asArray,
                pageSize: 50 }));case 13:_yield$inquirer$promp = _context.sent;plugin = _yield$inquirer$promp.plugin;


            selectedPlugins[plugin] = o.allPlugins[plugin];
            installMessage.push("".concat(
            (0, _logger.chalk)().white(plugin), " v(").concat((0, _logger.chalk)().green(
            o.allPlugins[plugin].version), ")"));_context.next = 21;break;case 19:



            selectedPlugins[selPluginKey] = selPlugin;
            installMessage.push("".concat(
            (0, _logger.chalk)().white(selPluginKey), " v(").concat((0, _logger.chalk)().green(selPlugin.version), ")"));case 21:



            questionPlugins = {};

            Object.keys(selectedPlugins).forEach(function (key) {
              // c.buildConfig.plugins[key] = 'source:rnv';
              var plugin = selectedPlugins[key];
              if (plugin.props) questionPlugins[key] = plugin;

              c.files.project.config.plugins[key] = 'source:rnv';

              // c.buildConfig.plugins[key] = selectedPlugins[key];
            });

            pluginKeys = Object.keys(questionPlugins);
            i = 0;case 25:if (!(i < pluginKeys.length)) {_context.next = 45;break;}
            pluginKey = pluginKeys[i];
            _plugin = questionPlugins[pluginKey];
            pluginProps = Object.keys(_plugin.props);
            finalProps = {};
            i2 = 0;case 31:if (!(i2 < pluginProps.length)) {_context.next = 40;break;}_context.next = 34;return (
              _inquirer["default"].prompt({
                name: 'propValue',
                type: 'input',
                message: "".concat(pluginKey, ": Add value for ").concat(
                pluginProps[i2], " (You can do this later in ./renative.json file)") }));case 34:_yield$inquirer$promp2 = _context.sent;propValue = _yield$inquirer$promp2.propValue;


            finalProps[pluginProps[i2]] = propValue;case 37:i2++;_context.next = 31;break;case 40:

            c.files.project.config.plugins[pluginKey] = {};
            c.files.project.config.plugins[pluginKey].props = finalProps;case 42:i++;_context.next = 25;break;case 45:


            spinner = (0, _ora["default"])("Installing: ".concat(installMessage.join(', '))).start();

            (0, _configManager.writeRenativeConfigFile)(c, c.paths.project.config, c.files.project.config);_context.next = 49;return (

              (0, _pluginManager.resolvePluginDependants)(c));case 49:_context.next = 51;return (

              (0, _taskManager.executeTask)(c, _constants.TASK_INSTALL, _constants.TASK_PLUGIN_ADD, originTask));case 51:

            spinner.succeed('All plugins installed!');
            (0, _logger.logSuccess)('Plugins installed successfully!');return _context.abrupt("return",
            true);case 54:case "end":return _context.stop();}}}, _callee);}));return function taskRnvPluginAdd(_x, _x2, _x3) {return _ref.apply(this, arguments);};}();exports.taskRnvPluginAdd = taskRnvPluginAdd;var _default =


{
  description: 'Add selected plugin to the project',
  fn: taskRnvPluginAdd,
  task: 'plugin add',
  params: _constants.PARAMS.withBase(),
  platforms: [] };exports["default"] = _default;
//# sourceMappingURL=task.rnv.plugin.add.js.map