import*as i18n from"../../core/i18n/i18n.js";import*as UI from"../../ui/legacy/legacy.js";import playerListViewStyles from"./playerListView.css.js";const UIStrings={hidePlayer:"Hide player",hideAllOthers:"Hide all others",savePlayerInfo:"Save player info",players:"Players"},str_=i18n.i18n.registerUIStrings("panels/media/PlayerListView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class PlayerListView extends UI.Widget.VBox{playerEntryFragments;playerEntriesWithHostnameFrameTitle;mainContainer;currentlySelectedEntry;constructor(e){super(!0),this.playerEntryFragments=new Map,this.playerEntriesWithHostnameFrameTitle=new Set,this.mainContainer=e,this.currentlySelectedEntry=null,this.contentElement.createChild("div","player-entry-header").textContent=i18nString(UIStrings.players)}createPlayerListEntry(e){const t=UI.Fragment.Fragment.build`
    <div class="player-entry-row hbox">
    <div class="player-entry-status-icon vbox">
    <div $="icon" class="player-entry-status-icon-centering"></div>
    </div>
    <div $="frame-title" class="player-entry-frame-title">FrameTitle</div>
    <div $="player-title" class="player-entry-player-title">PlayerTitle</div>
    </div>
    `,n=t.element();return n.addEventListener("click",this.selectPlayer.bind(this,e,n)),n.addEventListener("contextmenu",this.rightClickPlayer.bind(this,e)),t.$("icon").appendChild(UI.Icon.Icon.create("pause","media-player")),t}selectPlayer(e,t){this.mainContainer.renderMainPanel(e),null!==this.currentlySelectedEntry&&(this.currentlySelectedEntry.classList.remove("selected"),this.currentlySelectedEntry.classList.remove("force-white-icons")),t.classList.add("selected"),t.classList.add("force-white-icons"),this.currentlySelectedEntry=t}rightClickPlayer(e,t){const n=new UI.ContextMenu.ContextMenu(t);return n.headerSection().appendItem(i18nString(UIStrings.hidePlayer),this.mainContainer.markPlayerForDeletion.bind(this.mainContainer,e)),n.headerSection().appendItem(i18nString(UIStrings.hideAllOthers),this.mainContainer.markOtherPlayersForDeletion.bind(this.mainContainer,e)),n.headerSection().appendItem(i18nString(UIStrings.savePlayerInfo),this.mainContainer.exportPlayerData.bind(this.mainContainer,e)),n.show(),!0}setMediaElementFrameTitle(e,t,n){if(this.playerEntriesWithHostnameFrameTitle.has(e))n||this.playerEntriesWithHostnameFrameTitle.delete(e);else if(n)return;if(!this.playerEntryFragments.has(e))return;const r=this.playerEntryFragments.get(e);void 0!==r&&void 0!==r.element()&&(r.$("frame-title").textContent=t)}setMediaElementPlayerTitle(e,t){if(!this.playerEntryFragments.has(e))return;const n=this.playerEntryFragments.get(e);void 0!==n&&(n.$("player-title").textContent=t)}setMediaElementPlayerIcon(e,t){if(!this.playerEntryFragments.has(e))return;const n=this.playerEntryFragments.get(e);if(void 0===n)return;const r=n.$("icon");void 0!==r&&(r.textContent="",r.appendChild(UI.Icon.Icon.create(t,"media-player")))}formatAndEvaluate(e,t,n,r,i){n.length<=r||(n.length>=i&&(n=n.substring(0,i-3)+"..."),t.bind(this)(e,n))}addMediaElementItem(e){const t=this.createPlayerListEntry(e);this.contentElement.appendChild(t.element()),this.playerEntryFragments.set(e,t),this.playerEntriesWithHostnameFrameTitle.add(e)}deletePlayer(e){if(!this.playerEntryFragments.has(e))return;const t=this.playerEntryFragments.get(e);void 0!==t&&void 0!==t.element()&&(this.contentElement.removeChild(t.element()),this.playerEntryFragments.delete(e))}onEvent(e,t){const n=JSON.parse(t.value),r=n.event;if("kLoad"!==r)"kPlay"!==r?"kPause"!==r&&"kEnded"!==r?"kWebMediaPlayerDestroyed"!==r||this.setMediaElementPlayerIcon(e,"cross"):this.setMediaElementPlayerIcon(e,"pause"):this.setMediaElementPlayerIcon(e,"play");else{const t=n.url,r=t.substring(t.lastIndexOf("/")+1);this.formatAndEvaluate(e,this.setMediaElementPlayerTitle,r,1,20)}}onProperty(e,t){if("kFrameUrl"!==t.name)"kFrameTitle"===t.name&&t.value&&this.formatAndEvaluate(e,this.setMediaElementFrameTitle,t.value,1,20);else{const n=new URL(t.value).hostname;this.formatAndEvaluate(e,this.setMediaElementFrameTitle,n,1,20)}}onError(e,t){}onMessage(e,t){}wasShown(){super.wasShown(),this.registerCSSFiles([playerListViewStyles])}}