(function() {
  var createIwcConnector;

  createIwcConnector = function(callback, options) {
    var IwcConnector, duiClient, init, iwcHandler, received_HB, userIwcHandler;
    userIwcHandler = null;
    if (options != null) {
      userIwcHandler = options.iwcHandler;
    }
    iwcHandler = {};
    duiClient = new DUIClient();
    duiClient.connect(function(intent) {
      var _ref;
      if ((_ref = iwcHandler[intent.action]) != null) {
        _ref.map(function(f) {
          return setTimeout(function() {
            return f(intent);
          }, 0);
        });
      }
      if (userIwcHandler != null) {
        return userIwcHandler(intent);
      }
    });
    duiClient.initOK();
    received_HB = null;
    IwcConnector = (function() {
      function IwcConnector(engine, HB, execution_listener, yatta) {
        var receiveHB, receive_, sendHistoryBuffer, send_;
        this.engine = engine;
        this.HB = HB;
        this.execution_listener = execution_listener;
        this.yatta = yatta;
        this.duiClient = duiClient;
        this.iwcHandler = iwcHandler;
        send_ = (function(_this) {
          return function(o) {
            if (Object.getOwnPropertyNames(_this.initialized).length !== 0) {
              return _this.send(o);
            }
          };
        })(this);
        this.execution_listener.push(send_);
        this.initialized = {};
        receiveHB = (function(_this) {
          return function(json) {
            var him;
            HB = json.extras.HB;
            him = json.extras.user;
            _this.engine.applyOpsCheckDouble(HB);
            return _this.initialized[him] = true;
          };
        })(this);
        iwcHandler["Yatta_push_HB_element"] = [receiveHB];
        this.sendIwcIntent("Yatta_get_HB_element", this.HB.getOperationCounter());
        receive_ = (function(_this) {
          return function(intent) {
            var o;
            o = intent.extras;
            if (_this.initialized[o.uid.creator] != null) {
              return _this.receive(o);
            }
          };
        })(this);
        this.iwcHandler["Yatta_new_operation"] = [receive_];
        if (received_HB != null) {
          this.engine.applyOpsCheckDouble(received_HB);
        }
        sendHistoryBuffer = (function(_this) {
          return function(intent) {
            var json, state_vector;
            state_vector = intent.extras;
            console.log(state_vector);
            json = {
              HB: _this.yatta.getHistoryBuffer()._encode(state_vector),
              user: _this.yatta.getUserId()
            };
            return _this.sendIwcIntent("Yatta_push_HB_element", json);
          };
        })(this);
        this.iwcHandler["Yatta_get_HB_element"] = [sendHistoryBuffer];
      }

      IwcConnector.prototype.setIwcHandler = function(f) {
        return userIwcHandler = f;
      };

      IwcConnector.prototype.sendIwcIntent = function(action_name, content) {
        var intent;
        intent = null;
        if (arguments.length >= 2) {
          action_name = arguments[0], content = arguments[1];
          intent = {
            action: action_name,
            component: "",
            data: "",
            dataType: "",
            flags: ["PUBLISH_GLOBAL"],
            extras: content
          };
        } else {
          intent = arguments[0];
        }
        return this.duiClient.sendIntent(intent);
      };

      IwcConnector.prototype.send = function(o) {
        if (o.uid.creator === this.HB.getUserId() && (typeof o.uid.op_number !== "string")) {
          return this.sendIwcIntent("Yatta_new_operation", o);
        }
      };

      IwcConnector.prototype.receive = function(o) {
        if (o.uid.creator !== this.HB.getUserId()) {
          return this.engine.applyOp(o);
        }
      };

      return IwcConnector;

    })();
    init = function() {
      var proposed_user_id;
      proposed_user_id = Math.floor(Math.random() * 1000000);
      return callback(IwcConnector, proposed_user_id);
    };
    setTimeout(init, 5000);
    return void 0;
  };

  module.exports = createIwcConnector;

  if (typeof window !== "undefined" && window !== null) {
    if (window.Y == null) {
      window.Y = {};
    }
    window.Y.createIwcConnector = createIwcConnector;
  }

}).call(this);

//# sourceMappingURL=../Connectors/IwcConnector.js.map