!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).mxwidgets=e()}}((function(){var e,t=function(e){var t;return function(r){return t||e(t={exports:{},parent:r},t.exports),t.exports}},r=t((function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=s({});Object.keys(r).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===r[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return r[e]}}))}));var c=a({});Object.keys(c).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===c[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return c[e]}}))})),Object.keys(K).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===K[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return K[e]}}))})),Object.keys(ee).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ee[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ee[e]}}))}));var l=u({});Object.keys(l).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===l[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return l[e]}}))})),Object.keys(te).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===te[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return te[e]}}))})),Object.keys(re).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===re[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return re[e]}}))})),Object.keys(ne).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ne[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ne[e]}}))})),Object.keys(ie).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ie[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ie[e]}}))})),Object.keys(I).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===I[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return I[e]}}))})),Object.keys(oe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===oe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return oe[e]}}))})),Object.keys(ae).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ae[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ae[e]}}))})),Object.keys(se).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===se[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return se[e]}}))})),Object.keys(P).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===P[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return P[e]}}))})),Object.keys(E).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===E[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return E[e]}}))})),Object.keys(C).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===C[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return C[e]}}))})),Object.keys(Y).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Y[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Y[e]}}))})),Object.keys(ue).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ue[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ue[e]}}))})),Object.keys(ce).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ce[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ce[e]}}))})),Object.keys(le).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===le[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return le[e]}}))})),Object.keys(de).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===de[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return de[e]}}))})),Object.keys(fe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===fe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return fe[e]}}))})),Object.keys(pe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===pe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return pe[e]}}))})),Object.keys(ve).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ve[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ve[e]}}))})),Object.keys(T).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===T[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return T[e]}}))})),Object.keys(he).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===he[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return he[e]}}))})),Object.keys(me).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===me[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return me[e]}}))})),Object.keys(be).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===be[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return be[e]}}))})),Object.keys(D).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===D[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return D[e]}}))})),Object.keys(we).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===we[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return we[e]}}))})),Object.keys(_e).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===_e[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return _e[e]}}))})),Object.keys(Oe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Oe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Oe[e]}}))})),Object.keys(ke).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===ke[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return ke[e]}}))})),Object.keys(Ee).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Ee[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Ee[e]}}))})),Object.keys(Se).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Se[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Se[e]}}))})),Object.keys(je).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===je[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return je[e]}}))})),Object.keys(Ce).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Ce[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Ce[e]}}))})),Object.keys(xe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===xe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return xe[e]}}))})),Object.keys(F).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===F[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return F[e]}}))})),Object.keys(Ae).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Ae[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Ae[e]}}))})),Object.keys(We).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===We[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return We[e]}}))}));var d=o({});Object.keys(d).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===d[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return d[e]}}))}));var f=i({});Object.keys(f).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===f[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return f[e]}}))})),Object.keys(Pe).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===Pe[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return Pe[e]}}))})),Object.keys(J).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===J[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return J[e]}}))}));var p=n({});Object.keys(p).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===p[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return p[e]}}))}))})),n=t((function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.WidgetDriver=void 0;var n=r({});function i(e,t){for(var r=0;re.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[i++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,s=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return s=e.done,e},e:function(e){u=!0,a=e},f:function(){try{s||null==r.return||r.return()}finally{if(u)throw a}}}}(t);try{for(o.s();!(r=o.n()).done;){var a=r.value,s=e.parseRoomWidget(a);s&&i.push(s)}}catch(u){o.e(u)}finally{o.f()}return i}},{key:"parseRoomWidget",value:function(t){if(!t)return null;if("m.widget"!==t.type&&"im.vector.modular.widgets"!==t.type)return null;var r=t.content||{},n={id:t.state_key,creatorUserId:r.creatorUserId||t.sender,name:r.name,type:r.type,url:r.url,waitForIframeLoad:r.waitForIframeLoad,data:r.data};return e.processEstimatedWidget(n)}},{key:"processEstimatedWidget",value:function(e){return e.id&&e.creatorUserId&&e.type&&(0,Ae.isValidUrl)(e.url)?new r.Widget(e):null}}],null&&i(t.prototype,null),o&&i(t,o),Object.defineProperty(t,"prototype",{writable:!1}),e}();t.WidgetParser=a})),o=t((function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Widget=void 0;var n=r({});function i(e,t){for(var r=0;r=0;--i){var o=this.tryEntries[i],a=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var s=n.call(o,"catchLoc"),u=n.call(o,"finallyLoc");if(s&&u){if(this.prev=0;--r){var i=this.tryEntries[r];if(i.tryLoc<=this.prev&&n.call(i,"finallyLoc")&&this.prev=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),f}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var i=n.arg;E(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:j(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},e}function o(e,t,r,n,i,o,a){try{var s=e[o](a),u=s.value}catch(c){return void r(c)}s.done?t(u):Promise.resolve(u).then(n,i)}function a(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var a=e.apply(t,r);function s(e){o(a,n,i,s,u,"next",e)}function u(e){o(a,n,i,s,u,"throw",e)}s(void 0)}))}}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r1&&void 0!==arguments[1]?arguments[1]:null;return this.allowedEvents.some((function(r){return r.matchesAsRoomEvent(F.EventDirection.Send,e,t)}))}},{key:"canSendStateEvent",value:function(e,t){return this.allowedEvents.some((function(r){return r.matchesAsStateEvent(F.EventDirection.Send,e,t)}))}},{key:"canSendToDeviceEvent",value:function(e){return this.allowedEvents.some((function(t){return t.matchesAsToDeviceEvent(F.EventDirection.Send,e)}))}},{key:"canReceiveRoomEvent",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return this.allowedEvents.some((function(r){return r.matchesAsRoomEvent(F.EventDirection.Receive,e,t)}))}},{key:"canReceiveStateEvent",value:function(e,t){return this.allowedEvents.some((function(r){return r.matchesAsStateEvent(F.EventDirection.Receive,e,t)}))}},{key:"canReceiveToDeviceEvent",value:function(e){return this.allowedEvents.some((function(t){return t.matchesAsToDeviceEvent(F.EventDirection.Receive,e)}))}},{key:"stop",value:function(){this.isStopped=!0,this.transport.stop()}},{key:"beginCapabilities",value:function(){var e,t=this;this.emit("preparing"),this.transport.send(P.WidgetApiToWidgetAction.Capabilities,{}).then((function(r){return e=r.capabilities,t.driver.validateCapabilities(new Set(r.capabilities))})).then((function(r){console.log("Widget ".concat(t.widget.id," is allowed capabilities:"),Array.from(r)),t.allowedCapabilities=r,t.allowedEvents=F.WidgetEventCapability.findEventCapabilities(r),t.notifyCapabilities(e),t.emit("ready")}))}},{key:"notifyCapabilities",value:function(e){var t=this;this.transport.send(P.WidgetApiToWidgetAction.NotifyCapabilities,{requested:e,approved:Array.from(this.allowedCapabilities)}).catch((function(e){console.warn("non-fatal error notifying widget of approved capabilities:",e)})).then((function(){t.emit("capabilitiesNotified")}))}},{key:"onIframeLoad",value:function(e){this.widget.waitForIframeLoad?this.beginCapabilities():this.contentLoadedActionSent=!1}},{key:"handleContentLoadedAction",value:function(e){if(this.contentLoadedActionSent)throw new Error("Improper sequence: ContentLoaded Action can only be send once after the widget loaded and should only be used if waitForIframeLoad is false (default=true)");this.widget.waitForIframeLoad?this.transport.reply(e,{error:{message:"Improper sequence: not expecting ContentLoaded event if waitForIframLoad is true (default=true)"}}):(this.transport.reply(e,{}),this.beginCapabilities()),this.contentLoadedActionSent=!0}},{key:"replyVersions",value:function(e){this.transport.reply(e,{supported_versions:C.CurrentApiVersions})}},{key:"handleCapabilitiesRenegotiate",value:function(e){var t,r=this;this.transport.reply(e,{});var n=(null===(t=e.data)||void 0===t?void 0:t.capabilities)||[],i=new Set(n.filter((function(e){return!r.hasCapability(e)})));if(0===i.size)return this.notifyCapabilities([]);this.driver.validateCapabilities(i).then((function(e){return e.forEach((function(e){return r.allowedCapabilities.add(e)})),F.WidgetEventCapability.findEventCapabilities(e).forEach((function(e){return r.allowedEvents.push(e)})),r.notifyCapabilities(Array.from(i))}))}},{key:"handleNavigate",value:function(e){var t,r,n=this;if(!this.hasCapability(Y.MatrixCapabilities.MSC2931Navigate))return this.transport.reply(e,{error:{message:"Missing capability"}});if(null===(t=e.data)||void 0===t||!t.uri||null===(r=e.data)||void 0===r||!r.uri.toString().startsWith("https://matrix.to/#"))return this.transport.reply(e,{error:{message:"Invalid matrix.to URI"}});var i=function(t){return console.error("[ClientWidgetApi] Failed to handle navigation: ",t),n.transport.reply(e,{error:{message:"Error handling navigation"}})};try{this.driver.navigate(e.data.uri.toString()).catch((function(e){return i(e)})).then((function(){return n.transport.reply(e,{})}))}catch(o){return i(o)}}},{key:"handleOIDC",value:function(e){var t=this,r=1,n=function(n,i){return i=i||{},r>1?t.transport.send(P.WidgetApiToWidgetAction.OpenIDCredentials,d({state:n,original_request_id:e.requestId},i)):t.transport.reply(e,d({state:n},i))},i=function(i){return console.error("[ClientWidgetApi] Failed to handle OIDC: ",i),r>1?n(T.OpenIDRequestState.Blocked):t.transport.reply(e,{error:{message:i}})},o=new J.SimpleObservable((function(e){return e.state===T.OpenIDRequestState.PendingUserConfirmation&&r>1?(o.close(),i("client provided out-of-phase response to OIDC flow")):e.state===T.OpenIDRequestState.PendingUserConfirmation?(n(e.state),void r++):e.state!==T.OpenIDRequestState.Allowed||e.token?(e.state===T.OpenIDRequestState.Blocked&&(e.token=void 0),o.close(),n(e.state,e.token)):i("client provided invalid OIDC token for an allowed request")}));this.driver.askOpenID(o)}},{key:"handleReadEvents",value:function(e){var t=this;if(!e.data.type)return this.transport.reply(e,{error:{message:"Invalid request - missing event type"}});if(void 0!==e.data.limit&&(!e.data.limit||e.data.limit<0))return this.transport.reply(e,{error:{message:"Invalid request - limit out of range"}});var r=null;if(e.data.room_ids){r=e.data.room_ids,Array.isArray(r)||(r=[r]);var n,i=function(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return s(e,void 0);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?s(e,void 0):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,i=function(){};return{s:i,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){u=!0,o=e},f:function(){try{a||null==r.return||r.return()}finally{if(u)throw o}}}}(r);try{for(i.s();!(n=i.n()).done;){var o=n.value;if(!this.canUseRoomTimeline(o))return this.transport.reply(e,{error:{message:"Unable to access room timeline: ".concat(o)}})}}catch(l){i.e(l)}finally{i.f()}}var a=e.data.limit||0,u=Promise.resolve([]);if(void 0!==e.data.state_key){var c=!0===e.data.state_key?void 0:e.data.state_key.toString();if(!this.canReceiveStateEvent(e.data.type,null!=c?c:null))return this.transport.reply(e,{error:{message:"Cannot read state events of this type"}});u=this.driver.readStateEvents(e.data.type,c,a,r)}else{if(!this.canReceiveRoomEvent(e.data.type,e.data.msgtype))return this.transport.reply(e,{error:{message:"Cannot read room events of this type"}});u=this.driver.readRoomEvents(e.data.type,e.data.msgtype,a,r)}return u.then((function(r){return t.transport.reply(e,{events:r})}))}},{key:"handleSendEvent",value:function(e){var t,r=this;if(!e.data.type)return this.transport.reply(e,{error:{message:"Invalid request - missing event type"}});if(e.data.room_id&&!this.canUseRoomTimeline(e.data.room_id))return this.transport.reply(e,{error:{message:"Unable to access room timeline: ".concat(e.data.room_id)}});if(null!==e.data.state_key&&void 0!==e.data.state_key){if(!this.canSendStateEvent(e.data.type,e.data.state_key))return this.transport.reply(e,{error:{message:"Cannot send state events of this type"}});t=this.driver.sendEvent(e.data.type,e.data.content||{},e.data.state_key,e.data.room_id)}else{var n=e.data.content||{},i=n.msgtype;if(!this.canSendRoomEvent(e.data.type,i))return this.transport.reply(e,{error:{message:"Cannot send room events of this type"}});t=this.driver.sendEvent(e.data.type,n,null,e.data.room_id)}t.then((function(t){return r.transport.reply(e,{room_id:t.roomId,event_id:t.eventId})})).catch((function(t){return console.error("error sending event: ",t),r.transport.reply(e,{error:{message:"Error sending event"}})}))}},{key:"handleSendToDevice",value:(j=a(i().mark((function e(t){return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t.data.type){e.next=5;break}return e.next=3,this.transport.reply(t,{error:{message:"Invalid request - missing event type"}});case 3:e.next=32;break;case 5:if(t.data.messages){e.next=10;break}return e.next=8,this.transport.reply(t,{error:{message:"Invalid request - missing event contents"}});case 8:e.next=32;break;case 10:if("boolean"==typeof t.data.encrypted){e.next=15;break}return e.next=13,this.transport.reply(t,{error:{message:"Invalid request - missing encryption flag"}});case 13:e.next=32;break;case 15:if(this.canSendToDeviceEvent(t.data.type)){e.next=20;break}return e.next=18,this.transport.reply(t,{error:{message:"Cannot send to-device events of this type"}});case 18:e.next=32;break;case 20:return e.prev=20,e.next=23,this.driver.sendToDevice(t.data.type,t.data.encrypted,t.data.messages);case 23:return e.next=25,this.transport.reply(t,{});case 25:e.next=32;break;case 27:return e.prev=27,e.t0=e.catch(20),console.error("error sending to-device event",e.t0),e.next=32,this.transport.reply(t,{error:{message:"Error sending event"}});case 32:case"end":return e.stop()}}),e,this,[[20,27]])}))),function(e){return j.apply(this,arguments)})},{key:"pollTurnServers",value:(S=a(i().mark((function e(t,r){var n,o,a,s,u,c;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,this.transport.send(P.WidgetApiToWidgetAction.UpdateTurnServers,r);case 3:n=!1,o=!1,e.prev=5,s=m(t);case 7:return e.next=9,s.next();case 9:if(!(n=!(u=e.sent).done)){e.next=16;break}return c=u.value,e.next=13,this.transport.send(P.WidgetApiToWidgetAction.UpdateTurnServers,c);case 13:n=!1,e.next=7;break;case 16:e.next=22;break;case 18:e.prev=18,e.t0=e.catch(5),o=!0,a=e.t0;case 22:if(e.prev=22,e.prev=23,!n||null==s.return){e.next=27;break}return e.next=27,s.return();case 27:if(e.prev=27,!o){e.next=30;break}throw a;case 30:return e.finish(27);case 31:return e.finish(22);case 32:e.next=37;break;case 34:e.prev=34,e.t1=e.catch(0),console.error("error polling for TURN servers",e.t1);case 37:case"end":return e.stop()}}),e,this,[[0,34],[5,18,22,32],[23,,27,31]])}))),function(e,t){return S.apply(this,arguments)})},{key:"handleWatchTurnServers",value:(k=a(i().mark((function e(t){var r,n,o,a;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.hasCapability(Y.MatrixCapabilities.MSC3846TurnServers)){e.next=5;break}return e.next=3,this.transport.reply(t,{error:{message:"Missing capability"}});case 3:e.next=30;break;case 5:if(!this.turnServers){e.next=10;break}return e.next=8,this.transport.reply(t,{});case 8:e.next=30;break;case 10:return e.prev=10,r=this.driver.getTurnServers(),e.next=14,r.next();case 14:if(n=e.sent,o=n.done,a=n.value,!o){e.next=19;break}throw new Error("Client refuses to provide any TURN servers");case 19:return e.next=21,this.transport.reply(t,{});case 21:this.pollTurnServers(r,a),this.turnServers=r,e.next=30;break;case 25:return e.prev=25,e.t0=e.catch(10),console.error("error getting first TURN server results",e.t0),e.next=30,this.transport.reply(t,{error:{message:"TURN servers not available"}});case 30:case"end":return e.stop()}}),e,this,[[10,25]])}))),function(e){return k.apply(this,arguments)})},{key:"handleUnwatchTurnServers",value:(O=a(i().mark((function e(t){return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.hasCapability(Y.MatrixCapabilities.MSC3846TurnServers)){e.next=5;break}return e.next=3,this.transport.reply(t,{error:{message:"Missing capability"}});case 3:e.next=15;break;case 5:if(this.turnServers){e.next=10;break}return e.next=8,this.transport.reply(t,{});case 8:e.next=15;break;case 10:return e.next=12,this.turnServers.return(void 0);case 12:return this.turnServers=null,e.next=15,this.transport.reply(t,{});case 15:case"end":return e.stop()}}),e,this)}))),function(e){return O.apply(this,arguments)})},{key:"handleReadRelations",value:(_=a(i().mark((function e(t){var r,n,o=this;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t.data.event_id){e.next=2;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Invalid request - missing event ID"}}));case 2:if(!(void 0!==t.data.limit&&t.data.limit<0)){e.next=4;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Invalid request - limit out of range"}}));case 4:if(void 0===t.data.room_id||this.canUseRoomTimeline(t.data.room_id)){e.next=6;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Unable to access room timeline: ".concat(t.data.room_id)}}));case 6:return e.prev=6,e.next=9,this.driver.readEventRelations(t.data.event_id,t.data.room_id,t.data.rel_type,t.data.event_type,t.data.from,t.data.to,t.data.limit,t.data.direction);case 9:return r=e.sent,n=r.chunk.filter((function(e){return void 0!==e.state_key?o.canReceiveStateEvent(e.type,e.state_key):o.canReceiveRoomEvent(e.type,e.content.msgtype)})),e.abrupt("return",this.transport.reply(t,{chunk:n,prev_batch:r.prevBatch,next_batch:r.nextBatch}));case 14:return e.prev=14,e.t0=e.catch(6),console.error("error getting the relations",e.t0),e.next=19,this.transport.reply(t,{error:{message:"Unexpected error while reading relations"}});case 19:case"end":return e.stop()}}),e,this,[[6,14]])}))),function(e){return _.apply(this,arguments)})},{key:"handleUserDirectorySearch",value:(w=a(i().mark((function e(t){var r;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.hasCapability(Y.MatrixCapabilities.MSC3973UserDirectorySearch)){e.next=2;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Missing capability"}}));case 2:if("string"==typeof t.data.search_term){e.next=4;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Invalid request - missing search term"}}));case 4:if(!(void 0!==t.data.limit&&t.data.limit<0)){e.next=6;break}return e.abrupt("return",this.transport.reply(t,{error:{message:"Invalid request - limit out of range"}}));case 6:return e.prev=6,e.next=9,this.driver.searchUserDirectory(t.data.search_term,t.data.limit);case 9:return r=e.sent,e.abrupt("return",this.transport.reply(t,{limited:r.limited,results:r.results.map((function(e){return{user_id:e.userId,display_name:e.displayName,avatar_url:e.avatarUrl}}))}));case 13:return e.prev=13,e.t0=e.catch(6),console.error("error searching in the user directory",e.t0),e.next=18,this.transport.reply(t,{error:{message:"Unexpected error while searching in the user directory"}});case 18:case"end":return e.stop()}}),e,this,[[6,13]])}))),function(e){return w.apply(this,arguments)})},{key:"handleMessage",value:function(e){if(!this.isStopped){var t=new CustomEvent("action:".concat(e.detail.action),{detail:e.detail,cancelable:!0});if(this.emit("action:".concat(e.detail.action),t),!t.defaultPrevented)switch(e.detail.action){case P.WidgetApiFromWidgetAction.ContentLoaded:return this.handleContentLoadedAction(e.detail);case P.WidgetApiFromWidgetAction.SupportedApiVersions:return this.replyVersions(e.detail);case P.WidgetApiFromWidgetAction.SendEvent:return this.handleSendEvent(e.detail);case P.WidgetApiFromWidgetAction.SendToDevice:return this.handleSendToDevice(e.detail);case P.WidgetApiFromWidgetAction.GetOpenIDCredentials:return this.handleOIDC(e.detail);case P.WidgetApiFromWidgetAction.MSC2931Navigate:return this.handleNavigate(e.detail);case P.WidgetApiFromWidgetAction.MSC2974RenegotiateCapabilities:return this.handleCapabilitiesRenegotiate(e.detail);case P.WidgetApiFromWidgetAction.MSC2876ReadEvents:return this.handleReadEvents(e.detail);case P.WidgetApiFromWidgetAction.WatchTurnServers:return this.handleWatchTurnServers(e.detail);case P.WidgetApiFromWidgetAction.UnwatchTurnServers:return this.handleUnwatchTurnServers(e.detail);case P.WidgetApiFromWidgetAction.MSC3869ReadRelations:return this.handleReadRelations(e.detail);case P.WidgetApiFromWidgetAction.MSC3973UserDirectorySearch:return this.handleUserDirectorySearch(e.detail);default:return this.transport.reply(e.detail,{error:{message:"Unknown or unsupported action: "+e.detail.action}})}}}},{key:"takeScreenshot",value:function(){return this.transport.send(P.WidgetApiToWidgetAction.TakeScreenshot,{})}},{key:"updateVisibility",value:function(e){return this.transport.send(P.WidgetApiToWidgetAction.UpdateVisibility,{visible:e})}},{key:"sendWidgetConfig",value:function(e){return this.transport.send(P.WidgetApiToWidgetAction.WidgetConfig,e).then()}},{key:"notifyModalWidgetButtonClicked",value:function(e){return this.transport.send(P.WidgetApiToWidgetAction.ButtonClicked,{id:e}).then()}},{key:"notifyModalWidgetClose",value:function(e){return this.transport.send(P.WidgetApiToWidgetAction.CloseModalWidget,e).then()}},{key:"feedEvent",value:(b=a(i().mark((function e(t,r){var n;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t.room_id===r||this.canUseRoomTimeline(t.room_id)){e.next=2;break}return e.abrupt("return");case 2:if(void 0===t.state_key||null===t.state_key){e.next=7;break}if(this.canReceiveStateEvent(t.type,t.state_key)){e.next=5;break}return e.abrupt("return");case 5:e.next=9;break;case 7:if(this.canReceiveRoomEvent(t.type,null===(n=t.content)||void 0===n?void 0:n.msgtype)){e.next=9;break}return e.abrupt("return");case 9:return e.next=11,this.transport.send(P.WidgetApiToWidgetAction.SendEvent,t);case 11:case"end":return e.stop()}}),e,this)}))),function(e,t){return b.apply(this,arguments)})},{key:"feedToDevice",value:(g=a(i().mark((function e(t,r){return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this.canReceiveToDeviceEvent(t.type)){e.next=3;break}return e.next=3,this.transport.send(P.WidgetApiToWidgetAction.SendToDevice,d(d({},t),{},{encrypted:r}));case 3:case"end":return e.stop()}}),e,this)}))),function(e,t){return g.apply(this,arguments)})}])&&f(t.prototype,o),Object.defineProperty(t,"prototype",{writable:!1}),A}();t.ClientWidgetApi=b})),s=t((function(e,t){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}Object.defineProperty(t,"__esModule",{value:!0}),t.WidgetApi=void 0;var n=u({});function i(){i=function(){return e};var e={},t=Object.prototype,n=t.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",s=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(x){c=function(e,t,r){return e[t]=r}}function l(e,t,r,n){var i=t&&t.prototype instanceof p?t:p,o=Object.create(i.prototype),a=new S(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return{value:void 0,done:!0}}for(r.method=i,r.arg=o;;){var a=r.delegate;if(a){var s=O(a,r);if(s){if(s===f)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=d(e,t,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===f)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(e,r,a),o}function d(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(x){return{type:"throw",arg:x}}}e.wrap=l;var f={};function p(){}function v(){}function h(){}var y={};c(y,a,(function(){return this}));var m=Object.getPrototypeOf,g=m&&m(m(j([])));g&&g!==t&&n.call(g,a)&&(y=g);var b=h.prototype=p.prototype=Object.create(y);function w(e){["next","throw","return"].forEach((function(t){c(e,t,(function(e){return this._invoke(t,e)}))}))}function _(e,t){var i;this._invoke=function(o,a){function s(){return new t((function(i,s){!function i(o,a,s,u){var c=d(e[o],e,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==r(f)&&n.call(f,"__await")?t.resolve(f.__await).then((function(e){i("next",e,s,u)}),(function(e){i("throw",e,s,u)})):t.resolve(f).then((function(e){l.value=e,s(l)}),(function(e){return i("throw",e,s,u)}))}u(c.arg)}(o,a,i,s)}))}return i=i?i.then(s,s):s()}}function O(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,O(e,t),"throw"===t.method))return f;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=d(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,f;var i=n.arg;return i?i.done?(t[e.resultName]=i.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,f):i:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,f)}function k(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(k,this),this.reset(!0)}function j(e){if(e){var t=e[a];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,i=function t(){for(;++r=0;--i){var o=this.tryEntries[i],a=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var s=n.call(o,"catchLoc"),u=n.call(o,"finallyLoc");if(s&&u){if(this.prev=0;--r){var i=this.tryEntries[r];if(i.tryLoc<=this.prev&&n.call(i,"finallyLoc")&&this.prev=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),f}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var i=n.arg;E(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:j(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},e}function o(e,t,r,n,i,o,a){try{var s=e[o](a),u=s.value}catch(c){return void r(c)}s.done?t(u):Promise.resolve(u).then(n,i)}function a(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var a=e.apply(t,r);function s(e){o(a,n,i,s,u,"next",e)}function u(e){o(a,n,i,s,u,"throw",e)}s(void 0)}))}}function s(e,t){for(var r=0;r0&&void 0!==arguments[0]?arguments[0]:null,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,w),(e=b.call(this)).clientOrigin=r,p(d(e),"transport",void 0),p(d(e),"capabilitiesFinished",!1),p(d(e),"supportsMSC2974Renegotiate",!1),p(d(e),"requestedCapabilities",[]),p(d(e),"approvedCapabilities",void 0),p(d(e),"cachedClientVersions",void 0),p(d(e),"turnServerWatchers",0),!window.parent)throw new Error("No parent window. This widget doesn't appear to be embedded properly.");return e.transport=new n.PostmessageTransport(E.WidgetApiDirection.FromWidget,t,window.parent,window),e.transport.targetOrigin=r,e.transport.on("message",e.handleMessage.bind(d(e))),e}return t=w,(o=[{key:"hasCapability",value:function(e){return Array.isArray(this.approvedCapabilities)?this.approvedCapabilities.includes(e):this.requestedCapabilities.includes(e)}},{key:"requestCapability",value:function(e){if(this.capabilitiesFinished&&!this.supportsMSC2974Renegotiate)throw new Error("Capabilities have already been negotiated");this.requestedCapabilities.push(e)}},{key:"requestCapabilities",value:function(e){var t=this;e.forEach((function(e){return t.requestCapability(e)}))}},{key:"requestCapabilityForRoomTimeline",value:function(e){this.requestCapability("org.matrix.msc2762.timeline:".concat(e))}},{key:"requestCapabilityToSendState",value:function(e,t){this.requestCapability(F.WidgetEventCapability.forStateEvent(F.EventDirection.Send,e,t).raw)}},{key:"requestCapabilityToReceiveState",value:function(e,t){this.requestCapability(F.WidgetEventCapability.forStateEvent(F.EventDirection.Receive,e,t).raw)}},{key:"requestCapabilityToSendToDevice",value:function(e){this.requestCapability(F.WidgetEventCapability.forToDeviceEvent(F.EventDirection.Send,e).raw)}},{key:"requestCapabilityToReceiveToDevice",value:function(e){this.requestCapability(F.WidgetEventCapability.forToDeviceEvent(F.EventDirection.Receive,e).raw)}},{key:"requestCapabilityToSendEvent",value:function(e){this.requestCapability(F.WidgetEventCapability.forRoomEvent(F.EventDirection.Send,e).raw)}},{key:"requestCapabilityToReceiveEvent",value:function(e){this.requestCapability(F.WidgetEventCapability.forRoomEvent(F.EventDirection.Receive,e).raw)}},{key:"requestCapabilityToSendMessage",value:function(e){this.requestCapability(F.WidgetEventCapability.forRoomMessageEvent(F.EventDirection.Send,e).raw)}},{key:"requestCapabilityToReceiveMessage",value:function(e){this.requestCapability(F.WidgetEventCapability.forRoomMessageEvent(F.EventDirection.Receive,e).raw)}},{key:"requestOpenIDConnectToken",value:function(){var e=this;return new Promise((function(t,r){e.transport.sendComplete(P.WidgetApiFromWidgetAction.GetOpenIDCredentials,{}).then((function(n){var i=n.response;i.state===T.OpenIDRequestState.Allowed?t(i):i.state===T.OpenIDRequestState.Blocked?r(new Error("User declined to verify their identity")):i.state===T.OpenIDRequestState.PendingUserConfirmation?e.on("action:".concat(P.WidgetApiToWidgetAction.OpenIDCredentials),(function o(a){a.preventDefault();var s=a.detail;s.data.original_request_id===n.requestId&&(s.data.state===T.OpenIDRequestState.Allowed?(t(s.data),e.transport.reply(s,{})):s.data.state===T.OpenIDRequestState.Blocked?(r(new Error("User declined to verify their identity")),e.transport.reply(s,{})):(r(new Error("Invalid state on reply: "+i.state)),e.transport.reply(s,{error:{message:"Invalid state"}})),e.off("action:".concat(P.WidgetApiToWidgetAction.OpenIDCredentials),o))})):r(new Error("Invalid state: "+i.state))})).catch(r)}))}},{key:"updateRequestedCapabilities",value:function(){return this.transport.send(P.WidgetApiFromWidgetAction.MSC2974RenegotiateCapabilities,{capabilities:this.requestedCapabilities}).then()}},{key:"sendContentLoaded",value:function(){return this.transport.send(P.WidgetApiFromWidgetAction.ContentLoaded,{}).then()}},{key:"sendSticker",value:function(e){return this.transport.send(P.WidgetApiFromWidgetAction.SendSticker,e).then()}},{key:"setAlwaysOnScreen",value:function(e){return this.transport.send(P.WidgetApiFromWidgetAction.UpdateAlwaysOnScreen,{value:e}).then((function(e){return e.success}))}},{key:"openModalWidget",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:I.MatrixWidgetType.Custom;return this.transport.send(P.WidgetApiFromWidgetAction.OpenModalWidget,{type:i,url:e,name:t,buttons:r,data:n}).then()}},{key:"closeModalWidget",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return this.transport.send(P.WidgetApiFromWidgetAction.CloseModalWidget,e).then()}},{key:"sendRoomEvent",value:function(e,t,r){return this.transport.send(P.WidgetApiFromWidgetAction.SendEvent,{type:e,content:t,room_id:r})}},{key:"sendStateEvent",value:function(e,t,r,n){return this.transport.send(P.WidgetApiFromWidgetAction.SendEvent,{type:e,content:r,state_key:t,room_id:n})}},{key:"sendToDevice",value:function(e,t,r){return this.transport.send(P.WidgetApiFromWidgetAction.SendToDevice,{type:e,encrypted:t,messages:r})}},{key:"readRoomEvents",value:function(e,t,r,n){var i={type:e,msgtype:r};return void 0!==t&&(i.limit=t),n&&(n.includes(K.Symbols.AnyRoom)?i.room_ids=K.Symbols.AnyRoom:i.room_ids=n),this.transport.send(P.WidgetApiFromWidgetAction.MSC2876ReadEvents,i).then((function(e){return e.events}))}},{key:"readEventRelations",value:(g=a(i().mark((function e(t,r,n,o,a,s,u,c){var l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getClientVersions();case 2:if(e.sent.includes(C.UnstableApiVersion.MSC3869)){e.next=5;break}throw new Error("The read_relations action is not supported by the client.");case 5:return l={event_id:t,rel_type:n,event_type:o,room_id:r,to:u,from:s,limit:a,direction:c},e.abrupt("return",this.transport.send(P.WidgetApiFromWidgetAction.MSC3869ReadRelations,l));case 7:case"end":return e.stop()}}),e,this)}))),function(e,t,r,n,i,o,a,s){return g.apply(this,arguments)})},{key:"readStateEvents",value:function(e,t,r,n){var i={type:e,state_key:void 0===r||r};return void 0!==t&&(i.limit=t),n&&(n.includes(K.Symbols.AnyRoom)?i.room_ids=K.Symbols.AnyRoom:i.room_ids=n),this.transport.send(P.WidgetApiFromWidgetAction.MSC2876ReadEvents,i).then((function(e){return e.events}))}},{key:"setModalButtonEnabled",value:function(e,t){if(e===D.BuiltInModalButtonID.Close)throw new Error("The close button cannot be disabled");return this.transport.send(P.WidgetApiFromWidgetAction.SetModalButtonEnabled,{button:e,enabled:t}).then()}},{key:"navigateTo",value:function(e){if(!e||!e.startsWith("https://matrix.to/#"))throw new Error("Invalid matrix.to URI");return this.transport.send(P.WidgetApiFromWidgetAction.MSC2931Navigate,{uri:e}).then()}},{key:"getTurnServers",value:function(){var e,t=this;return(e=i().mark((function e(){var r,n;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=function(){var e=a(i().mark((function e(n){return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n.preventDefault(),r(n.detail.data),e.next=4,t.transport.reply(n.detail,{});case 4:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),t.on("action:".concat(P.WidgetApiToWidgetAction.UpdateTurnServers),n),0!==t.turnServerWatchers){e.next=12;break}return e.prev=3,e.next=6,v(t.transport.send(P.WidgetApiFromWidgetAction.WatchTurnServers,{}));case 6:e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(3),t.off("action:".concat(P.WidgetApiToWidgetAction.UpdateTurnServers),n),e.t0;case 12:t.turnServerWatchers++,e.prev=13;case 14:return e.next=17,v(new Promise((function(e){return r=e})));case 17:return e.next=19,e.sent;case 19:e.next=14;break;case 21:if(e.prev=21,t.off("action:".concat(P.WidgetApiToWidgetAction.UpdateTurnServers),n),t.turnServerWatchers--,0!==t.turnServerWatchers){e.next=27;break}return e.next=27,v(t.transport.send(P.WidgetApiFromWidgetAction.UnwatchTurnServers,{}));case 27:return e.finish(21);case 28:case"end":return e.stop()}}),e,null,[[3,8],[13,,21,28]])})),function(){return new h(e.apply(this,arguments))})()}},{key:"searchUserDirectory",value:(m=a(i().mark((function e(t,r){var n;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getClientVersions();case 2:if(e.sent.includes(C.UnstableApiVersion.MSC3973)){e.next=5;break}throw new Error("The user_directory_search action is not supported by the client.");case 5:return n={search_term:t,limit:r},e.abrupt("return",this.transport.send(P.WidgetApiFromWidgetAction.MSC3973UserDirectorySearch,n));case 7:case"end":return e.stop()}}),e,this)}))),function(e,t){return m.apply(this,arguments)})},{key:"start",value:function(){var e=this;this.transport.start(),this.getClientVersions().then((function(t){t.includes(C.UnstableApiVersion.MSC2974)&&(e.supportsMSC2974Renegotiate=!0)}))}},{key:"handleMessage",value:function(e){var t=new CustomEvent("action:".concat(e.detail.action),{detail:e.detail,cancelable:!0});if(this.emit("action:".concat(e.detail.action),t),!t.defaultPrevented)switch(e.detail.action){case P.WidgetApiToWidgetAction.SupportedApiVersions:return this.replyVersions(e.detail);case P.WidgetApiToWidgetAction.Capabilities:return this.handleCapabilities(e.detail);case P.WidgetApiToWidgetAction.UpdateVisibility:case P.WidgetApiToWidgetAction.NotifyCapabilities:return this.transport.reply(e.detail,{});default:return this.transport.reply(e.detail,{error:{message:"Unknown or unsupported action: "+e.detail.action}})}}},{key:"replyVersions",value:function(e){this.transport.reply(e,{supported_versions:C.CurrentApiVersions})}},{key:"getClientVersions",value:function(){var e=this;return Array.isArray(this.cachedClientVersions)?Promise.resolve(this.cachedClientVersions):this.transport.send(P.WidgetApiFromWidgetAction.SupportedApiVersions,{}).then((function(t){return e.cachedClientVersions=t.supported_versions,t.supported_versions})).catch((function(e){return console.warn("non-fatal error getting supported client versions: ",e),[]}))}},{key:"handleCapabilities",value:function(e){var t=this;return this.capabilitiesFinished?this.transport.reply(e,{error:{message:"Capability negotiation already completed"}}):this.getClientVersions().then((function(r){return r.includes(C.UnstableApiVersion.MSC2871)?t.once("action:".concat(P.WidgetApiToWidgetAction.NotifyCapabilities),(function(e){t.approvedCapabilities=e.detail.data.approved,t.emit("ready")})):t.emit("ready"),t.capabilitiesFinished=!0,t.transport.reply(e,{capabilities:t.requestedCapabilities})}))}}])&&s(t.prototype,o),Object.defineProperty(t,"prototype",{writable:!1}),w}();t.WidgetApi=m})),u=t((function(e,t){"use strict";function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}Object.defineProperty(t,"__esModule",{value:!0}),t.PostmessageTransport=void 0;var i=r({});function o(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function a(e){for(var t=1;t0&&a.length>i&&!a.warned){a.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+String(t)+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=e,u.type=t,u.count=a.length,s=u,console&&console.warn&&console.warn(s)}return e}function g(e,t,r){var n={fired:!1,wrapFn:void 0,target:e,type:t,listener:r},i=function(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,0===arguments.length?this.listener.call(this.target):this.listener.apply(this.target,arguments)}.bind(n);return i.listener=r,n.wrapFn=i,i}function b(e,t,r){var n=e._events;if(void 0===n)return[];var i=n[t];return void 0===i?[]:"function"==typeof i?r?[i.listener||i]:[i]:r?function(e){for(var t=new Array(e.length),r=0;r0&&(o=t[0]),o instanceof Error)throw o;var a=new Error("Unhandled error."+(o?" ("+o.message+")":""));throw a.context=o,a}var s=i[e];if(void 0===s)return!1;if("function"==typeof s)d(s,this,t);else{var u=s.length,c=_(s,u);for(r=0;r=0;o--)if(r[o]===t||r[o].listener===t){a=r[o].listener,i=o;break}if(i<0)return this;0===i?r.shift():function(e,t){for(;t+1=0;n--)this.removeListener(e,t[n]);return this},p.prototype.listeners=function(e){return b(this,e,!0)},p.prototype.rawListeners=function(e){return b(this,e,!1)},p.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):w.call(e,t)},p.prototype.listenerCount=w,p.prototype.eventNames=function(){return this._eventsCount>0?e(this._events):[]};var k,E={};Object.defineProperty(E,"__esModule",{value:!0}),E.WidgetApiDirection=void 0,E.invertedDirection=function(e){if(e===k.ToWidget)return k.FromWidget;if(e===k.FromWidget)return k.ToWidget;throw new Error("Invalid direction")},E.WidgetApiDirection=k,function(e){e.ToWidget="toWidget",e.FromWidget="fromWidget"}(k||(E.WidgetApiDirection=k={}));var S,j,C={};Object.defineProperty(C,"__esModule",{value:!0}),C.UnstableApiVersion=C.MatrixApiVersion=C.CurrentApiVersions=void 0,C.MatrixApiVersion=S,function(e){e.Prerelease1="0.0.1",e.Prerelease2="0.0.2"}(S||(C.MatrixApiVersion=S={})),C.UnstableApiVersion=j,function(e){e.MSC2762="org.matrix.msc2762",e.MSC2871="org.matrix.msc2871",e.MSC2931="org.matrix.msc2931",e.MSC2974="org.matrix.msc2974",e.MSC2876="org.matrix.msc2876",e.MSC3819="org.matrix.msc3819",e.MSC3846="town.robin.msc3846",e.MSC3869="org.matrix.msc3869",e.MSC3973="org.matrix.msc3973"}(j||(C.UnstableApiVersion=j={}));var x=[S.Prerelease1,S.Prerelease2,j.MSC2762,j.MSC2871,j.MSC2931,j.MSC2974,j.MSC2876,j.MSC3819,j.MSC3846,j.MSC3869,j.MSC3973];C.CurrentApiVersions=x;var A,W,P={};Object.defineProperty(P,"__esModule",{value:!0}),P.WidgetApiToWidgetAction=P.WidgetApiFromWidgetAction=void 0,P.WidgetApiToWidgetAction=A,function(e){e.SupportedApiVersions="supported_api_versions",e.Capabilities="capabilities",e.NotifyCapabilities="notify_capabilities",e.TakeScreenshot="screenshot",e.UpdateVisibility="visibility",e.OpenIDCredentials="openid_credentials",e.WidgetConfig="widget_config",e.CloseModalWidget="close_modal",e.ButtonClicked="button_clicked",e.SendEvent="send_event",e.SendToDevice="send_to_device",e.UpdateTurnServers="update_turn_servers"}(A||(P.WidgetApiToWidgetAction=A={})),P.WidgetApiFromWidgetAction=W,function(e){e.SupportedApiVersions="supported_api_versions",e.ContentLoaded="content_loaded",e.SendSticker="m.sticker",e.UpdateAlwaysOnScreen="set_always_on_screen",e.GetOpenIDCredentials="get_openid",e.CloseModalWidget="close_modal",e.OpenModalWidget="open_modal",e.SetModalButtonEnabled="set_button_enabled",e.SendEvent="send_event",e.SendToDevice="send_to_device",e.WatchTurnServers="watch_turn_servers",e.UnwatchTurnServers="unwatch_turn_servers",e.MSC2876ReadEvents="org.matrix.msc2876.read_events",e.MSC2931Navigate="org.matrix.msc2931.navigate",e.MSC2974RenegotiateCapabilities="org.matrix.msc2974.request_capabilities",e.MSC3869ReadRelations="org.matrix.msc3869.read_relations",e.MSC3973UserDirectorySearch="org.matrix.msc3973.user_directory_search"}(W||(P.WidgetApiFromWidgetAction=W={}));var M,T={};Object.defineProperty(T,"__esModule",{value:!0}),T.OpenIDRequestState=void 0,T.OpenIDRequestState=M,function(e){e.Allowed="allowed",e.Blocked="blocked",e.PendingUserConfirmation="request"}(M||(T.OpenIDRequestState=M={}));var R,I={};Object.defineProperty(I,"__esModule",{value:!0}),I.MatrixWidgetType=void 0,I.MatrixWidgetType=R,function(e){e.Custom="m.custom",e.JitsiMeet="m.jitsi",e.Stickerpicker="m.stickerpicker"}(R||(I.MatrixWidgetType=R={}));var L,D={};Object.defineProperty(D,"__esModule",{value:!0}),D.BuiltInModalButtonID=void 0,D.BuiltInModalButtonID=L,function(e){e.Close="m.close"}(L||(D.BuiltInModalButtonID=L={}));var U,q,F={};function N(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){s=!0,o=e},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw o}}}}(t);try{for(i.s();!(r=i.n()).done;){var o=r.value,a=null,s=void 0,u=null;if(o.startsWith("org.matrix.msc2762.send.event:")?(a=q.Send,u=U.Event,s=o.substring("org.matrix.msc2762.send.event:".length)):o.startsWith("org.matrix.msc2762.send.state_event:")?(a=q.Send,u=U.State,s=o.substring("org.matrix.msc2762.send.state_event:".length)):o.startsWith("org.matrix.msc3819.send.to_device:")?(a=q.Send,u=U.ToDevice,s=o.substring("org.matrix.msc3819.send.to_device:".length)):o.startsWith("org.matrix.msc2762.receive.event:")?(a=q.Receive,u=U.Event,s=o.substring("org.matrix.msc2762.receive.event:".length)):o.startsWith("org.matrix.msc2762.receive.state_event:")?(a=q.Receive,u=U.State,s=o.substring("org.matrix.msc2762.receive.state_event:".length)):o.startsWith("org.matrix.msc3819.receive.to_device:")&&(a=q.Receive,u=U.ToDevice,s=o.substring("org.matrix.msc3819.receive.to_device:".length)),null!==a&&null!==u&&void 0!==s){var c=s.startsWith("m.room.message#")||u===U.State,l=null;if(s.includes("#")&&c){var d=s.split("#"),f=d.findIndex((function(e){return!e.endsWith("\\")}));s=d.slice(0,f+1).map((function(e){return e.endsWith("\\")?e.substring(0,e.length-1):e})).join("#"),l=d.slice(f+1).join("#")}n.push(new e(a,s,u,l,o))}}}catch(p){i.e(p)}finally{i.f()}return n}}],(r=[{key:"matchesAsStateEvent",value:function(e,t,r){return this.kind===U.State&&this.direction===e&&this.eventType===t&&(null===this.keyStr||this.keyStr===r)}},{key:"matchesAsToDeviceEvent",value:function(e,t){return this.kind===U.ToDevice&&this.direction===e&&this.eventType===t}},{key:"matchesAsRoomEvent",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;return this.kind===U.Event&&this.direction===e&&this.eventType===t&&("m.room.message"!==this.eventType||null===this.keyStr||this.keyStr===r)}}])&&V(t.prototype,r),n&&V(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();F.WidgetEventCapability=B;var G,K={};Object.defineProperty(K,"__esModule",{value:!0}),K.Symbols=void 0,K.Symbols=G,function(e){e.AnyRoom="*"}(G||(K.Symbols=G={}));var $,Y={};Object.defineProperty(Y,"__esModule",{value:!0}),Y.VideoConferenceCapabilities=Y.StickerpickerCapabilities=Y.MatrixCapabilities=void 0,Y.getTimelineRoomIDFromCapability=function(e){return e.substring(e.indexOf(":")+1)},Y.isTimelineCapability=function(e){return null==e?void 0:e.startsWith("org.matrix.msc2762.timeline:")},Y.isTimelineCapabilityFor=function(e,t){return e==="org.matrix.msc2762.timeline:".concat(t)},Y.MatrixCapabilities=$,function(e){e.Screenshots="m.capability.screenshot",e.StickerSending="m.sticker",e.AlwaysOnScreen="m.always_on_screen",e.RequiresClient="io.element.requires_client",e.MSC2931Navigate="org.matrix.msc2931.navigate",e.MSC3846TurnServers="town.robin.msc3846.turn_servers",e.MSC3973UserDirectorySearch="org.matrix.msc3973.user_directory_search"}($||(Y.MatrixCapabilities=$={}));var z=[$.StickerSending];Y.StickerpickerCapabilities=z;var H=[$.AlwaysOnScreen];Y.VideoConferenceCapabilities=H;var J={};function Q(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){s=!0,o=e},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw o}}}}(this.listeners);try{for(r.s();!(t=r.n()).done;)(0,t.value)(e)}catch(n){r.e(n)}finally{r.f()}}},{key:"close",value:function(){this.listeners=[]}}])&&X(t.prototype,r),Object.defineProperty(t,"prototype",{writable:!1}),e}();J.SimpleObservable=Z;var ee={};Object.defineProperty(ee,"__esModule",{value:!0});var te={};Object.defineProperty(te,"__esModule",{value:!0});var re={};Object.defineProperty(re,"__esModule",{value:!0});var ne={};Object.defineProperty(ne,"__esModule",{value:!0});var ie={};Object.defineProperty(ie,"__esModule",{value:!0});var oe={};Object.defineProperty(oe,"__esModule",{value:!0}),oe.isErrorResponse=function(e){return"error"in e&&!!e.error.message};var ae={};Object.defineProperty(ae,"__esModule",{value:!0});var se={};Object.defineProperty(se,"__esModule",{value:!0});var ue={};Object.defineProperty(ue,"__esModule",{value:!0});var ce={};Object.defineProperty(ce,"__esModule",{value:!0});var le={};Object.defineProperty(le,"__esModule",{value:!0});var de={};Object.defineProperty(de,"__esModule",{value:!0});var fe={};Object.defineProperty(fe,"__esModule",{value:!0});var pe={};Object.defineProperty(pe,"__esModule",{value:!0});var ve={};Object.defineProperty(ve,"__esModule",{value:!0});var he={};Object.defineProperty(he,"__esModule",{value:!0});var ye,me={};Object.defineProperty(me,"__esModule",{value:!0}),me.WidgetKind=void 0,me.WidgetKind=ye,function(e){e.Room="room",e.Account="account",e.Modal="modal"}(ye||(me.WidgetKind=ye={}));var ge,be={};Object.defineProperty(be,"__esModule",{value:!0}),be.ModalButtonKind=void 0,be.ModalButtonKind=ge,function(e){e.Primary="m.primary",e.Secondary="m.secondary",e.Warning="m.warning",e.Danger="m.danger",e.Link="m.link"}(ge||(be.ModalButtonKind=ge={}));var we={};Object.defineProperty(we,"__esModule",{value:!0});var _e={};Object.defineProperty(_e,"__esModule",{value:!0});var Oe={};Object.defineProperty(Oe,"__esModule",{value:!0});var ke={};Object.defineProperty(ke,"__esModule",{value:!0});var Ee={};Object.defineProperty(Ee,"__esModule",{value:!0});var Se={};Object.defineProperty(Se,"__esModule",{value:!0});var je={};Object.defineProperty(je,"__esModule",{value:!0});var Ce={};Object.defineProperty(Ce,"__esModule",{value:!0});var xe={};Object.defineProperty(xe,"__esModule",{value:!0});var Ae={};Object.defineProperty(Ae,"__esModule",{value:!0}),Ae.isValidUrl=function(e){if(!e)return!1;try{var t=new URL(e);return"http"===t.protocol||"https"===t.protocol}catch(r){if(r instanceof TypeError)return!1;throw r}};var We={};Object.defineProperty(We,"__esModule",{value:!0}),We.assertPresent=function(e,t){if(!e[t])throw new Error("".concat(t," is required"))};var Pe={};function Me(e){return null==e?"".concat(e):String(e)}return Object.defineProperty(Pe,"__esModule",{value:!0}),Pe.runTemplate=function(e,t,r){for(var n=Object.assign({},t.data,{matrix_room_id:r.widgetRoomId||"",matrix_user_id:r.currentUserId,matrix_display_name:r.userDisplayName||r.currentUserId,matrix_avatar_url:r.userHttpAvatarUrl||"",matrix_widget_id:t.id,"org.matrix.msc2873.client_id":r.clientId||"","org.matrix.msc2873.client_theme":r.clientTheme||"","org.matrix.msc2873.client_language":r.clientLanguage||""}),i=e,o=0,a=Object.keys(n);o