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