// Generated by CoffeeScript 1.12.7
(function() {
  var UIComponentServer, registerPort, root;

  window.addEventListener("message", registerPort = function(event) {
    return chrome.storage.local.get("vimiumSecret", function(arg) {
      var secret;
      secret = arg.vimiumSecret;
      if (!(event.source === window.parent && event.data === secret)) {
        return;
      }
      UIComponentServer.portOpen(event.ports[0]);
      return window.removeEventListener("message", registerPort);
    });
  });

  UIComponentServer = {
    ownerPagePort: null,
    handleMessage: null,
    portOpen: function(ownerPagePort) {
      this.ownerPagePort = ownerPagePort;
      this.ownerPagePort.onmessage = (function(_this) {
        return function(event) {
          return typeof _this.handleMessage === "function" ? _this.handleMessage(event) : void 0;
        };
      })(this);
      return this.registerIsReady();
    },
    registerHandler: function(handleMessage) {
      this.handleMessage = handleMessage;
    },
    postMessage: function(message) {
      var ref;
      return (ref = this.ownerPagePort) != null ? ref.postMessage(message) : void 0;
    },
    hide: function() {
      return this.postMessage("hide");
    },
    registerIsReady: (function() {
      var uiComponentIsReadyCount;
      uiComponentIsReadyCount = document.readyState === "loading" ? (window.addEventListener("DOMContentLoaded", function() {
        return UIComponentServer.registerIsReady();
      }), 0) : 1;
      return function() {
        if (++uiComponentIsReadyCount === 2) {
          if (window.frameId != null) {
            this.postMessage({
              name: "setIframeFrameId",
              iframeFrameId: window.frameId
            });
          }
          return this.postMessage("uiComponentIsReady");
        }
      };
    })()
  };

  root = typeof exports !== "undefined" && exports !== null ? exports : window;

  root.UIComponentServer = UIComponentServer;

  root.isVimiumUIComponent = true;

}).call(this);
