/*! License details at fancyapps.com/license */
import{canUseDOM as t}from"../utils/canUseDOM.js";let e,n=!1,o=!1,i=!1,r=!1;const l=()=>{const t=new URL(document.URL).hash,e=t.slice(1).split("-"),n=e[e.length-1],o=n&&/^\+?\d+$/.test(n)&&parseInt(e.pop()||"1",10)||1;return{hash:t,slug:e.join("-"),index:o}},a=()=>{if(!e||e.getInstance())return;const{slug:t,index:n}=l();if(!t)return;let o=document.querySelector(`[data-slug="${t}"]`);if(o&&o.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0})),!e||e.getInstance())return;const i=document.querySelectorAll(`[data-fancybox="${t}"]`);i.length&&(o=i[n-1],o&&o.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0})))},s=()=>{var t,n,o;if(!e)return;if(i)return;const s=null==e?void 0:e.getInstance();if(!1===(null==s?void 0:s.getOptions().Hash))return;const{slug:c,index:u}=l(),d=(null===(t=null==s?void 0:s.getSlide())||void 0===t?void 0:t.fancybox)||(null===(n=null==s?void 0:s.getSlide())||void 0===n?void 0:n.slug)||void 0;s&&d&&(c===d?null===(o=s.getCarousel())||void 0===o||o.goTo(u-1):(r=!0,s.close(),r=!1)),a()},c=()=>{e&&setTimeout((()=>{n=!0,a(),n=!1,window.addEventListener("hashchange",s,!1)}),300)},u=()=>{let t,e="auto",a="";function s(){if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;const i=t.getCarousel();if(!i)return;const{hash:r,slug:s}=l(),u=t.getSlide();if(!u)return;let d=u.fancybox||"",h=parseInt(u.index+"",10)+1;if(!d)return;let f=`#${d}-${h}`;if(r!==f&&(a=r),history.scrollRestoration&&(e=history.scrollRestoration,history.scrollRestoration="manual"),i.on("change",c),!n)if(d===s)try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+f)}catch(t){}else try{window.history.pushState({},document.title,window.location.pathname+window.location.search+f),o=!0}catch(t){}}function c(){if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;const{slug:e}=l(),n=t.getSlide();if(!n)return;let o=n.fancybox||"",r=`#${o}-${n.index+1}`;if(o===e){i=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+r)}catch(t){}i=!1}}function u(){if(r)return;if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;const e=t.getSlide();if(!e)return;if(e.fancybox||""){i=!0;try{!o||n||function(){if(window.parent===window)return!1;try{var t=window.frameElement}catch(e){t=null}return null===t?"data:"===location.protocol:t.hasAttribute("sandbox")}()?window.history.replaceState({},document.title,window.location.pathname+window.location.search+a):window.history.back()}catch(t){}i=!1}}return{init:function(e){t=e,t.on("ready",s),t.on("close",u)},destroy:function(){null==t||t.off("ready",s),null==t||t.off("close",u);const n=null==t?void 0:t.getCarousel();n&&n.off("change",c),t=void 0,history.scrollRestoration&&e&&(history.scrollRestoration=e)}}};u.startFromUrl=a,u.setup=function(n){e||(e=n,t()&&(/complete|interactive|loaded/.test(document.readyState)?c():document.addEventListener("DOMContentLoaded",c)))};export{u as Hash};
