(function(d,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],r):(d=typeof globalThis<"u"?globalThis:d||self,r(d.trame_iframe={},d.Vue))})(this,function(d,r){"use strict";const b={IFrame:{setup(f,{emit:u,expose:i}){const c=r.ref(null);function m(o){c.value&&c.value.contentWindow.postMessage(o,"*")}function s(o){var g,w;(g=o==null?void 0:o.data)!=null&&g.emit&&u(o.data.emit,(w=o.data)==null?void 0:w.value)}return r.onMounted(()=>{c.value.contentWindow.addEventListener("message",s)}),r.onBeforeUnmount(()=>{c.value.contentWindow.removeEventListener("message",s)}),i({triggerEmit:s,postMessage:m}),{triggerEmit:s,postMessage:m,elem:c}},template:'<iframe ref="elem" v-bind="$attrs"></iframe>'},Communicator:{props:["targetOrigin","enableRpc"],props:{targetOrigin:{type:String},enableRpc:{type:Boolean,default:!1}},setup(f,{emit:u}){const i=r.inject("trame"),c=f.targetOrigin,m=f.enableRpc;function s(t){c?window.parent.postMessage(t,c):window.postMessage(t,"*")}i.state.ready&&s({event:"stateReady"});function o(t){var a,e,n;(a=t==null?void 0:t.data)!=null&&a.emit&&u(t.data.emit,(e=t.data)==null?void 0:e.value),(n=t==null?void 0:t.data)!=null&&n.state&&i.state.update(t.data.state)}function g(t,a,e){const n=(...p)=>{s({rpcCallback:{channel:"trame.state.watch",id:t,payload:{stateVars:p}}})};i.state.watch(a,n)}function w(t,a){const e=(n,...p)=>{s({rpcCallback:{channel:"trame.trigger",id:t,payload:{error:n,result:p}}})};i.trigger(a.name,a.args,a.kwargs).then(e.bind(null,!1)).catch(e.bind(null,!0))}function k(t,a){const e=i.state.get(a.key);s({rpcCallback:{channel:"trame.state.get",id:t,payload:{result:e}}})}function y(t){var p;if(t.origin!==c||!((p=t==null?void 0:t.data)!=null&&p.rpc))return;const{obj:a,method:e,args:n}=t.data.rpc;switch(a){case"trame":switch(e){case"trigger":{const{eventId:l}=t.data.meta;w(l,n)}break}break;case"trame.state":switch(e){case"watch":{const{eventId:l}=t.data.meta;g(l,n)}break;case"get":{const{eventId:l}=t.data.meta;k(l,n)}break;default:if(typeof i.state[e]!="function"){console.error(`unsupported trame.state rpc; ${e}`);return}i.state[e](...n);break}break;default:console.error(`unsupported rpc: ${a}`)}}return r.onMounted(()=>{m&&window.addEventListener("message",y),window.addEventListener("message",o)}),r.onBeforeUnmount(()=>{m&&window.removeEventListener("message",y),window.removeEventListener("message",o)}),{postMessage:s,triggerEmit:o}}}};function h(f){Object.keys(b).forEach(u=>{f.component(u,b[u])})}d.install=h,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
