document.addEventListener("DOMContentLoaded",(function(){class t{constructor(t){this.element=t,this.progressBar=t.querySelector(".spectra-counter-progress-bar"),this.progressLabel=t.querySelector(".spectra-counter-progress-label"),this.progressValue=t.querySelector(".spectra-counter-value"),this.progressBar&&this.progressValue&&(this.counterBlock=t.closest(".wp-block-spectra-counter"),this.counterBlock&&(this.startNumber=parseFloat(this.counterBlock.dataset.counterStart)||0,this.endNumber=parseFloat(this.counterBlock.dataset.counterEnd)||100,this.totalNumber=parseFloat(this.counterBlock.dataset.counterTotal)||100,this.duration=parseInt(this.counterBlock.dataset.counterDuration)||2e3,this.prefix=this.counterBlock.dataset.counterPrefix||"",this.suffix=this.counterBlock.dataset.counterSuffix||"",this.separator=this.counterBlock.dataset.counterSeparator||",",this.decimals=parseInt(this.counterBlock.dataset.counterDecimals)||0,this.totalNumber<Math.max(Math.abs(this.startNumber),Math.abs(this.endNumber))&&(this.totalNumber=Math.max(Math.abs(this.startNumber),Math.abs(this.endNumber))),this.setInitialState(),this.hookIntoCounterAnimation()))}setInitialState(){const t=this.totalNumber>0?Math.min(Math.max(this.startNumber/this.totalNumber*100,0),100):0;this.progressBar.style.width=`${t}%`,this.updateProgressLabel(this.startNumber)}formatNumber(t){const e=parseFloat(t).toFixed(this.decimals);if(this.separator&&""!==this.separator.trim()){const t=e.split(".");return t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,this.separator),t.join(".")}return e}updateProgressLabel(t){const e=this.formatNumber(t),s=this.progressLabel.querySelector(".spectra-counter-prefix");s&&(s.textContent=this.prefix),this.progressValue.textContent=e;const r=this.progressLabel.querySelector(".spectra-counter-suffix");r&&(r.textContent=this.suffix)}hookIntoCounterAnimation(){this.counterBlock.addEventListener("counterAnimationStart",(()=>{this.startProgressAnimation()})),this.counterBlock.addEventListener("counterAnimationUpdate",(t=>{t.detail&&void 0!==t.detail.currentNumber&&this.updateProgress(t.detail.currentNumber)})),this.observeElement()}startProgressAnimation(){this.element.classList.add("spectra-counter-animating");const t=performance.now(),e=this.endNumber-this.startNumber,s=r=>{const a=r-t,o=Math.min(a/this.duration,1),i=this.startNumber+e*o;this.updateProgress(i),o<1?requestAnimationFrame(s):(this.updateProgress(this.endNumber),this.element.classList.remove("spectra-counter-animating"))};requestAnimationFrame(s)}updateProgress(t){const e=isNaN(t)?this.startNumber:t,s=this.totalNumber>0?Math.min(Math.max(e/this.totalNumber*100,0),100):0;this.progressBar.style.width=`${s}%`,this.updateProgressLabel(e)}observeElement(){const t=new IntersectionObserver((e=>{e.forEach((e=>{e.isIntersecting&&(setTimeout((()=>{this.element.classList.contains("spectra-counter-animating")||this.startProgressAnimation()}),100),t.unobserve(e.target))}))}),{threshold:.1,rootMargin:"0px 0px -50px 0px"});t.observe(this.element)}}function e(){document.querySelectorAll(".wp-block-spectra-counter-child-progress-bar.spectra-counter-progress-bar--bar").forEach((e=>{new t(e)}))}e(),document.addEventListener("spectra-blocks-loaded",e)}));