"use strict";

var mlink = require("../index");
var config = require('../../config');
var Router = mlink.Router;

var _require = require("../../util"),
    simulator = _require.simulator,
    util = _require.util;

var debuggerRouter = Router.get("debugger");

debuggerRouter.registerHandler(function (message) {
  if (message.payload.method === "WxDebug.applyChannelId") {
    var channelId = debuggerRouter.newChannel(config.CHANNELID);
    message.payload = {
      method: "WxDebug.pushChannelId",
      params: {
        channelId: channelId,
        connectUrl: util.getConnectUrl(channelId)
      }
    };
    message.reply();
  } else if (message.payload.method === "WxDebug.simrun") {
    simulator.connect(message.payload.params).catch(function (e) {
      debuggerRouter.pushMessage("page.entry", {
        method: "WxDebug.prompt",
        params: {
          messageText: "PLEASE_INSTALL_XCODE",
          channelId: message.payload.params
        }
      });
    });
  }
}).at("page.entry");