var _____WB$wombat$assign$function_____=function(name){return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name))||self[name];};if(!self.__WB_pmw){self.__WB_pmw=function(obj){this.__WB_source=obj;return this;}}{
let window = _____WB$wombat$assign$function_____("window");
let self = _____WB$wombat$assign$function_____("self");
let document = _____WB$wombat$assign$function_____("document");
let location = _____WB$wombat$assign$function_____("location");
let top = _____WB$wombat$assign$function_____("top");
let parent = _____WB$wombat$assign$function_____("parent");
let frames = _____WB$wombat$assign$function_____("frames");
let opens = _____WB$wombat$assign$function_____("opens");
import n, { useContext as b, useMemo as P, useEffect as v, useSyncExternalStore as O, useState as N, useCallback as m } from "react";
import { useGetService as w } from "@wix/services-manager-react";
const C = (r) => r === null || typeof r != "object" && typeof r != "function";
class g {
  overrides;
  compListeners;
  constructor(e) {
    this.overrides = new Map(e ? Object.entries(e) : []), this.compListeners = /* @__PURE__ */ new Map(), this.subscribeByNickname = this.subscribeByNickname.bind(this), this.getOverrides = this.getOverrides.bind(this), this.updateOverrides = this.updateOverrides.bind(this);
  }
  subscribeByNickname(e, t) {
    return this.compListeners.has(e) || this.compListeners.set(e, /* @__PURE__ */ new Set()), this.compListeners.get(e).add(t), () => {
      const i = this.compListeners.get(e);
      i.delete(t), i.size === 0 && this.compListeners.delete(e);
    };
  }
  getOverrides(e) {
    return this.overrides.get(e);
  }
  updateOverrides(e, t) {
    if (typeof t > "u")
      this.overrides.delete(e);
    else {
      const i = this.overrides.get(e);
      i ? C(t) ? this.overrides.set(e, t) : this.overrides.set(e, Object.assign(i, t)) : this.overrides.set(e, t);
    }
    this.compListeners.get(e)?.forEach((i) => i(t));
  }
}
class u {
  parent;
  store;
  #e = /* @__PURE__ */ new Map();
  constructor(e) {
    this.parent = e, this.store = new g(), this.getOverrides = this.getOverrides.bind(this), this.updateOverrides = this.updateOverrides.bind(this);
  }
  createChildStore() {
    return new u(this);
  }
  #t(e) {
    if (this.parent && !this.#e.has(e)) {
      const t = () => {
        const s = this.parent.getOverrides(e);
        this.updateOverrides(e, s);
      };
      this.#e.set(e, this.parent.subscribeByNickname(e, t));
    }
  }
  subscribeByNickname(e, t) {
    const s = this.store.subscribeByNickname(e, t);
    return this.#t(e), s;
  }
  getOverrides(e) {
    return this.#t(e), this.store.getOverrides(e) ?? this.parent?.getOverrides(e) ?? !1;
  }
  updateOverrides(e, t) {
    this.#t(e), this.store.updateOverrides(e, t);
  }
}
const l = (...r) => [...r.reduce((t, s) => (s?.className?.split(" ").forEach((i) => t.add(i)), t), /* @__PURE__ */ new Set())].join(" ");
class a {
  parent;
  store;
  #e;
  #t = /* @__PURE__ */ new Map();
  constructor(e, t, s) {
    this.parent = t, this.store = new g(e), this.#e = s, this.getOverrides = this.getOverrides.bind(this), this.updateOverrides = this.updateOverrides.bind(this), this.initWithParentProps = this.initWithParentProps.bind(this);
  }
  createChildStore(e) {
    return new a(void 0, this, e);
  }
  #s(e) {
    if (this.parent && !this.#t.has(e)) {
      const t = (s) => {
        this.updateOverrides(e, s);
      };
      this.#t.set(e, this.parent.subscribeByNickname(e, t));
    }
  }
  subscribeByNickname(e, t) {
    const s = this.store.subscribeByNickname(e, t);
    return this.#s(e), s;
  }
  getOverrides(e) {
    this.#s(e);
    const t = this.store.getOverrides(e);
    return typeof t > "u" ? this.initWithParentProps(e) : t;
  }
  updateOverrides(e, t) {
    if (t && "className" in t) {
      const s = this.getOverrides(e);
      t.className = l(s, t);
    }
    this.store.updateOverrides(e, t);
  }
  #r(e) {
    const t = this.#e, s = { ...e, ...t };
    return "className" in s && (s.className = l(e, t, {
      className: s.id
    })), s;
  }
  initWithParentProps(e) {
    const t = this.parent?.getOverrides(e);
    if (!t)
      return;
    const s = this.#r(t);
    return this.store.updateOverrides(e, s), s;
  }
}
class B {
  #e;
  #t;
  constructor() {
    this.#t = /* @__PURE__ */ new Set(), this.subscribe = this.subscribe.bind(this), this.getAllStyles = this.getAllStyles.bind(this), this.updateOverrides = this.updateOverrides.bind(this);
  }
  subscribe(e) {
    return this.#t.add(e), () => {
      this.#t.delete(e);
    };
  }
  getScopedStylesStore(e) {
    return {
      subscribe: this.getScopedStylesStore.bind(this),
      getScopedStylesStore: this.getScopedStylesStore.bind(this),
      getAllStyles: this.getAllStyles.bind(this),
      getOverrides: (s) => this.getOverrides(`${s}${e}`),
      updateOverrides: (s, i) => {
        this.updateOverrides(`${s}${e}`, i);
      }
    };
  }
  getOverrides(e) {
    return this.#e?.[e];
  }
  getAllStyles() {
    return this.#e;
  }
  updateOverrides(e, t) {
    if (t === void 0) {
      const { [e]: s, ...i } = this.#e;
      this.#e = Object.keys(i).length === 0 ? void 0 : i;
    } else
      this.#e = { ...this.#e, [e]: t };
    this.#t.forEach((s) => s());
  }
}
const S = (r) => "item-" + r;
class p {
  #e;
  #t;
  #s;
  #r;
  #o;
  #i;
  constructor(e, t) {
    this.#e = new a(e), this.#t = new u(), this.#s = new B(), this.#o = t, this.#r = /* @__PURE__ */ new Map(), this.#i = /* @__PURE__ */ new Map();
  }
  get props() {
    return this.#e;
  }
  get scopedStores() {
    return this.#r;
  }
  get deletedComponents() {
    return this.#t;
  }
  get styles() {
    return this.#s;
  }
  get scopeId() {
    return this.#o;
  }
  createScopedStore(e, t, s) {
    const i = this.#r.get(e);
    if (i)
      return i;
    const o = new p({}, e);
    o.#e = this.#e.createChildStore({ className: S(e) });
    for (const c in t)
      o.props.initWithParentProps(c), o.props.updateOverrides(c, t[c]);
    return o.#t = this.#t.createChildStore(), o.#s = this.#s.getScopedStylesStore(`.${S(e)}`), this.#r.set(e, o), this.#i.set(e, s), o;
  }
  disposeScopedStore(e) {
    this.#i.get(e)?.(), this.#r.delete(e);
  }
}
const E = new p(), h = n.createContext(E), R = ({ itemIds: r, children: e }) => r.map((t) => n.createElement(y, { itemId: t, key: t }, e)), y = ({ children: r, itemId: e }) => {
  const t = b(h), s = P(() => t.createScopedStore(e), [t, e]);
  return v(() => () => {
    t.disposeScopedStore(e);
  }, [e, t]), n.createElement(h.Provider, { value: s }, r);
}, L = (r) => {
  let e = "";
  for (const [t, s] of Object.entries(r)) {
    e += `.${t} {`;
    for (const [i, o] of Object.entries(s))
      e += `${i}: ${o};`;
    e += "}";
  }
  return e;
}, x = ({ styles: r }) => {
  const e = O(r.subscribe, () => r.getAllStyles(), () => r.getAllStyles());
  return e ? n.createElement("style", { "data-testid": "style-overrides" }, L(e)) : null;
}, T = ({ createState: r, VeloRuntime: e }) => (s) => {
  const i = w(), { stateStore: o, velo: c } = s.stateStore ? s : r(s, i);
  return n.createElement(
    n.Fragment,
    null,
    n.createElement(h.Provider, { value: o }, e ? n.createElement(e, { ...s, ...c }, s.children) : s.children),
    n.createElement(x, { styles: o.styles })
  );
}, F = (r) => {
  const e = b(h), [, t] = N(0), s = () => t((d) => d + 1), i = e.props.getOverrides(r);
  v(() => {
    const d = e.props.subscribeByNickname(r, s);
    return () => {
      d();
    };
  }, [r]);
  const o = O((d) => e.deletedComponents.subscribeByNickname(r, d), () => e.deletedComponents.getOverrides(r), () => e.deletedComponents.getOverrides(r));
  return [i, !!o, { updateProps: (d) => {
    e.props.updateOverrides(r, d);
  }, updateStyles: (d) => {
    const f = e.props.getOverrides(r)?.id;
    e.styles.updateOverrides(f, d);
  } }];
}, $ = (r) => ({ scopeId: e }) => n.createElement(y, { itemId: e, key: e }, r({ scopeId: e })), z = (r) => m($(r), [r]);
export {
  y as ContextualStateProvider,
  R as Repeat,
  h as StateContext,
  p as StateStoreImpl,
  T as createStateProvider,
  E as stateStore,
  z as useRepeaterChildren,
  F as useStateOverrides
};
//# sourceMappingURL=builder-state.mjs.map

}