(()=>{"use strict";const e=window.wc.__experimentalInteractivity,t=t=>(0,e.getContext)(t),{state:o,actions:n}=(0,e.store)("woocommerce/product-gallery"),a=(e,t)=>{const o=e[e.isDialogOpen?"dialogVisibleImagesIds":"visibleImagesIds"],n=o.indexOf(e.selectedImage),a="next"===t?Math.min(n+1,o.length-1):Math.max(n-1,0);e.selectedImage=o[a]},r=e=>{var t;e.isDialogOpen=!1,e.selectedImage=e.firstMainImageId,document.body.classList.remove("wc-block-product-gallery-modal-open"),e.elementThatTriggeredDialogOpening&&(null===(t=e.elementThatTriggeredDialogOpening)||void 0===t||t.focus(),e.elementThatTriggeredDialogOpening=null)},c={state:{get isSelected(){const{selectedImage:e,imageId:o}=t();return e===o},get pagerDotFillOpacity(){return o.isSelected?1:.2},get pagerButtonPressed(){return!!o.isSelected},get thumbnailTabIndex(){return o.isSelected?"0":"-1"}},actions:{closeDialog:()=>{const e=t();r(e)},openDialog:()=>{t().isDialogOpen=!0,document.body.classList.add("wc-block-product-gallery-modal-open");const e=document.querySelector('dialog[aria-label="Product gallery"]');if(!e)return;e.focus();const o=e.querySelectorAll(".wc-block-product-gallery-large-image-next-previous--button")[0];o&&setTimeout((()=>{o.focus()}),100)},selectImage:()=>{const e=t();e.selectedImage=e.imageId},selectNextImage:e=>{e.stopPropagation();const o=t();a(o,"next")},selectPreviousImage:e=>{e.stopPropagation();const o=t();a(o,"previous")},onThumbnailKeyDown:e=>{const o=t();"Enter"!==e.code&&"Space"!==e.code&&"NumpadEnter"!==e.code||("Space"===e.code&&e.preventDefault(),o.selectedImage=o.imageId)},onSelectedLargeImageKeyDown:a=>{if(o.isSelected&&"Enter"===a.code||"Space"===a.code||"NumpadEnter"===a.code){var r;"Space"===a.code&&a.preventDefault(),n.openDialog();const o=null===(r=(0,e.getElement)())||void 0===r?void 0:r.ref;t().elementThatTriggeredDialogOpening=o}},onViewAllImagesKeyDown:o=>{if("Enter"===o.code||"Space"===o.code||"NumpadEnter"===o.code){var a;"Space"===o.code&&o.preventDefault(),n.openDialog();const r=null===(a=(0,e.getElement)())||void 0===a?void 0:a.ref;t().elementThatTriggeredDialogOpening=r}}},callbacks:{watchForChangesOnAddToCartForm:()=>{const e=t(),o=document.querySelector(`form[data-product_id="${e.productId}"]`);if(!o)return;const n=new MutationObserver((function(t){for(const o of t){const t=o.target.getAttribute("current-image");"attributes"===o.type&&t&&e.visibleImagesIds.includes(t)&&(e.selectedImage=t)}}));n.observe(o,{attributes:!0});const a=document.querySelector(".wp-block-add-to-cart-form .reset_variations"),r=()=>{e.selectedImage=e.firstMainImageId};return a&&a.addEventListener("click",r),()=>{n.disconnect(),document.removeEventListener("click",r)}},keyboardAccess:()=>{const e=t();let o=!0;const n=t=>{o&&e.isDialogOpen&&(o=!1,requestAnimationFrame((()=>{o=!0})),"Escape"===t.code&&r(e),"ArrowLeft"===t.code&&a(e,"previous"),"ArrowRight"===t.code&&a(e,"next"))};return document.addEventListener("keydown",n),()=>document.removeEventListener("keydown",n)},dialogFocusTrap:()=>{const e=document.querySelector('dialog[aria-label="Product gallery"]');if(!e)return;const t=t=>{if("Tab"===t.code){const o='a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [tabindex]:not([tabindex="-1"])',n=e.querySelectorAll(o);if(!n.length)return;const a=n[0],r=n[n.length-1];t.shiftKey||t.target!==r||(t.preventDefault(),a.focus()),t.shiftKey&&t.target===a&&(t.preventDefault(),r.focus())}};return e.addEventListener("keydown",t),()=>e.removeEventListener("keydown",t)}}};(0,e.store)("woocommerce/product-gallery",c)})();