// packages/block-editor/src/components/block-list/use-block-props/use-focus-handler.js
import { useSelect, useDispatch } from "@wordpress/data";
import {
  useRefEffect,
  privateApis as composePrivateApis
} from "@wordpress/compose";
import { isInsideRootBlock } from "../../../utils/dom.mjs";
import { store as blockEditorStore } from "../../../store/index.mjs";
import { unlock } from "../../../lock-unlock.mjs";
var { subscribeDelegatedListener } = unlock(composePrivateApis);
function useFocusHandler(clientId) {
  const { isBlockSelected } = useSelect(blockEditorStore);
  const { selectBlock, selectionChange } = useDispatch(blockEditorStore);
  return useRefEffect(
    (node) => {
      function onFocus(event) {
        if (node.parentElement.closest('[contenteditable="true"]')) {
          return;
        }
        if (isBlockSelected(clientId)) {
          if (!event.target.isContentEditable) {
            selectionChange(clientId);
          }
          return;
        }
        if (!isInsideRootBlock(node, event.target)) {
          return;
        }
        selectBlock(clientId);
      }
      return subscribeDelegatedListener(node, "focusin", onFocus);
    },
    [isBlockSelected, selectBlock]
  );
}
export {
  useFocusHandler
};
//# sourceMappingURL=use-focus-handler.mjs.map
