/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import{r as t,c as s,g as i,e,a as n}from"./index-BM3Om9WE.js";import{h}from"./renderer-DpNDfhNy.js";const a=class{constructor(i){t(this,i),this.nanoResizeStateChange=s(this,"nanoResizeStateChange",7),this.nanoResizeObserverReady=s(this,"nanoResizeObserverReady",7),this.nanoResizeContentFitChange=s(this,"nanoResizeContentFitChange",7),this.nanoResize=s(this,"nanoResize",7)}ro;appliedStates;isReady=!1;get host(){return i(this)}currentWidth;currentHeight;classNames=[];contentFitX=null;contentFitY=null;classNameChange(){this.host.className=this.classNames.join(" "),!this.isReady&&this.classNames.includes("is-ready")&&(this.isReady=!0,this.nanoResizeObserverReady.emit())}notifyContentFit;states;newStatesProp(){this.statesChanged()}nanoResizeStateChange;nanoResizeObserverReady;nanoResizeContentFitChange;nanoResize;dimensionChanged(){this.assessChanges()}statesChanged(){if(!this.states)return;const t=t=>{const s=t.split(/(\d+)/).filter((t=>t.length));return{bp:parseInt(s[0]),dir:s[1]}};this.appliedStates={h:new Map,w:new Map},this.states.split(",").map((s=>{if((s=s.trim()).includes(" ")){const[i,...e]=s.split(" "),{bp:n,dir:h}=t(i);this.appliedStates[h].set(n,{states:e,applied:!1})}else{const{bp:i,dir:e}=t(s);this.appliedStates[e].set(i,{applied:!1})}}))}assesContentFit(){if(!this.notifyContentFit)return;const t=(t=>{if("contents"!==window.getComputedStyle(t).display)return t;const s=t=>Array.from(t.children).find((t=>{if("contents"!==window.getComputedStyle(t).display)return t;s(t)}));return s(t)})(this.host.firstElementChild);if(!t)return;let s=!1;const{width:i,height:e}=this.host.getBoundingClientRect();this.notifyContentFit&&this.notifyContentFit.includes("x")&&(i<t.scrollWidth&&!1!==this.contentFitX?(s=!0,this.contentFitX=!1):i>=t.scrollWidth&&!0!==this.contentFitX&&(s=!0,this.contentFitX=!0)),this.notifyContentFit&&this.notifyContentFit.includes("y")&&(e<t.scrollHeight&&!1!==this.contentFitY?(s=!0,this.contentFitY=!1):e>=t.scrollHeight&&!0!==this.contentFitY&&(s=!0,this.contentFitY=!0)),s&&this.nanoResizeContentFitChange.emit({x:this.contentFitX,y:this.contentFitY})}assessChanges=()=>{if(!this.currentWidth&&!this.currentHeight)return;if(e((()=>this.assesContentFit())),!this.states||!this.appliedStates)return;const t={h:new Map,w:new Map};let s=!1;Object.keys(this.appliedStates).forEach((i=>{let e;e="h"===i?this.currentHeight:this.currentWidth,this.appliedStates[i].forEach(((n,h)=>{e>=h&&!1===n.applied?(n.applied=!0,t[i].set(h,n),s=!0):e<h&&!0===n.applied&&(n.applied=!1,t[i].set(h,n),s=!0)}))})),s?this.applyChanges(t):this.classNames.includes("is-ready")||(this.classNames=[...this.classNames,"is-ready"])};applyChanges(t){if(!this.states)return;let s=[...this.classNames];Object.keys(t).forEach((i=>{t[i].forEach((t=>{t.states&&t.states.map((i=>{t.applied?(s.push(i),s=s.filter((t=>t!=="not-"+i))):(s.push("not-"+i),s=s.filter((t=>t!==i)))}))}))})),this.classNames=[...new Set(["is-ready",...s])],this.nanoResizeStateChange.emit(this.toSimpleObj(t))}toSimpleObj(t){const s={};return Object.keys(t).forEach((i=>{t[i].forEach(((t,e)=>{s[e+i]=t.applied}))})),s}attachRO(){window.ResizeObserver&&(this.ro=new ResizeObserver((t=>{const{width:s,height:i}=this.host.getBoundingClientRect();this.currentWidth=Math.ceil(s),this.currentHeight=Math.ceil(i),this.nanoResize.emit(t)})),this.ro.observe(this.host))}componentWillLoad(){this.classNames=Array.from(this.host.classList),this.statesChanged(),this.applyChanges(this.appliedStates),this.currentWidth||this.currentHeight?this.assessChanges():e((()=>{const{width:t,height:s}=this.host.getBoundingClientRect();this.currentWidth=Math.ceil(t),this.currentHeight=Math.ceil(s)}))}connectedCallback(){this.ro||this.attachRO()}disconnectedCallback(){this.ro&&(this.ro.disconnect(),this.ro=void 0)}render(){return h(n,{key:"a2c57218f65a9af2297e6662c29d3a7c853092df",class:{"nano-resize-observe":!0,"content-fit-x":this.contentFitX,"content-fit-y":this.contentFitY,"content-nofit-x":!1===this.contentFitX,"content-nofit-y":!1===this.contentFitY}},h("slot",{key:"cf0faa31f7c1daa08238555a46699c1b731e13b8"}),!!this.notifyContentFit&&(null!==this.contentFitX||null!==this.contentFitY)&&[h("slot",this.contentFitX?{name:"content-fit-x"}:{name:"content-nofit-x"}),h("slot",this.contentFitY?{name:"content-fit-y"}:{name:"content-nofit-y"})])}static get watchers(){return{classNames:["classNameChange"],states:["newStatesProp","statesChanged"],currentHeight:["dimensionChanged"],currentWidth:["dimensionChanged"]}}};a.style=":host { display: block }";export{a as nano_resize_observe}