'use strict';

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

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

var _debug2 = _interopRequireDefault(_debug);

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

var _socketListResponses = require('./socket-list-responses');

var _socketListResponses2 = _interopRequireDefault(_socketListResponses);

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 { info, debug } = (0, _debug2.default)('cmd-socket-list');

class SocketListCmd {
  constructor(context) {
    info('SocketListCmd.constructor', context);
    this.session = context.session;
    this.Socket = context.Socket;
    this.responses = (0, _socketListResponses2.default)(this.session);
  }

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

    return _asyncToGenerator(function* () {
      info('SocketListCmd.run');
      _this.fullPrint = cmd.full || !!socketName;

      if (socketName) {
        const socket = yield _this.Socket.get(socketName);
        (0, _printTools.echo)();
        _this.printSocket(socket);
      } else {
        const sockets = cmd.withDeps ? yield _this.Socket.flatList() : yield _this.Socket.list();
        _this.printSockets(sockets);
      }
    })();
  }

  printEndpoint(endpoint) {
    const endpointResponses = this.responses.endpoint(endpoint);
    (0, _printTools.echo)(8)(endpointResponses.name);
    (0, _printTools.echo)(8)(endpointResponses.description);
    (0, _printTools.echo)(8)(endpointResponses.url);
    if (!endpoint.existRemotely) {
      (0, _printTools.echo)(8)(endpointResponses.notSynced);
    }
    (0, _printTools.echo)();

    if (endpoint.metadata.parameters && this.fullPrint) {
      (0, _printTools.echo)(12)(this.responses.params);
      (0, _printTools.echo)();
      Object.keys(endpoint.metadata.parameters).forEach(param => {
        (0, _printTools.echo)(12)(endpointResponses.parameter(param).name);
        (0, _printTools.echo)(12)(endpointResponses.parameter(param).description);
        (0, _printTools.echo)(12)(endpointResponses.parameter(param).example);
        (0, _printTools.echo)();
      });
    }

    if (endpoint.metadata.response && this.fullPrint) {
      (0, _printTools.echo)(12)(endpointResponses.response().mimetype);
      (0, _printTools.echo)();
      endpoint.metadata.response.examples.forEach(example => {
        (0, _printTools.echo)(12)(endpointResponses.response(example).description);
        (0, _printTools.echo)(12)(endpointResponses.response(example).exitCode);
        (0, _printTools.echo)(12)(endpointResponses.response(example).example);
        (0, _printTools.echo)();
      });
    }
  }

  printEventHandler(eventHandler) {
    const metadata = eventHandler.metadata;
    const handlerResponses = this.responses.handler(eventHandler);

    (0, _printTools.echo)(8)(handlerResponses.name);
    (0, _printTools.echo)(8)(handlerResponses.description);
    (0, _printTools.echo)();

    if (metadata.parameters && this.fullPrint) {
      (0, _printTools.echo)(12)(this.responses.params);
      (0, _printTools.echo)();
      Object.keys(metadata.parameters).forEach(param => {
        (0, _printTools.echo)(12)(handlerResponses.parameter(param).name);
        (0, _printTools.echo)(12)(handlerResponses.parameter(param).description);
        (0, _printTools.echo)(12)(handlerResponses.parameter(param).example);
        (0, _printTools.echo)();
      });
    }
  }

  printEvent(event) {
    const eventResponses = this.responses.event(event);

    (0, _printTools.echo)(8)(eventResponses.name);
    (0, _printTools.echo)(8)(eventResponses.description);
    (0, _printTools.echo)();

    if (event.metadata.parameters && this.fullPrint) {
      (0, _printTools.echo)(12)(this.responses.params);
      (0, _printTools.echo)();
      Object.keys(event.metadata.parameters).forEach(param => {
        (0, _printTools.echo)(12)(eventResponses.parameter(param).name);
        (0, _printTools.echo)(12)(eventResponses.parameter(param).description);
        (0, _printTools.echo)(12)(eventResponses.parameter(param).example);
        (0, _printTools.echo)();
      });
    }
  }

  printSocket(socket) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      debug('printSocket');

      const endpoints = socket.getEndpoints();
      const eventHandlers = socket.getEventHandlers();
      const events = socket.getEvents();

      (0, _printTools.echo)(4)(_this2.responses.socket(socket).name);
      (0, _printTools.echo)(4)(_this2.responses.socket(socket).description);
      if (socket.version) (0, _printTools.echo)(4)(_this2.responses.socket(socket).version);
      (0, _printTools.echo)(4)(_this2.responses.socket(socket).type);
      (0, _printTools.echo)(4)(_this2.responses.socket(socket).status);
      (0, _printTools.echo)();

      endpoints.forEach(function (endpoint) {
        return _this2.printEndpoint(endpoint);
      });
      eventHandlers.forEach(function (eventHandler) {
        return _this2.printEventHandler(eventHandler);
      });
      events.forEach(function (event) {
        return _this2.printEvent(event);
      });
    })();
  }

  printSockets(sockets) {
    (0, _printTools.echo)();
    if (!sockets || sockets.length === 0) {
      (0, _printTools.echo)(4)(this.responses.lackSockets);
      (0, _printTools.echo)(4)(this.responses.createNewOne);
      (0, _printTools.echo)(4)(this.responses.installNewOne);
      (0, _printTools.echo)();

      return process.exit();
    }

    sockets.forEach(socket => this.printSocket(socket));
    (0, _printTools.echo)();
  }
}

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