(function(h,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(h=typeof globalThis<"u"?globalThis:h||self,o(h["Builder-state"]={}))})(this,function(h){"use strict";const o=r=>r===null||typeof r!="object"&&typeof r!="function";class a{overrides;compListeners;subscribeToChangesFunc;constructor(e){this.overrides=new Map(e?Object.entries(e):[]),this.compListeners=new Map,this.subscribeByNickname=this.subscribeByNickname.bind(this),this.getOverrides=this.getOverrides.bind(this),this.updateOverrides=this.updateOverrides.bind(this),this.subscribeToChanges=this.subscribeToChanges.bind(this),this.subscribeToChangesFunc=null}subscribeByNickname(e,s){return this.compListeners.has(e)||this.compListeners.set(e,new Set),this.compListeners.get(e).add(s),()=>{const i=this.compListeners.get(e);i.delete(s),i.size===0&&this.compListeners.delete(e)}}getOverrides(e){return this.overrides.get(e)}updateOverrides(e,s){if(typeof s>"u")this.overrides.delete(e);else{const i=this.overrides.get(e);i?o(s)?this.overrides.set(e,s):this.overrides.set(e,Object.assign(i,s)):this.overrides.set(e,s)}this.compListeners.get(e)?.forEach(i=>i(s)),this.subscribeToChangesFunc?.({[e]:s})}subscribeToChanges(e){return this.subscribeToChangesFunc=e,()=>{this.subscribeToChangesFunc=null}}}class d{parent;store;#e=new Map;constructor(e){this.parent=e,this.store=new a,this.getOverrides=this.getOverrides.bind(this),this.updateOverrides=this.updateOverrides.bind(this)}createChildStore(){return new d(this)}#s(e){if(this.parent&&!this.#e.has(e)){const s=()=>{const t=this.parent.getOverrides(e);this.updateOverrides(e,t)};this.#e.set(e,this.parent.subscribeByNickname(e,s))}}subscribeByNickname(e,s){const t=this.store.subscribeByNickname(e,s);return this.#s(e),t}getOverrides(e){return this.#s(e),this.store.getOverrides(e)??this.parent?.getOverrides(e)??!1}updateOverrides(e,s){this.#s(e),this.store.updateOverrides(e,s)}}const b=(...r)=>[...r.reduce((s,t)=>(t?.className?.split(" ").forEach(i=>s.add(i)),s),new Set)].join(" ");class c{parent;store;#e;#s=new Map;constructor(e,s,t){this.parent=s,this.store=new a(e),this.#e=t,this.getOverrides=this.getOverrides.bind(this),this.updateOverrides=this.updateOverrides.bind(this),this.initWithParentProps=this.initWithParentProps.bind(this),this.subscribeToChanges=this.subscribeToChanges.bind(this)}createChildStore(e){return new c(void 0,this,e)}#t(e){if(this.parent&&!this.#s.has(e)){const s=t=>{this.updateOverrides(e,t)};this.#s.set(e,this.parent.subscribeByNickname(e,s))}}subscribeByNickname(e,s){const t=this.store.subscribeByNickname(e,s);return this.#t(e),t}getOverrides(e){this.#t(e);const s=this.store.getOverrides(e);return typeof s>"u"?this.initWithParentProps(e):s}updateOverrides(e,s){if(s&&"className"in s){const t=this.getOverrides(e);s.className=b(t,s)}this.store.updateOverrides(e,s)}#i(e){const s=this.#e,t={...e,...s};return"className"in t&&(t.className=b(e,s,{className:t.id})),t}initWithParentProps(e){const s=this.parent?.getOverrides(e);if(!s)return;const t=this.#i(s);return this.store.updateOverrides(e,t),t}subscribeToChanges(e){this.store.subscribeToChanges(e)}}class g{#e;#s;constructor(){this.#s=new Set,this.subscribe=this.subscribe.bind(this),this.getAllStyles=this.getAllStyles.bind(this),this.updateOverrides=this.updateOverrides.bind(this)}subscribe(e){return this.#s.add(e),()=>{this.#s.delete(e)}}getScopedStylesStore(e){return{subscribe:this.getScopedStylesStore.bind(this),getScopedStylesStore:this.getScopedStylesStore.bind(this),getAllStyles:this.getAllStyles.bind(this),getOverrides:t=>this.getOverrides(`${t}${e}`),updateOverrides:(t,i)=>{this.updateOverrides(`${t}${e}`,i)}}}getOverrides(e){return this.#e?.[e]}getAllStyles(){return this.#e}updateOverrides(e,s){if(s===void 0){const{[e]:t,...i}=this.#e;this.#e=Object.keys(i).length===0?void 0:i}else this.#e={...this.#e,[e]:s};this.#s.forEach(t=>t())}}const l=r=>"item-"+r;class u{#e;#s;#t;#i;#h;#r;constructor(e,s){this.#e=new c(e),this.#s=new d,this.#t=new g,this.#h=s,this.#i=new Map,this.#r=new Map}get props(){return this.#e}get scopedStores(){return this.#i}get deletedComponents(){return this.#s}get styles(){return this.#t}get scopeId(){return this.#h}createScopedStore(e,s,t){const i=this.#i.get(e);if(i)return i;const n=new u({},e);n.#e=this.#e.createChildStore({className:l(e)});for(const p in s)n.props.initWithParentProps(p),n.props.updateOverrides(p,s[p]);return n.#s=this.#s.createChildStore(),n.#t=this.#t.getScopedStylesStore(`.${l(e)}`),this.#i.set(e,n),this.#r.set(e,t),n}disposeScopedStore(e){this.#r.get(e)?.(),this.#i.delete(e)}subscribeToChanges(e){this.#e.subscribeToChanges(e)}}h.StateStoreImpl=u,h.isPrimitive=o,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=builder-state.js.map