// @ts-check
(()=>{
  'use strict';
  const App=window.IUS_TEMPLATE,{utils}=App;
  let scale=1,x=0,y=0,start=null,pinch=null;
  const q=s=>utils.$(s);
  function clamp(){const vp=q('[data-lightbox-viewport]'),img=q('[data-lightbox-image]');if(!vp||!img)return;const maxX=Math.max(0,(img.clientWidth*scale-vp.clientWidth)/2),maxY=Math.max(0,(img.clientHeight*scale-vp.clientHeight)/2);x=Math.max(-maxX,Math.min(maxX,x));y=Math.max(-maxY,Math.min(maxY,y))}
  function apply(){clamp();const img=q('[data-lightbox-image]');if(img)img.style.transform=`translate3d(${x}px,${y}px,0) scale(${scale})`;const out=q('[data-zoom-output]');if(out)out.textContent=`${Math.round(scale*100)}%`;q('[data-lightbox-viewport]')?.classList.toggle('is-panning',scale>1)}
  function setScale(next,clientX,clientY){const prev=scale;scale=Math.max(1,Math.min(4,next));if(clientX!=null&&clientY!=null&&prev!==scale){x+=(clientX-window.innerWidth/2)*(1/prev-1/scale);y+=(clientY-window.innerHeight/2)*(1/prev-1/scale)}if(scale===1){x=0;y=0}apply()}
  function reset(){scale=1;x=0;y=0;apply()}
  function open(src,alt,trigger){const d=q('[data-lightbox-dialog]'),img=q('[data-lightbox-image]');if(!d||!img)return;img.src=src;img.alt=alt||'';q('[data-lightbox-title]').textContent=alt||'Image inspector';reset();utils.openDialog(d,trigger)}
  function close(){const d=q('[data-lightbox-dialog]');utils.closeDialog(d);const img=q('[data-lightbox-image]');if(img)img.src=''}
  function init(){
    q('[data-zoom-in]')?.addEventListener('click',()=>setScale(scale+.25));q('[data-zoom-out]')?.addEventListener('click',()=>setScale(scale-.25));q('[data-zoom-reset]')?.addEventListener('click',reset);utils.$$('[data-lightbox-close]').forEach(b=>b.addEventListener('click',close));
    utils.$$('[data-lightbox-src]').forEach(b=>b.addEventListener('click',()=>open(b.dataset.lightboxSrc,b.dataset.lightboxAlt,b)));
    const vp=q('[data-lightbox-viewport]');vp?.addEventListener('wheel',e=>{e.preventDefault();setScale(scale+(e.deltaY<0?.2:-.2),e.clientX,e.clientY)},{passive:false});
    vp?.addEventListener('pointerdown',e=>{if(scale<=1)return;vp.setPointerCapture(e.pointerId);start={id:e.pointerId,x:e.clientX,y:e.clientY,ox:x,oy:y}});vp?.addEventListener('pointermove',e=>{if(!start||start.id!==e.pointerId)return;x=start.ox+e.clientX-start.x;y=start.oy+e.clientY-start.y;apply()});vp?.addEventListener('pointerup',()=>start=null);
    vp?.addEventListener('touchstart',e=>{if(e.touches.length===2){const [a,b]=e.touches;pinch={distance:Math.hypot(a.clientX-b.clientX,a.clientY-b.clientY),scale}}},{passive:true});vp?.addEventListener('touchmove',e=>{if(e.touches.length===2&&pinch){const [a,b]=e.touches;setScale(pinch.scale*Math.hypot(a.clientX-b.clientX,a.clientY-b.clientY)/pinch.distance)}},{passive:true});vp?.addEventListener('touchend',()=>pinch=null,{passive:true});
    vp?.addEventListener('dblclick',()=>scale===1?setScale(2):reset());vp?.addEventListener('keydown',e=>{if(e.key==='+')setScale(scale+.25);else if(e.key==='-')setScale(scale-.25);else if(e.key==='0')reset();else if(scale>1&&['ArrowLeft','ArrowRight','ArrowUp','ArrowDown'].includes(e.key)){e.preventDefault();const step=35;x+=e.key==='ArrowLeft'?step:e.key==='ArrowRight'?-step:0;y+=e.key==='ArrowUp'?step:e.key==='ArrowDown'?-step:0;apply()}});
    window.addEventListener('resize',()=>{reset()},{passive:true});
  }
  App.modules.lightbox={init,open,close,reset};
})();
