// packages/block-editor/src/components/recursion-provider/index.js
import { createContext, useContext, useMemo } from "@wordpress/element";
import deprecated from "@wordpress/deprecated";
import { useBlockEditContext } from "../block-edit/context";
import { jsx } from "react/jsx-runtime";
var RenderedRefsContext = createContext({});
RenderedRefsContext.displayName = "RenderedRefsContext";
function addToBlockType(renderedBlocks, blockName, uniqueId) {
  const result = {
    ...renderedBlocks,
    [blockName]: renderedBlocks[blockName] ? new Set(renderedBlocks[blockName]) : /* @__PURE__ */ new Set()
  };
  result[blockName].add(uniqueId);
  return result;
}
function RecursionProvider({ children, uniqueId, blockName = "" }) {
  const previouslyRenderedBlocks = useContext(RenderedRefsContext);
  const { name } = useBlockEditContext();
  blockName = blockName || name;
  const newRenderedBlocks = useMemo(
    () => addToBlockType(previouslyRenderedBlocks, blockName, uniqueId),
    [previouslyRenderedBlocks, blockName, uniqueId]
  );
  return /* @__PURE__ */ jsx(RenderedRefsContext.Provider, { value: newRenderedBlocks, children });
}
function useHasRecursion(uniqueId, blockName = "") {
  const previouslyRenderedBlocks = useContext(RenderedRefsContext);
  const { name } = useBlockEditContext();
  blockName = blockName || name;
  return Boolean(previouslyRenderedBlocks[blockName]?.has(uniqueId));
}
var DeprecatedExperimentalRecursionProvider = (props) => {
  deprecated("wp.blockEditor.__experimentalRecursionProvider", {
    since: "6.5",
    alternative: "wp.blockEditor.RecursionProvider"
  });
  return /* @__PURE__ */ jsx(RecursionProvider, { ...props });
};
var DeprecatedExperimentalUseHasRecursion = (...args) => {
  deprecated("wp.blockEditor.__experimentalUseHasRecursion", {
    since: "6.5",
    alternative: "wp.blockEditor.useHasRecursion"
  });
  return useHasRecursion(...args);
};
export {
  DeprecatedExperimentalRecursionProvider,
  DeprecatedExperimentalUseHasRecursion,
  RecursionProvider,
  useHasRecursion
};
//# sourceMappingURL=index.js.map
