"use strict";

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

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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

var Router = require("koa-router");
var mlink = require("../../link");
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();

wsRouter.all("/page/entry", function () {
  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx, next) {
    var terminal;
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            terminal = new WebsocketTerminal(ctx.websocket);

            entryHub.join(terminal, false);
            _context.next = 4;
            return next();

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

  return function (_x, _x2) {
    return _ref.apply(this, arguments);
  };
}());

wsRouter.all("/debugProxy/inspector/:channelId", function () {
  var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(ctx, next) {
    var terminal;
    return _regenerator2.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            terminal = new WebsocketTerminal(ctx.websocket);

            terminal.channelId = ctx.params.channelId;
            inspectorHub.join(terminal, false);
            _context2.next = 5;
            return next();

          case 5:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, undefined);
  }));

  return function (_x3, _x4) {
    return _ref2.apply(this, arguments);
  };
}());

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

            terminal.channelId = ctx.params.channelId;
            proxyDebuggerHub.join(terminal, true);
            _context3.next = 5;
            return next();

          case 5:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3, undefined);
  }));

  return function (_x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}());

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

            terminal.channelId = ctx.params.channelId;
            runtimeWorkerHub.join(terminal, true);
            _context4.next = 5;
            return next();

          case 5:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4, undefined);
  }));

  return function (_x7, _x8) {
    return _ref4.apply(this, arguments);
  };
}());

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

            terminal.channelId = ctx.params.channelId;
            proxyNativeHub.join(terminal, true);
            _context5.next = 5;
            return next();

          case 5:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5, undefined);
  }));

  return function (_x9, _x10) {
    return _ref5.apply(this, arguments);
  };
}());

module.exports = wsRouter;