class LiquidAdaptiveColorBehavior extends LiquidBehavior{static behaviorName="liquidAdaptiveColor";static regionsEvents={liquidPageContent:[{"change:computedStyles":"initialize"}]};static windowEvents={scroll:{func:"onScroll",throttle:{wait:150,options:{leading:!0}}}};get bindToThis(){return["init","initWatch","detectCollision","onPageContentBgChange"]}get throttledFunctions(){return{onPageContentBgChange:{wait:150}}}initialize(){this.init(),this.onScroll()}init(){if(this.handleWidgetViewColorSchemeChange(),this.liquidApp.layoutRegions?.liquidPageContent?.model.get("behaviors").findIndex(t=>t.behaviorName==="liquidAdaptiveBackground")>=0)return this.listenToPageContentBgChange();this.initWatch()}handleWidgetViewColorSchemeChange(){this.view.model.on("change:colorScheme",(i,t)=>{t?this.view.el.setAttribute(DATA_ATTRS.COLOR_SCHEME.HTML_ATTR,t):this.view.el.removeAttribute(DATA_ATTRS.COLOR_SCHEME.HTML_ATTR)})}listenToPageContentBgChange(){const i=this.liquidApp.layoutRegions?.liquidPageContent?.model;i&&this.listenTo(i,"change:adaptiveBg",this.onPageContentBgChange)}onPageContentBgChange(i,{currentColor:t}){this.setWidgetModelColorScheme(this.getLuminosity(t.brightness))}initWatch(){if(!this.liquidApp.layoutRegions?.liquidPageContent)return;const i=new Backbone.Collection;this.liquidApp.layoutRegions.liquidPageContent&&i.add(this.liquidApp.layoutRegions.liquidPageContent.model?.get("childrenCollection").filter(t=>t.get("isContainer"))),this.liquidApp.layoutRegions.liquidPageFooter&&i.add(this.liquidApp.layoutRegions.liquidPageFooter.model?.get("childrenCollection").filter(t=>t.get("isContainer"))),this.pageContentContainers=i,this.scrollDirection=1,this.lastScrollPos=window.scrollY}onScroll(){this.isDestroyed||!this.pageContentContainers?.length||(this.scrollDirection=this.lastScrollPos<=window.scrollY?1:-1,this.detectCollision(),this.lastScrollPos=window.scrollY)}detectCollision(){const{scrollY:i}=window,t=this.view.model.get("rect")||this.view.el.getBoundingClientRect(),n=this.pageContentContainers.map(e=>{const o=e.get("rect")||{};return{cid:e.cid,rect:{...o,y:(o?.y||0)-i,bottom:(o?.bottom||0)-i}}}),l=n.filter(({rect:e})=>(this.scrollDirection>0?e.y>=t.y:e.y<=t.y)&&e.right>=t.right&&e.x<=t.x),s=n.filter(({rect:e})=>(this.scrollDirection>0?e.y<=t.y:e.y>=t.y)&&e.right>=t.right&&e.x<=t.x),d=this.scrollDirection>0?s[s.length-1]:l[l.length-1];d&&this.setWidgetModelColorScheme(this.getLuminosity(this.pageContentContainers.find(e=>e.cid===d.cid).get("brightness").backgroundColor))}getLuminosity(i){let t="light";return(!isNaN(i)&&i<=.5||i==="dark")&&(t="dark"),t}setWidgetModelColorScheme(i){this.liquidApp.fastdom.mutate(()=>this.view.model.set({colorScheme:i}))}destroy(){this.setWidgetModelColorScheme(null),super.destroy()}}typeof window<"u"&&(window.liquid?.app?window.liquid?.app?.model?.set("loadedBehaviors",[...window.liquid.app.model.get("loadedBehaviors"),LiquidAdaptiveColorBehavior]):window.liquid?.loadedBehaviors?.push(LiquidAdaptiveColorBehavior));
