// packages/components/src/context/context-system-provider.js
import deepmerge from "deepmerge";
import fastDeepEqual from "fast-deep-equal/es6";
import { isPlainObject } from "is-plain-object";
import { createContext, useContext, useRef, useMemo, memo } from "@wordpress/element";
import warn from "@wordpress/warning";
import { useUpdateEffect } from "../utils/index.js";
import { jsx as _jsx } from "react/jsx-runtime";
var ComponentsContext = createContext(
  /** @type {Record<string, any>} */
  {}
);
ComponentsContext.displayName = "ComponentsContext";
var useComponentsContext = () => useContext(ComponentsContext);
function useContextSystemBridge({
  value
}) {
  const parentContext = useComponentsContext();
  const valueRef = useRef(value);
  useUpdateEffect(() => {
    if (
      // Objects are equivalent.
      fastDeepEqual(valueRef.current, value) && // But not the same reference.
      valueRef.current !== value
    ) {
      globalThis.SCRIPT_DEBUG === true ? warn(`Please memoize your context: ${JSON.stringify(value)}`) : void 0;
    }
  }, [value]);
  const config = useMemo(() => {
    return deepmerge(parentContext !== null && parentContext !== void 0 ? parentContext : {}, value !== null && value !== void 0 ? value : {}, {
      isMergeableObject: isPlainObject
    });
  }, [parentContext, value]);
  return config;
}
var BaseContextSystemProvider = ({
  children,
  value
}) => {
  const contextValue = useContextSystemBridge({
    value
  });
  return /* @__PURE__ */ _jsx(ComponentsContext.Provider, {
    value: contextValue,
    children
  });
};
var ContextSystemProvider = memo(BaseContextSystemProvider);
export {
  ComponentsContext,
  ContextSystemProvider,
  useComponentsContext
};
//# sourceMappingURL=context-system-provider.js.map
