import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{MediaModel}from"./MediaModel.js";import{PlayerDetailView}from"./PlayerDetailView.js";import{PlayerListView}from"./PlayerListView.js";class PlayerDataCollection{constructor(){this._properties=new Map,this._messages=[],this._events=[],this._errors=[]}onProperty(e){this._properties.set(e.name,e.value)}onError(e){this._errors.push(e)}onMessage(e){this._messages.push(e)}onEvent(e){this._events.push(e)}export(){return{properties:this._properties,messages:this._messages,events:this._events,errors:this._errors}}}class PlayerDataDownloadManager{constructor(){this._playerDataCollection=new Map}addPlayer(e){this._playerDataCollection.set(e,new PlayerDataCollection)}onProperty(e,t){const s=this._playerDataCollection.get(e);s&&s.onProperty(t)}onError(e,t){const s=this._playerDataCollection.get(e);s&&s.onError(t)}onMessage(e,t){const s=this._playerDataCollection.get(e);s&&s.onMessage(t)}onEvent(e,t){const s=this._playerDataCollection.get(e);s&&s.onEvent(t)}exportPlayerData(e){const t=this._playerDataCollection.get(e);if(!t)throw new Error("Unable to find player");return t.export()}deletePlayer(e){this._playerDataCollection.delete(e)}}let mainViewInstance;export class MainView extends UI.Panel.PanelWithSidebar{constructor(){super("Media"),this._detailPanels=new Map,this._deletedPlayers=new Set,this._downloadStore=new PlayerDataDownloadManager,this._sidebar=new PlayerListView(this),this._sidebar.show(this.panelSidebarElement()),SDK.SDKModel.TargetManager.instance().observeModels(MediaModel,this)}static instance(e={forceNew:null}){const{forceNew:t}=e;return mainViewInstance&&!t||(mainViewInstance=new MainView),mainViewInstance}renderMainPanel(e){if(!this._detailPanels.has(e))return;const t=this.splitWidget().mainWidget();t&&t.detachChildWidgets(),this._detailPanels.get(e)?.show(this.mainElement())}wasShown(){super.wasShown();for(const e of SDK.SDKModel.TargetManager.instance().models(MediaModel))this._addEventListeners(e)}willHide(){for(const e of SDK.SDKModel.TargetManager.instance().models(MediaModel))this._removeEventListeners(e)}modelAdded(e){this.isShowing()&&this._addEventListeners(e)}modelRemoved(e){this._removeEventListeners(e)}_addEventListeners(e){e.ensureEnabled(),e.addEventListener("PlayerPropertiesChanged",this._propertiesChanged,this),e.addEventListener("PlayerEventsAdded",this._eventsAdded,this),e.addEventListener("PlayerMessagesLogged",this._messagesLogged,this),e.addEventListener("PlayerErrorsRaised",this._errorsRaised,this),e.addEventListener("PlayersCreated",this._playersCreated,this)}_removeEventListeners(e){e.removeEventListener("PlayerPropertiesChanged",this._propertiesChanged,this),e.removeEventListener("PlayerEventsAdded",this._eventsAdded,this),e.removeEventListener("PlayerMessagesLogged",this._messagesLogged,this),e.removeEventListener("PlayerErrorsRaised",this._errorsRaised,this),e.removeEventListener("PlayersCreated",this._playersCreated,this)}_onPlayerCreated(e){this._sidebar.addMediaElementItem(e),this._detailPanels.set(e,new PlayerDetailView),this._downloadStore.addPlayer(e)}_propertiesChanged(e){for(const t of e.data.properties)this.onProperty(e.data.playerId,t)}_eventsAdded(e){for(const t of e.data.events)this.onEvent(e.data.playerId,t)}_messagesLogged(e){for(const t of e.data.messages)this.onMessage(e.data.playerId,t)}_errorsRaised(e){for(const t of e.data.errors)this.onError(e.data.playerId,t)}_shouldPropagate(e){return!this._deletedPlayers.has(e)&&this._detailPanels.has(e)}onProperty(e,t){this._shouldPropagate(e)&&(this._sidebar.onProperty(e,t),this._downloadStore.onProperty(e,t),this._detailPanels.get(e)?.onProperty(t))}onError(e,t){this._shouldPropagate(e)&&(this._sidebar.onError(e,t),this._downloadStore.onError(e,t),this._detailPanels.get(e)?.onError(t))}onMessage(e,t){this._shouldPropagate(e)&&(this._sidebar.onMessage(e,t),this._downloadStore.onMessage(e,t),this._detailPanels.get(e)?.onMessage(t))}onEvent(e,t){this._shouldPropagate(e)&&(this._sidebar.onEvent(e,t),this._downloadStore.onEvent(e,t),this._detailPanels.get(e)?.onEvent(t))}_playersCreated(e){const t=e.data;for(const s of t)this._onPlayerCreated(s)}markPlayerForDeletion(e){this._deletedPlayers.add(e),this._detailPanels.delete(e),this._sidebar.deletePlayer(e),this._downloadStore.deletePlayer(e)}markOtherPlayersForDeletion(e){for(const t of this._detailPanels.keys())t!==e&&this.markPlayerForDeletion(t)}exportPlayerData(e){const t=this._downloadStore.exportPlayerData(e),s="data:application/octet-stream,"+encodeURIComponent(JSON.stringify(t,null,2)),a=document.createElement("a");a.href=s,a.download=e+".json",a.click()}}