var e=class{errorPanel=null;currentErrorIcon=null;clickOutsideHandler=null;repositionHandler=null;errorHandlers=new WeakMap;errorDetails=new WeakMap;cssLoaded=!1;constructor(){document.addEventListener(`omni-icon:error`,this.handleErrorEvent),document.addEventListener(`omni-icon:loaded`,this.handleLoadedEvent)}async loadCSS(){if(this.cssLoaded)return;this.cssLoaded=!0;let e=document.createElement(`link`);e.rel=`stylesheet`,e.href=new URL(``+new URL(`ErrorObserver-DlFfVRM7.css`,import.meta.url).href,``+import.meta.url).href,document.head.appendChild(e)}handleErrorEvent=e=>{let t=e.detail;t?.element&&this.setupErrorIcon(t)};handleLoadedEvent=e=>{let t=e.detail;!t?.element||!t.wasError||this.cleanupErrorIcon(t.element)};setupErrorIcon(e){let t=e.element;if(this.errorDetails.set(t,e),this.errorHandlers.has(t))return;this.loadCSS(),t.style.cursor=`pointer`,t.setAttribute(`role`,`button`),t.setAttribute(`aria-expanded`,`false`),t.setAttribute(`tabindex`,`0`);let n=n=>{if(n.stopPropagation(),this.currentErrorIcon===t){this.hideErrorPanel();return}let r=this.errorDetails.get(t)??e;this.showErrorPanel(t,r)},r=e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),n(e))};t.addEventListener(`click`,n),t.addEventListener(`keydown`,r),this.errorHandlers.set(t,{clickHandler:n,keyHandler:r})}showErrorPanel(e,t){this.errorPanel||(this.errorPanel=document.createElement(`div`),this.errorPanel.className=`oiwc-error-panel`,document.body.appendChild(this.errorPanel)),this.errorPanel.innerHTML=`
			<div class="oiwc-error-panel__header">
				<span class="oiwc-error-panel__title">Omni Icon - ${t.type}</span>
				<button class="oiwc-error-panel__close" aria-label="Close error message">×</button>
			</div>
			<p class="oiwc-error-panel__meta">
				<strong>Icon:</strong> <code>${t.iconName}</code>
			</p>
			<p class="oiwc-error-panel__message">${t.message}</p>
		`,this.currentErrorIcon?.setAttribute(`aria-expanded`,`false`),this.currentErrorIcon?.removeAttribute(`data-oiwc-expanded`),this.currentErrorIcon=e,e.setAttribute(`aria-expanded`,`true`),e.setAttribute(`data-oiwc-expanded`,`true`),this.errorPanel.style.display=`block`,this.errorPanel.classList.remove(`oiwc-error-panel--visible`),this.positionPanel(),requestAnimationFrame(()=>{this.errorPanel?.classList.add(`oiwc-error-panel--visible`)}),this.errorPanel.querySelector(`.oiwc-error-panel__close`)?.addEventListener(`click`,e=>{e.stopPropagation(),this.hideErrorPanel()},{once:!0}),this.bindGlobalHandlers()}hideErrorPanel(){this.errorPanel&&(this.currentErrorIcon&&=(this.currentErrorIcon.setAttribute(`aria-expanded`,`false`),this.currentErrorIcon.removeAttribute(`data-oiwc-expanded`),null),this.errorPanel.classList.remove(`oiwc-error-panel--visible`),this.errorPanel.style.display=`none`,this.unbindGlobalHandlers())}bindGlobalHandlers(){this.unbindGlobalHandlers(),this.clickOutsideHandler=e=>{if(!this.currentErrorIcon||!this.errorPanel)return;let t=e.composedPath();!t.includes(this.currentErrorIcon)&&!t.includes(this.errorPanel)&&this.hideErrorPanel()},setTimeout(()=>{this.clickOutsideHandler&&document.addEventListener(`click`,this.clickOutsideHandler)},0),this.repositionHandler=()=>this.positionPanel(),window.addEventListener(`scroll`,this.repositionHandler,!0),window.addEventListener(`resize`,this.repositionHandler)}unbindGlobalHandlers(){this.clickOutsideHandler&&=(document.removeEventListener(`click`,this.clickOutsideHandler),null),this.repositionHandler&&=(window.removeEventListener(`scroll`,this.repositionHandler,!0),window.removeEventListener(`resize`,this.repositionHandler),null)}positionPanel(){if(!this.errorPanel||!this.currentErrorIcon)return;let e=this.currentErrorIcon.getBoundingClientRect();this.errorPanel.style.visibility=`hidden`,this.errorPanel.style.display=`block`;let t=this.errorPanel.getBoundingClientRect();this.errorPanel.style.visibility=``;let n=e.right+8+window.scrollX,r=e.top+window.scrollY,i=window.scrollX+window.innerWidth-8;n+t.width>i&&(n=e.left+window.scrollX-t.width-8),n<8&&(n=8);let a=window.scrollY+window.innerHeight-t.height-8;r>a&&(r=Math.max(window.scrollY+8,a)),this.errorPanel.style.left=`${n}px`,this.errorPanel.style.top=`${r}px`}cleanupErrorIcon(e){let t=this.errorHandlers.get(e);t&&(e.removeEventListener(`click`,t.clickHandler),e.removeEventListener(`keydown`,t.keyHandler),this.errorHandlers.delete(e)),this.errorDetails.delete(e),e.style.cursor=``,e.removeAttribute(`role`),e.removeAttribute(`tabindex`),e.removeAttribute(`aria-expanded`),e.removeAttribute(`data-oiwc-expanded`),this.currentErrorIcon===e&&this.hideErrorPanel()}};export{e as ErrorObserver};