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