"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/block-list/use-block-props/use-block-refs.js
var use_block_refs_exports = {};
__export(use_block_refs_exports, {
  useBlockElement: () => useBlockElement,
  useBlockElementRef: () => useBlockElementRef,
  useBlockRefProvider: () => useBlockRefProvider
});
module.exports = __toCommonJS(use_block_refs_exports);
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_block_refs_provider = require("../../provider/block-refs-provider");
function useBlockRefProvider(clientId) {
  const { refsMap } = (0, import_element.useContext)(import_block_refs_provider.BlockRefs);
  return (0, import_compose.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 } = (0, import_element.useContext)(import_block_refs_provider.BlockRefs);
  (0, import_element.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] = (0, import_element.useState)(null);
  useBlockElementRef(clientId, setBlockElement);
  return blockElement;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBlockElement,
  useBlockElementRef,
  useBlockRefProvider
});
//# sourceMappingURL=use-block-refs.js.map
