const L="starlight-image-zoom-zoomable",p=globalThis.requestIdleCallback??(y=>setTimeout(y,1));customElements.define("starlight-image-zoom",class w extends HTMLElement{#t;#i=[];#o;#s=this.querySelector("template");#e={image:"starlight-image-zoom-image",opened:"starlight-image-zoom-opened",source:"starlight-image-zoom-source",transition:"starlight-image-zoom-transition"};#r="zoomTransform";static#u=!1;constructor(){super();const t=()=>{p(()=>{const e=[...document.querySelectorAll(L)];e.length!==0&&(this.#m(e),document.addEventListener("click",this.#d),window.addEventListener("resize",this.#a))})};globalThis.addEventListener("DOMContentLoaded",t,{once:!0}),document.addEventListener("astro:after-preparation",()=>{document.removeEventListener("click",this.#d),window.removeEventListener("resize",this.#a)},{once:!0}),w.#u||=document.addEventListener("astro:after-swap",t)===void 0}#m(t){for(const e of t){const n=e.querySelector("img");n&&(this.#i.push(n),e.querySelector("button")?.addEventListener("click",i=>{i.stopPropagation(),this.#l(n)}))}}#d=({target:t})=>{if(!(t instanceof HTMLElement&&t.closest("figcaption"))){if(this.#t){this.#n();return}t instanceof HTMLImageElement&&this.#i.includes(t)&&this.#l(t)}};#a=()=>{this.#n(!0)};#c=()=>{this.#n()};#f=t=>{t.preventDefault(),this.#n()};#l(t){if(!this.#s||this.#t)return;this.#o=document.activeElement;const e=document.createElement("div");e.append(this.#s.content.cloneNode(!0));const n=e.querySelector("dialog"),i=n?.querySelector("figure");if(!n||!i)return;const r={overflow:document.body.style.overflow,width:document.body.style.width},d=document.body.clientWidth;document.body.style.overflow="hidden",document.body.style.width=`${d}px`,document.querySelector("header")?.style.setProperty("padding-inline-end",`calc(var(--sl-nav-pad-x) + ${window.innerWidth-d}px)`);const s=this.#w(t);t.classList.add(this.#e.source),s.classList.add(this.#e.image),i.append(s),this.#p(t.getAttribute("alt"),i),document.body.append(e),document.addEventListener("wheel",this.#c,{once:!0}),n.addEventListener("cancel",this.#f),n.showModal(),p(()=>{s.style.transform="",document.body.classList.add(this.#e.opened)}),this.#t={body:r,dialog:n,image:t,zoomedImage:s}}#n(t=!1){if(window.removeEventListener("wheel",this.#c),!this.#t)return;const{zoomedImage:e}=this.#t;e.style.transform=e.dataset[this.#r]??"",document.body.classList.add(this.#e.transition),document.body.classList.remove(this.#e.opened);const{matches:n}=globalThis.matchMedia("(prefers-reduced-motion: reduce)");t||n?this.#h():e.addEventListener("transitionend",this.#h,{once:!0})}#h=()=>{if(!this.#t)return;const{dialog:t,image:e}=this.#t;document.body.classList.remove(this.#e.transition),e.classList.remove(this.#e.source),t.parentElement?.remove(),document.body.style.overflow=this.#t.body.overflow,document.body.style.width=this.#t.body.width,document.querySelector("header")?.style.setProperty("padding-inline-end","var(--sl-nav-pad-x)"),this.#t=void 0,this.#o instanceof HTMLElement&&this.#o.focus()};#p(t,e){if(Object.hasOwn(this.dataset,"hideCaption")||(t=t?.trim()??"",t.length===0))return;const n=document.createElement("figcaption");n.ariaHidden="true",n.textContent=t,e.append(n)}#w(t){const e=t.getBoundingClientRect(),n=this.#y(t),i=n?window.innerWidth:t.naturalWidth,r=n?window.innerHeight:t.naturalHeight,d=Math.min(window.innerWidth,i),s=Math.min(window.innerHeight,r),l=Math.min(d/i,s/r),a=(n?window.innerWidth:t.naturalWidth)*l,c=(n?window.innerHeight:t.naturalHeight)*l,h=(window.innerHeight-c)/2,u=(window.innerWidth-a)/2,o=t.cloneNode(!0);o.removeAttribute("id"),o.style.position="absolute",o.style.width=`${a}px`,o.style.height=`${c}px`,o.style.top=`${h}px`,o.style.left=`${u}px`,o.style.transform="";const m=e.width/a,f=e.height/c,g=(-u+(e.width-a)/2+e.left)/m,v=(-h+(e.height-c)/2+e.top)/f;return o.style.transform=`scale(${m}, ${f}) translate3d(${g}px, ${v}px, 0)`,o.dataset[this.#r]=o.style.transform,t.parentElement?.tagName==="PICTURE"&&t.currentSrc&&(o.src=t.currentSrc),t.id&&(o.dataset.zoomId=t.id),o}#y(t){if(t.currentSrc.toLowerCase().endsWith(".svg"))return!0;try{const e=new URL(t.currentSrc);return e.pathname!=="/_image"?!1:e.searchParams.get("href")?.includes("origFormat=svg")}catch{return!1}}});