'use strict';

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

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

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

var _debug2 = _interopRequireDefault(_debug);

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

var _dateUtils = require('../utils/date-utils');

var _socketSync = require('./socket-sync');

var _socketSync2 = _interopRequireDefault(_socketSync);

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _bluebird2.default(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 _bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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

class SocketInstall {
  constructor(context) {
    this.context = context;
    this.session = context.session;
    this.Socket = context.Socket;
    this.registry = new context.Registry();
  }

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

    return _asyncToGenerator(function* () {
      if (!socketName) return (0, _printTools.error)('Socket name is a required parameter!');

      return _this.registry.searchSocketByName(socketName).then(function (socketFromRegistry) {
        debug(`socket found: ${socketFromRegistry.name} ${socketFromRegistry.vesrion}`);
        if (!socketFromRegistry) {
          _bluebird2.default.reject(null);
        }

        _this.Socket.add(socketFromRegistry);

        const status = _chalk2.default.grey('socket added:');
        const name = _chalk2.default.blue(socketFromRegistry.name);
        const version = _chalk2.default.dim(`(${socketFromRegistry.version})`);
        (0, _printTools.echo)(7)(`${status} ${(0, _dateUtils.currentTime)()} ${name} ${version}`);

        const deploy = new _socketSync2.default(_this.context);
        return deploy.run([socketFromRegistry.name, {}]);
      }).catch(function (err) {
        if (err.response) {
          if (err.response.status === 404) {
            (0, _printTools.echo)();
            (0, _printTools.echo)(4)('No socket found 😕');
            (0, _printTools.echo)(4)(`To search for socket type: ${_chalk2.default.cyan('syncano-cli socket search <socket name>')}`);
            (0, _printTools.echo)();
            return;
          }
          if (err.response.data && err.response.data.name) {
            // Something wrong with the name
            (0, _printTools.echo)();
            (0, _printTools.error)(4)(_chalk2.default.red(`Socket "${socketName}" has already been installed!`));
            (0, _printTools.echo)(4)(`To upgrade type ${_chalk2.default.cyan(`syncano-cli socket upgrade ${socketName}`)}`);
            (0, _printTools.echo)();
          }
        } else {
          (0, _printTools.echo)();
          (0, _printTools.echo)(`${_chalk2.default.red(err)}\n`);
          (0, _printTools.echo)();
        }
      });
    })();
  }
}
exports.default = SocketInstall;
module.exports = exports['default'];
//# sourceMappingURL=socket-install.js.map