import{getContext as e,getElement as t,store as s}from"@wordpress/interactivity";const{state:n,actions:i}=s("twentig/carousel",{state:{get isAtStart(){const{currentIndex:t}=e();return 0===t},get isAtEnd(){const{visibleItems:t,currentIndex:s,maxIndex:n}=e();return t?.length&&Math.max(...t)===n||s===n},get currentIndex(){return e().currentIndex},get isMarkerActive(){const t=e();return n.currentIndex===t.index}},actions:{goPrevious(){const{currentIndex:t}=e(),s=t>0?t-1:t;i.updateCarouselPosition(s)},goNext(){const{currentIndex:t,maxIndex:s}=e(),n=t<s?t+1:t;i.updateCarouselPosition(n)},updateCarouselPosition(t){const s=e().slides;if(!s?.children[t])return;const n=parseFloat(getComputedStyle(s).gap)||0,i=s.children[t];s.scrollLeft=(i.offsetWidth+n)*t},goToIndex(){const t=e().index;i.updateCarouselPosition(t)}},callbacks:{initCarousel(){const{ref:s}=t();let n=null,i=null,r=null;const o=()=>{n?._io?.disconnect(),i?.disconnect(),clearTimeout(r)};try{n=e();const t=s.querySelector(".tw-gallery-carousel-list");if(!t)return o;n.slides=t,t.classList.add("is-initialized");const a=e=>{n._io?.disconnect(),n.visibleItems=[];const s=getComputedStyle(t).paddingLeft;n.paddingLeft=s;const o=e&&"0px"!==s?`0px -${s} 0px -${s}`:"0px";let c=!0;n._io=new IntersectionObserver((t=>{let s=[...n.visibleItems];if(t.forEach((e=>{const t=parseInt(e.target.dataset.index,10);isNaN(t)||(e.isIntersecting?(s.includes(t)||s.push(t),e.target.classList.add("in-view")):(s=s.filter((e=>e!==t)),e.target.classList.remove("in-view")))})),n.visibleItems=s,n.currentIndex,n.visibleItems.length&&(n.currentIndex=Math.min(...n.visibleItems)),c&&e&&!n.visibleItems.includes(0))return c=!1,void queueMicrotask((()=>a(!1)));c=!1}),{root:t,rootMargin:o,threshold:.95}),Array.from(t.children).forEach(((e,t)=>{e.dataset.index=t,n._io.observe(e)})),e?i||(i=new ResizeObserver((()=>{getComputedStyle(t).paddingLeft!==n.paddingLeft&&(clearTimeout(r),r=setTimeout((()=>{a(!0)}),100))})),i.observe(t)):i&&(i.disconnect(),i=null)};a(n.showEdges)}catch(e){console.error("Carousel init failed:",e)}return o}}});