'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _inquirer = require('inquirer');

var _inquirer2 = _interopRequireDefault(_inquirer);

var _debug = require('../utils/debug');

var _debug2 = _interopRequireDefault(_debug);

var _printTools = require('../utils/print-tools');

var _uniqueInstance = require('../utils/unique-instance');

var _uniqueInstance2 = _interopRequireDefault(_uniqueInstance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const { debug } = (0, _debug2.default)('cmd-attach');

class Attach {
  constructor(context) {
    debug('Attach.constructor');
    this.session = context.session;
    this.Init = context.Init;
  }

  run([cmd = {}]) {
    var _this = this;

    return _asyncToGenerator(function* () {
      _this.cmd = cmd;
      _this.init = new _this.Init();

      if (_this.session.project) {
        const confirmQuestion = [{
          type: 'confirm',
          name: 'confirm',
          message: (0, _printTools.p)(2)('This project is already attached. Are you sure you want to change instance?'),
          default: false
        }];

        const { confirm } = (yield _inquirer2.default.prompt(confirmQuestion)) || {};
        if (confirm === false) return process.exit();
      }

      const questions = yield _this.getQuestions();
      const { instance } = (yield _inquirer2.default.prompt(questions)) || {};

      const respInstanceName = instance && instance !== (0, _printTools.p)(2)('Create a new one...') ? instance.trim() : null;
      let instanceName = cmd.instance || respInstanceName;

      if (!instanceName) {
        instanceName = yield _this.createNewInstance();
      }

      _this.init.addConfigFiles({ instance: instanceName }, _this.session.projectPath);

      (0, _printTools.echo)();
      return _this.session.load();
    })();
  }

  createNewInstance() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const randomName = (0, _uniqueInstance2.default)();
      const { instanceName } = yield _inquirer2.default.prompt([{
        name: 'instanceName',
        type: 'input',
        default: randomName,
        message: (0, _printTools.p)(2)('Choose instance name for your project:')
      }]);

      const instance = yield _this2.session.createInstance(instanceName);

      return instance.name;
    })();
  }

  getQuestions() {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      debug('getQuestions');
      const questions = [];

      if (!_this3.cmd.instance) {
        const instances = yield _this3.session.getInstances();
        const instancesNames = instances.map(function (instance) {
          return (0, _printTools.p)(2)(instance.name);
        });
        instancesNames.unshift((0, _printTools.p)(2)('Create a new one...'));

        questions.push({
          name: 'instance',
          type: 'list',
          pageSize: 16,
          message: (0, _printTools.p)(2)('Choose instance for your project:'),
          choices: instancesNames,
          default: 0
        });
      }

      return questions;
    })();
  }
}

exports.default = Attach;
module.exports = exports['default'];
//# sourceMappingURL=attach.js.map