// packages/block-editor/src/components/block-list/use-block-props/use-block-refs.js
import { useContext, useState, useLayoutEffect } from "@wordpress/element";
import { useRefEffect } from "@wordpress/compose";
import { BlockRefs } from "../../provider/block-refs-provider";
function useBlockRefProvider(clientId) {
  const { refsMap } = useContext(BlockRefs);
  return useRefEffect(
    (element) => {
      refsMap.set(clientId, element);
      return () => refsMap.delete(clientId);
    },
    [clientId]
  );
}
function assignRef(ref, value) {
  if (typeof ref === "function") {
    ref(value);
  } else if (ref) {
    ref.current = value;
  }
}
function useBlockElementRef(clientId, ref) {
  const { refsMap } = useContext(BlockRefs);
  useLayoutEffect(() => {
    assignRef(ref, refsMap.get(clientId));
    const unsubscribe = refsMap.subscribe(
      clientId,
      () => assignRef(ref, refsMap.get(clientId))
    );
    return () => {
      unsubscribe();
      assignRef(ref, null);
    };
  }, [refsMap, clientId, ref]);
}
function useBlockElement(clientId) {
  const [blockElement, setBlockElement] = useState(null);
  useBlockElementRef(clientId, setBlockElement);
  return blockElement;
}
export {
  useBlockElement,
  useBlockElementRef,
  useBlockRefProvider
};
//# sourceMappingURL=use-block-refs.js.map
