'use strict';

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

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _inquirer = require('inquirer');

var _inquirer2 = _interopRequireDefault(_inquirer);

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

var _debug2 = _interopRequireDefault(_debug);

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

var _login = require('./login');

var _login2 = _interopRequireDefault(_login);

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-init');

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

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

    return _asyncToGenerator(function* () {
      if (!_this.session.settings.account.authenticated()) {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)(_chalk2.default.red('You have to be logged in to initialize a new project!'));
        yield new _login2.default(_this.context).run([]);
      }

      _this.init = new _this.Init();

      const { project } = _this.session;
      const { instance } = cmd;

      const questions = [{
        name: 'Template',
        type: 'list',
        message: (0, _printTools.p)(2)('Choose template for your project'),
        choices: _this.init.getTemplatesChoices().map(function (choice) {
          return (0, _printTools.p)(4)(choice);
        }),
        default: 1
      }];

      if (!project) {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)(_chalk2.default.blue('New project? Exciting! 🎉'));
        (0, _printTools.echo)();
      } else {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)('I found the Syncano instance for the project in this folder,');
        (0, _printTools.echo)(4)("but you don't have any config files - I'll create them for you!");
        (0, _printTools.echo)();
      }

      _inquirer2.default.prompt(questions).then((() => {
        var _ref = _asyncToGenerator(function* (promptResponses) {
          _this.init.templateName = promptResponses.Template.split(' - ')[0] // find name
          .replace(/ /g, ''); // strip padding

          if (!project && instance) {
            yield _this.session.checkConnection(instance);
            _this.init.addConfigFiles({ instance });
            return _this.init.createFilesAndFolders();
          }

          if (!project && !instance) {
            return _this.session.createInstance().then(function (newInstance) {
              _this.init.addConfigFiles({ instance: newInstance.name });
              _this.init.createFilesAndFolders();
              return _this.session.load();
            });
          }

          if (_this.init.checkConfigFiles()) {
            return _this.init.noConfigFiles();
          }
        });

        return function (_x) {
          return _ref.apply(this, arguments);
        };
      })());
    })();
  }
}

exports.default = InitCmd;
module.exports = exports['default'];
//# sourceMappingURL=init.js.map