'use strict';

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

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _inquirer = require('inquirer');

var _inquirer2 = _interopRequireDefault(_inquirer);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

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

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"); }); }; }

class SocketCreate {
  constructor(context) {
    this.session = context.session;
    this.Socket = context.Socket;
  }

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

    return _asyncToGenerator(function* () {
      let socketName = socketNameArg;
      if (socketNameArg === '.') {
        socketName = _path2.default.parse(process.cwd()).name;
      }

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

      (0, _printTools.echo)();
      const response = yield _inquirer2.default.prompt(questions);
      const template = response.template.split(' - ')[0] // find name
      .replace(/ /g, ''); // strip padding

      (0, _printTools.echo)();
      return _this.Socket.create(socketName, template).then(function (socket) {
        (0, _printTools.echo)(4)(`Your Socket configuration is stored at ${_chalk2.default.cyan(socket.getSocketPath())}`);
        (0, _printTools.echo)();
      }).catch(function (err) {
        return (0, _printTools.error)(err);
      });
    })();
  }
}

exports.default = SocketCreate;
module.exports = exports['default'];
//# sourceMappingURL=socket-create.js.map