import { StateStoreImpl as f } from "@wix/builder-state";
export * from "@wix/builder-state";
import n, { useContext as v, useMemo as i, useEffect as y, useSyncExternalStore as m, useState as E, useCallback as C } from "react";
import { useGetService as g } from "@wix/services-manager-react";
const x = new f(), d = n.createContext(x), A = ({ itemIds: t, children: e }) => t.map((s) => n.createElement(O, { itemId: s, key: s }, e)), O = ({ children: t, itemId: e }) => {
  const s = v(d), c = i(() => s.createScopedStore(e), [s, e]);
  return y(() => () => {
    s.disposeScopedStore(e);
  }, [e, s]), n.createElement(d.Provider, { value: c }, t);
}, P = (t) => {
  let e = "";
  for (const [s, c] of Object.entries(t)) {
    e += `.${s} {`;
    for (const [l, o] of Object.entries(c))
      e += `${l}: ${o};`;
    e += "}";
  }
  return e;
}, h = ({ styles: t }) => {
  const e = m(t.subscribe, () => t.getAllStyles(), () => t.getAllStyles());
  return e ? n.createElement("style", { "data-testid": "style-overrides" }, P(e)) : null;
}, F = ({ createState: t, VeloRuntime: e }) => ({ subscribeToChanges: c, overrides: l, ...o }) => {
  const S = g(), { stateStore: a, velo: p } = o.stateStore ? o : i(() => t(o, S), []);
  return i(() => {
    c?.((r) => {
      Object.entries(r).forEach(([u, b]) => {
        a.props.updateOverrides(u, b);
      });
    }), Object.entries(l || {}).forEach(([r, u]) => a.props.updateOverrides(r, u));
  }, []), n.createElement(
    n.Fragment,
    null,
    n.createElement(d.Provider, { value: a }, e ? n.createElement(e, { ...o, ...o.velo ? o.velo : p }, o.children) : o.children),
    n.createElement(h, { styles: a.styles })
  );
}, N = (t) => {
  const e = v(d), [, s] = E(0), c = () => s((r) => r + 1), l = e.props.getOverrides(t);
  y(() => {
    const r = e.props.subscribeByNickname(t, c);
    return () => {
      r();
    };
  }, [t]);
  const o = m((r) => e.deletedComponents.subscribeByNickname(t, r), () => e.deletedComponents.getOverrides(t), () => e.deletedComponents.getOverrides(t));
  return [l, !!o, { updateProps: (r) => {
    e.props.updateOverrides(t, r);
  }, updateStyles: (r) => {
    const u = e.props.getOverrides(t)?.id;
    e.styles.updateOverrides(u, r);
  } }];
}, j = (t) => ({ scopeId: e }) => n.createElement(O, { itemId: e, key: e }, t({ scopeId: e })), w = (t) => C(j(t), [t]);
export {
  O as ContextualStateProvider,
  A as Repeat,
  d as StateContext,
  F as createStateProvider,
  x as stateStore,
  w as useRepeaterChildren,
  N as useStateOverrides
};
//# sourceMappingURL=builder-state-react.mjs.map