"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/components/src/context/context-system-provider.js
var context_system_provider_exports = {};
__export(context_system_provider_exports, {
  ComponentsContext: () => ComponentsContext,
  ContextSystemProvider: () => ContextSystemProvider,
  useComponentsContext: () => useComponentsContext
});
module.exports = __toCommonJS(context_system_provider_exports);
var import_deepmerge = __toESM(require("deepmerge"), 1);
var import_es6 = __toESM(require("fast-deep-equal/es6"), 1);
var import_is_plain_object = require("is-plain-object");
var import_element = require("@wordpress/element");
var import_warning = __toESM(require("@wordpress/warning"), 1);
var import_utils = require("../utils/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var ComponentsContext = (0, import_element.createContext)(
  /** @type {Record<string, any>} */
  {}
);
ComponentsContext.displayName = "ComponentsContext";
var useComponentsContext = () => (0, import_element.useContext)(ComponentsContext);
function useContextSystemBridge({
  value
}) {
  const parentContext = useComponentsContext();
  const valueRef = (0, import_element.useRef)(value);
  (0, import_utils.useUpdateEffect)(() => {
    if (
      // Objects are equivalent.
      (0, import_es6.default)(valueRef.current, value) && // But not the same reference.
      valueRef.current !== value
    ) {
      globalThis.SCRIPT_DEBUG === true ? (0, import_warning.default)(`Please memoize your context: ${JSON.stringify(value)}`) : void 0;
    }
  }, [value]);
  const config = (0, import_element.useMemo)(() => {
    return (0, import_deepmerge.default)(parentContext !== null && parentContext !== void 0 ? parentContext : {}, value !== null && value !== void 0 ? value : {}, {
      isMergeableObject: import_is_plain_object.isPlainObject
    });
  }, [parentContext, value]);
  return config;
}
var BaseContextSystemProvider = ({
  children,
  value
}) => {
  const contextValue = useContextSystemBridge({
    value
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ComponentsContext.Provider, {
    value: contextValue,
    children
  });
};
var ContextSystemProvider = (0, import_element.memo)(BaseContextSystemProvider);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ComponentsContext,
  ContextSystemProvider,
  useComponentsContext
});
//# sourceMappingURL=context-system-provider.cjs.map
