'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var Router = require('koa-router');
var mlink = require('../../mlink/midware');
var WebsocketTerminal = mlink.Terminal.WebsocketTerminal;

var inspectorHub = mlink.Hub.get('proxy.inspector');
var proxyNativeHub = mlink.Hub.get('proxy.native');
var proxyDebuggerHub = mlink.Hub.get('page.debugger');
var runtimeWorkerHub = mlink.Hub.get('runtime.worker');
var entryHub = mlink.Hub.get('page.entry');
var wsRouter = Router();

var _require = require('../../util/logger'),
    logger = _require.logger;

wsRouter.all('/page/entry', /*#__PURE__*/_regenerator2.default.mark(function _callee(next) {
  return _regenerator2.default.wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          logger.verbose('Joined entry hub joined');
          entryHub.join(new WebsocketTerminal(this.websocket));

        case 2:
        case 'end':
          return _context.stop();
      }
    }
  }, _callee, this);
}));
wsRouter.all('/debugProxy/inspector/:channelId', /*#__PURE__*/_regenerator2.default.mark(function _callee2(next) {
  var terminal;
  return _regenerator2.default.wrap(function _callee2$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          terminal = new WebsocketTerminal(this.websocket);

          terminal.channelId = this.params.channelId;
          logger.verbose('Joined entry hub joined, channelId -> ' + terminal.channelId);
          inspectorHub.join(terminal, true);
          _context2.next = 6;
          return next;

        case 6:
        case 'end':
          return _context2.stop();
      }
    }
  }, _callee2, this);
}));

wsRouter.all('/debugProxy/debugger/:channelId', /*#__PURE__*/_regenerator2.default.mark(function _callee3(next) {
  var terminal;
  return _regenerator2.default.wrap(function _callee3$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          terminal = new WebsocketTerminal(this.websocket);

          terminal.channelId = this.params.channelId;
          logger.verbose('Joined entry hub joined, channelId -> ' + terminal.channelId);
          proxyDebuggerHub.join(terminal, true);
          _context3.next = 6;
          return next;

        case 6:
        case 'end':
          return _context3.stop();
      }
    }
  }, _callee3, this);
}));

wsRouter.all('/debugProxy/runtime/:channelId', /*#__PURE__*/_regenerator2.default.mark(function _callee4(next) {
  var terminal;
  return _regenerator2.default.wrap(function _callee4$(_context4) {
    while (1) {
      switch (_context4.prev = _context4.next) {
        case 0:
          terminal = new WebsocketTerminal(this.websocket);

          terminal.channelId = this.params.channelId;
          logger.verbose('Joined entry hub joined, channelId -> ' + terminal.channelId);
          runtimeWorkerHub.join(terminal);
          _context4.next = 6;
          return next;

        case 6:
        case 'end':
          return _context4.stop();
      }
    }
  }, _callee4, this);
}));

wsRouter.all('/debugProxy/native/:channelId', /*#__PURE__*/_regenerator2.default.mark(function _callee5(next) {
  var terminal;
  return _regenerator2.default.wrap(function _callee5$(_context5) {
    while (1) {
      switch (_context5.prev = _context5.next) {
        case 0:
          terminal = new WebsocketTerminal(this.websocket);

          terminal.channelId = this.params.channelId;
          logger.verbose('Joined entry hub joined, channelId -> ' + terminal.channelId);
          proxyNativeHub.join(terminal, true);
          _context5.next = 6;
          return next;

        case 6:
        case 'end':
          return _context5.stop();
      }
    }
  }, _callee5, this);
}));
module.exports = wsRouter;