define(["ember"],function(a){var b,c,d,e;return e=[].slice,d="*",b=a.RSVP.Promise,c=a.CoreObject.extend({defaultMessage:{dest:"host",src:"client",payload:{module:"common"}},reqPool:[],reqSeed:0,listener:a.$({}),send:function(a){return window.postMessage(a,d)},getReqId:function(){return this.reqSeed+=1},receive:function(a){var b,c;switch(b=a.data,b.dest){case"host":break;case"client":if(b.response){if(c=this.reqPool.findBy("id",b.id),null!=c)return b.reject?c.defer.reject(b.payload):c.defer.resolve(b.payload),this.reqPool=this.reqPool.filter(function(a){return a!==c})}else if(this.listener.trigger("message",[b.payload]),null!=b.payload.event)return this.listener.trigger(b.payload.event,[b.payload])}},post:function(b,c,d){return null==c&&(c={}),null==d&&(d={}),d=a.$.extend(d,this.defaultMessage),d.payload=a.$.extend({},d.payload,c),d.payload.data=[b],this.send(d)},getMsg:function(){return a.$.extend({id:this.getReqId(),reply:!0},this.defaultMessage)},comn:function(){var a,b,c;return 2<=arguments.length?a=e.call(arguments,0,c=arguments.length-1):(c=0,a=[]),b=arguments[c++],this.communicate(a,b)},comnQ:function(){var b,c;return c=1<=arguments.length?e.call(arguments,0):[],b=this.getMsg(),b.payload=c.map(function(c){var d,e;return e=c.pop(),d=c,a.$.extend({data:d},b.payload,e)}),this._communicate(b)},request:function(a,b){return null==b&&(b={}),this.communicate([a],b)},communicate:function(b,c){var d;return d=this.getMsg(),d.payload=a.$.extend({},d.payload,c),d.payload.data=b,this._communicate(d)},_communicate:function(a){var c;return c={id:a.id,message:a,defer:Promise.defer()},this.reqPool.push(c),this.send(a),new b(function(a,b){return c.defer.promise.then(function(){return a.apply(null,arguments)},function(){return b.apply(null,arguments)})})}}).create(),window.addEventListener("message",c.receive.bind(c),!1),c});