(()=>{"use strict";(()=>{const e=document.getElementById("viewers"),i=document.getElementById("viewer-template");let t;function r(){window.popupWindowApi.onMessage(a),e.addEventListener("keydown",s,{capture:!0}),c("initialized")}const s=i=>{if(!0===i.ctrlKey||!0===i.altKey||!0===i.metaKey)return;if("Tab"===i.code)return c("focusChange",i.shiftKey?"previous":"next"),void i.preventDefault();if(!t)return;if(!1===i.shiftKey){switch(i.key){case"ArrowUp":n("previousElementSibling",1);break;case"ArrowDown":n("nextElementSibling",1);break;case"PageUp":n("previousElementSibling",4);break;case"PageDown":n("nextElementSibling",4);break;case"Home":e.firstElementChild.focus();break;case"End":e.lastElementChild.focus();break;case"Escape":c("hideViewersList");break;case" ":case"Enter":t.click();break;default:return}i.preventDefault()}},n=(e,i)=>{let r=t;for(let t=0;t!==i&&r[e];++t)r=r[e];r.focus()},a=(e,...i)=>{console.debug(`[ScreenSharingViewersList] Received message - name: ${e} args: ${JSON.stringify(i)}`);const t={viewersListModified:l,avatarAvailable:o};t.hasOwnProperty(e)?t[e](...i):console.error(`[ScreenSharingViewersList] Unhandled message: ${e}`)},o=(i,t)=>{const r=e.childNodes;for(let e=0;e!==r.length;++e){const s=r[e];if(s.nodeType===s.ELEMENT_NODE){const s=r[e];if(i===s.id){s.querySelector("img.viewer-avatar").src=t}}}},l=(r,s)=>{(()=>{for(;e.firstChild;)e.removeChild(e.firstChild)})(),(r=>{for(let s=0;s!==r.length;++s){const n=r[s],a=i.cloneNode(!0);a.removeAttribute("id");const o=a.querySelector("img.viewer-avatar"),l=a.querySelector("div.viewer-name");o.src=n.fallbackAvatarUri,o.setAttribute("aria-hidden","true"),l.innerText=n.displayName,l.title=n.displayName,a.id=n.username,a.addEventListener("click",(()=>{c("grantControl",n.username)})),a.addEventListener("focus",(()=>{a.classList.add("focused"),t&&t.classList.remove("focused"),t=a})),e.appendChild(a)}})(r),(i=>{console.debug(`[ScreenSharingViewersList] Resizing to ${i}px high`),e.setAttribute("style",`height: ${i}px`)})(s),window.scrollTo(0,0),e.firstElementChild.focus()},c=(e,...i)=>{console.debug(`[ScreenSharingViewersList] Sending message - name: ${e} args: ${JSON.stringify(i)}`),window.popupWindowApi.sendMessage(e,...i)};r()})()})();