"use strict";class PhotoReel extends EventHandlerBase{#config;#photoReelEl;#tiles;#closeButton;#initialized;#initCancelled;#destroyCancelled;#loadWaiting;#destroyWaiting;#intervalLockerCount;#maxIntervalLockerCount;#threadAttemptTimeout;#tilesSeen;#isVisible;constructor(_config){super(),this.#config=Object.assign({},_config),this.#photoReelEl=null,this.#tiles=[],this.#closeButton=null,this.#initialized=!1,this.#initCancelled=!1,this.#destroyCancelled=!1,this.#loadWaiting=!1,this.#destroyWaiting=!1,this.#intervalLockerCount=0,this.#maxIntervalLockerCount=10,this.#threadAttemptTimeout=750,this.#tilesSeen=[],this.#isVisible=!1}#initPhotoReel(){if(this.#initialized)return;this.#loadWaiting=!0,this.#initCancelled=!1,this.#destroyCancelled=!0;let interval=setInterval(function(){!this.#destroyWaiting||this.#intervalLockerCount>this.#maxIntervalLockerCount?(this.#applySettings(),this.#setVisibility(!1),this.#captureDomContext(),this.#loadWaiting=!1,this.#intervalLockerCount=0,clearInterval(interval)):this.#intervalLockerCount+=1,this.#initCancelled&&(this.#intervalLockerCount=0,this.#loadWaiting=!1,clearInterval(interval))}.bind(this),this.#threadAttemptTimeout)}#captureDomContext(){if(!this.#config.photoReelEl)throw new Error("config.photoReelEl not set");this.#photoReelEl=this.#config.photoReelEl,this.#tiles=this.#photoReelEl.querySelectorAll(".photo-reel-photo"),this.#closeButton=this.#photoReelEl.querySelector(".js-close-btn")}#applySettings(){if(!this.#photoReelEl||!this.#closeButton)return null;this.#closeButton.addEventListener("click",this.hide.bind(this)),this.#photoReelEl.addEventListener("click",function(e){const photoReelPhoto=e.target.closest(".photo-reel-photo");photoReelPhoto&&this.#tileClicked(photoReelPhoto)}.bind(this)),this.#photoReelEl.addEventListener("scroll",function(e){this.#checkAllTilesVisibility(20)}.bind(this)),document.addEventListener("keydown",function(event){"Escape"===event.key&&this.hide()}.bind(this))}#tileClicked(_tile){const idx=Array.prototype.indexOf.call(this.#tiles,_tile);this._emit("tileclick",{index:idx,tile:this.#tiles[idx]})}#checkAllTilesVisibility(threshold){this.#tiles.forEach((function(tile,idx){this.#isVisible&&this.#checkTileVisible(tile,threshold,"within")&&this.#tilesSeen.indexOf(idx)<=-1&&(this.#tilesSeen.push(idx),this._emit("tileviewed",{index:idx,total:this.#tiles.length,tile:this.#tiles[idx]}))}),this)}#checkTileVisible(tile,threshold,mode){threshold=threshold||0,mode=mode||"visible";const rect=tile.getBoundingClientRect(),viewHeight=Math.max(document.documentElement.clientHeight,window.innerHeight),above=rect.bottom-threshold<0,below=rect.top-viewHeight+threshold>=0,within=rect.top-threshold>=0&&rect.bottom+threshold<=viewHeight;return"above"===mode?above:"below"===mode?below:"within"===mode?within:!above&&!below}#destroyPhotoReel(){this.#destroyWaiting=!0,this.#initCancelled=!0,this.#destroyCancelled=!1;let interval=setInterval(function(){this.#loadWaiting?this.#intervalLockerCount+=1:(clearInterval(interval),this.#destroyWaiting=!1,this.#intervalLockerCount=0),this.#destroyCancelled&&(this.#intervalLockerCount=0,this.#destroyWaiting=!1,clearInterval(interval))}.bind(this),this.#threadAttemptTimeout);this.offAll(),this.#initialized=!1}#setVisibility(toggle){const body=document.querySelector("body");this.#isVisible=toggle,toggle?(this.#photoReelEl.classList.add("show"),body.classList.add("overflow-hidden")):(this.#photoReelEl.classList.remove("show"),body.classList.remove("overflow-hidden"))}init(){this.#captureDomContext(),this.#initPhotoReel()}destroy(){this.#destroyPhotoReel()}show(idx){this.#setVisibility(!0),this.#checkAllTilesVisibility(0),void 0!==idx&&this.#tiles[idx].scrollIntoView({behavior:"smooth",block:"center"}),this._emit("shown",{})}hide(){this.#isVisible&&(this.#setVisibility(!1),this._emit("hidden",{}))}}window.PhotoReel=PhotoReel;