'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 _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 SocketUnInstall {
  constructor(context) {
    this.session = context.session;
    this.Socket = context.Socket;
  }

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

    return _asyncToGenerator(function* () {
      const confirmation = [{
        type: 'confirm',
        name: 'confirm',
        message: (0, _printTools.p)(2)(`Are you sure you want to remove: ${_chalk2.default.red(socketName)}`),
        default: false
      }];

      const socket = yield _this.Socket.get(socketName);

      if (!socket.existLocally && !socket.existRemotely) {
        (0, _printTools.warning)(`Socket ${socket.name} was not found on server nor in config!`);
        process.exit();
      }

      const promptResponse = yield _inquirer2.default.prompt(confirmation);
      if (!promptResponse.confirm) process.exit();
      (0, _printTools.echo)();

      return _this.Socket.uninstall(socket).then(function () {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)(`Socket ${_chalk2.default.cyan(socket.name)} has been ${_chalk2.default.green('successfully')} removed!`);
        (0, _printTools.echo)();
      }).catch(function (err) {
        (0, _printTools.warning)(err);
        (0, _printTools.echo)();
      });
    })();
  }
}
exports.default = SocketUnInstall;
module.exports = exports['default'];
//# sourceMappingURL=socket-uninstall.js.map