'use strict';

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

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

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

    return _asyncToGenerator(function* () {
      const availabeVerionTypes = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
      if (cmd.bump && availabeVerionTypes.indexOf(cmd.bump) === -1) {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)(`Wrong version type: ${_chalk2.default.cyan(cmd.bump)}`);
        (0, _printTools.echo)(4)(`Choose from: ${availabeVerionTypes.join(', ')}`);
        (0, _printTools.echo)();
        process.exit();
      }

      _this.socket = yield _this.Socket.getLocal(socketName);

      if (!_this.socket.existLocally) {
        (0, _printTools.echo)();
        (0, _printTools.error)(4)(`Socket ${_chalk2.default.cyan(socketName)} cannot be found!`);
        (0, _printTools.echo)();
        process.exit();
      }

      if (cmd.bump) {
        _this.socket.bumpVersion(cmd.bump);
      }

      (0, _printTools.echo)();
      (0, _printTools.echon)(4)(`Submitting Socket ${_chalk2.default.cyan(_this.socket.name)} `);
      (0, _printTools.echon)(_chalk2.default.dim(`(${_this.socket.spec.version})... `));

      _this.socket.submit().then(function (submitStatus) {
        if (submitStatus.status === 200) {
          const publishCommand = _chalk2.default.cyan('syncano-cli socket publish', _this.socket.name);
          (0, _printTools.echo)(_chalk2.default.green('Done!'));
          (0, _printTools.echon)(4)('By default your socket is private 🔒. ');
          (0, _printTools.echo)(`Type ${publishCommand} to make it available for everyone!`);
          (0, _printTools.echo)();
        }
      }).catch(function (err) {
        if (err.response && err.response.data) {
          (0, _printTools.error)(4)(err.response.data.message);
          (0, _printTools.echo)();
        } else {
          (0, _printTools.error)(4)(err.message);
        }
      });
    })();
  }
}
exports.default = SocketSubmitCmd;
module.exports = exports['default'];
//# sourceMappingURL=socket-submit.js.map