import p from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import R from "../../../rc-util/es/hooks/useEvent.mjs";
import x from "../../../rc-util/es/hooks/useLayoutEffect.mjs";
import S from "../../../rc-util/es/isEqual.mjs";
import * as u from "react";
import { r as C } from "../../../react-dom/index.mjs";
function P(r) {
  var e = /* @__PURE__ */ u.createContext(void 0), f = function(i) {
    var n = i.value, l = i.children, a = u.useRef(n);
    a.current = n;
    var d = u.useState(function() {
      return {
        getValue: function() {
          return a.current;
        },
        listeners: /* @__PURE__ */ new Set()
      };
    }), m = p(d, 1), c = m[0];
    return x(function() {
      C.unstable_batchedUpdates(function() {
        c.listeners.forEach(function(t) {
          t(n);
        });
      });
    }, [n]), /* @__PURE__ */ u.createElement(e.Provider, {
      value: c
    }, l);
  };
  return {
    Context: e,
    Provider: f,
    defaultValue: r
  };
}
function $(r, e) {
  var f = R(typeof e == "function" ? e : function(t) {
    if (e === void 0)
      return t;
    if (!Array.isArray(e))
      return t[e];
    var v = {};
    return e.forEach(function(s) {
      v[s] = t[s];
    }), v;
  }), o = u.useContext(r == null ? void 0 : r.Context), i = o || {}, n = i.listeners, l = i.getValue, a = u.useRef();
  a.current = f(o ? l() : r == null ? void 0 : r.defaultValue);
  var d = u.useState({}), m = p(d, 2), c = m[1];
  return x(function() {
    if (!o)
      return;
    function t(v) {
      var s = f(v);
      S(a.current, s, !0) || c({});
    }
    return n.add(t), function() {
      n.delete(t);
    };
  }, [o]), a.current;
}
export {
  P as createContext,
  $ as useContext
};
