var __awaiter=this&&this.__awaiter||function(t,e,s,n){return new(s||(s=Promise))(function(i,r){function o(t){try{u(n.next(t))}catch(t){r(t)}}function c(t){try{u(n.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?i(t.value):((e=t.value)instanceof s?e:new s(function(t){t(e)})).then(o,c)}u((n=n.apply(t,e||[])).next())})};import{EventProviderStatus as t}from"./event";import{objectsEqual as e}from"./util";import{Util as s}from".";export const subscribe=n=>i=>class extends i{constructor(){super(...arguments),this._connectRequested=!1,this._status=t.DISCONNECTED,this._statusCallback=t=>this._onEventProviderStatusChanged(t)}connectedCallback(){super.connectedCallback&&super.connectedCallback(),this.connectEvents()}disconnectedCallback(){this.disconnectEvents(),super.disconnectedCallback&&super.disconnectedCallback()}connectEvents(){n.getEventProvider()?this._connectRequested||(this._connectRequested=!0,n.getEventProvider().subscribeStatusChange(this._statusCallback),this._doConnect()):console.log("No event provider available so cannot subscribe")}disconnectEvents(){this._connectRequested&&(this._connectRequested=!1,n.getEventProvider().unsubscribeStatusChange(this._statusCallback),this._onEventsDisconnect())}_doConnect(){return __awaiter(this,void 0,void 0,function*(){this.eventsConnected&&this._onEventsConnect()})}get eventsConnected(){return this._connectRequested&&n.getEventProvider().status===t.CONNECTED}_onEventProviderStatusChanged(e){switch(e){case t.DISCONNECTED:this._onEventsDisconnect();break;case t.CONNECTED:this._doConnect()}}_onEventsConnect(){this._addEventSubscriptions(),this.onEventsConnect()}_onEventsDisconnect(){this._removeEventSubscriptions(),this.onEventsDisconnect()}_addEventSubscriptions(){return __awaiter(this,void 0,void 0,function*(){let t=!!this._attributeRefs,e=!!this._assetIds,i=this._attributeRefs?this._attributeRefs:this._assetIds;if(i&&i.length>0){if(this._subscriptionIds=[],e){let t=yield n.getEventProvider().subscribeAssetEvents(i,!0,t=>this._onEvent(t)),e=this._attributeRefs?this._attributeRefs:this._assetIds;if(!this._subscriptionIds||!s.objectsEqual(i,e))return void n.getEventProvider().unsubscribe(t);this._subscriptionIds.push(t)}let r=yield n.getEventProvider().subscribeAttributeEvents(i,t,t=>this._onEvent(t)),o=this._attributeRefs?this._attributeRefs:this._assetIds;if(!this._subscriptionIds||!s.objectsEqual(i,o))return void n.getEventProvider().unsubscribe(r);this._subscriptionIds.push(r)}})}_removeEventSubscriptions(){this._subscriptionIds&&(this._subscriptionIds.forEach(t=>{n.getEventProvider().unsubscribe(t)}),this._subscriptionIds=void 0)}_refreshEventSubscriptions(){this._removeEventSubscriptions(),this._addEventSubscriptions()}set assetIds(t){e(this._assetIds,t)||(this._assetIds=t,this._refreshEventSubscriptions(),this.requestUpdate("assetIds"))}get assetIds(){return this._assetIds}set attributeRefs(t){e(this._attributeRefs,t)||(this._attributeRefs=t,this._refreshEventSubscriptions())}get attributeRefs(){return this._attributeRefs}_sendEvent(t){n.getEventProvider().sendEvent(t)}_sendEventWithReply(t){return n.getEventProvider().sendEventWithReply(t)}onEventsConnect(){}onEventsDisconnect(){}_onEvent(t){}};