var h="application/x-postmate-v1+json",y=5,w=0,k=function(){return++w},P=function(n){var e=document.createElement("a");e.href=n;var a=e.protocol.length>4?e.protocol:window.location.protocol,t=e.host.length?e.port==="80"||e.port==="443"?e.hostname:e.host:window.location.host;return e.origin||a+"//"+t},E={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},m=function(n,e){return!(typeof e=="string"&&n.origin!==e||!n.data||typeof n.data=="object"&&!("postmate"in n.data)||n.data.type!==h||!E[n.data.postmate])},L=function(n,e){var a=typeof n[e]=="function"?n[e]():n[e];return c.Promise.resolve(a)},O=(function(){function r(e){var a=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.events={},this.listener=function(t){if(!m(t,a.childOrigin))return!1;var s=((t||{}).data||{}).value||{},i=s.data,d=s.name;t.data.postmate==="emit"&&d in a.events&&a.events[d].call(a,i)},this.parent.addEventListener("message",this.listener,!1)}var n=r.prototype;return n.get=function(a){var t=this;return new c.Promise(function(s){var i=k(),d=function o(l){l.data.uid===i&&l.data.postmate==="reply"&&(t.parent.removeEventListener("message",o,!1),s(l.data.value))};t.parent.addEventListener("message",d,!1),t.child.postMessage({postmate:"request",type:h,property:a,uid:i},t.childOrigin)})},n.call=function(a,t){this.child.postMessage({postmate:"call",type:h,property:a,data:t},this.childOrigin)},n.on=function(a,t){this.events[a]=t},n.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},r})(),I=(function(){function r(e){var a=this;this.model=e.model,this.parent=e.parent,this.parentOrigin=e.parentOrigin,this.child=e.child,this.child.addEventListener("message",function(t){if(m(t,a.parentOrigin)){var s=t.data,i=s.property,d=s.uid,o=s.data;if(t.data.postmate==="call"){i in a.model&&typeof a.model[i]=="function"&&a.model[i](o);return}L(a.model,i).then(function(l){return t.source.postMessage({property:i,postmate:"reply",type:h,uid:d,value:l},t.origin)})}})}var n=r.prototype;return n.emit=function(a,t){this.parent.postMessage({postmate:"emit",type:h,value:{name:a,data:t}},this.parentOrigin)},r})(),c=(function(){function r(e){var a=e.container,t=a===void 0?typeof t<"u"?t:document.body:a,s=e.model,i=e.url,d=e.name,o=e.classListArray,l=o===void 0?[]:o;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=d||"",this.frame.classList.add.apply(this.frame.classList,l),t.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=s||{},this.sendHandshake(i)}var n=r.prototype;return n.sendHandshake=function(a){var t=this,s=P(a),i=0,d;return new r.Promise(function(o,l){var p=function u(f){return m(f,s)?f.data.postmate==="handshake-reply"?(clearInterval(d),t.parent.removeEventListener("message",u,!1),t.childOrigin=f.origin,o(new O(t))):l("Failed handshake"):!1};t.parent.addEventListener("message",p,!1);var v=function(){i++,t.child.postMessage({postmate:"handshake",type:h,model:t.model},s),i===y&&clearInterval(d)},g=function(){v(),d=setInterval(v,500)};t.frame.attachEvent?t.frame.attachEvent("onload",g):t.frame.onload=g,t.frame.src=a})},r})();c.debug=!1;c.Promise=(function(){try{return window?window.Promise:Promise}catch{return null}})();c.Model=(function(){function r(e){return this.child=window,this.model=e,this.parent=this.child.parent,this.sendHandshakeReply()}var n=r.prototype;return n.sendHandshakeReply=function(){var a=this;return new c.Promise(function(t,s){var i=function d(o){if(o.data.postmate){if(o.data.postmate==="handshake"){a.child.removeEventListener("message",d,!1),o.source.postMessage({postmate:"handshake-reply",type:h},o.origin),a.parentOrigin=o.origin;var l=o.data.model;return l&&Object.keys(l).forEach(function(p){a.model[p]=l[p]}),t(new I(a))}return s("Handshake Reply Failed")}};a.child.addEventListener("message",i,!1)})},r})();export{c as P};
