// packages/block-editor/src/components/block-selection-clearer/index.js
import { useSelect, useDispatch } from "@wordpress/data";
import { useRefEffect } from "@wordpress/compose";
import { store as blockEditorStore } from "../../store";
import { jsx } from "react/jsx-runtime";
function useBlockSelectionClearer() {
  const { getSettings, hasSelectedBlock, hasMultiSelection } = useSelect(blockEditorStore);
  const { clearSelectedBlock } = useDispatch(blockEditorStore);
  const { clearBlockSelection: isEnabled } = getSettings();
  return useRefEffect(
    (node) => {
      if (!isEnabled) {
        return;
      }
      function onMouseDown(event) {
        if (!hasSelectedBlock() && !hasMultiSelection()) {
          return;
        }
        if (event.target !== node) {
          return;
        }
        clearSelectedBlock();
      }
      node.addEventListener("mousedown", onMouseDown);
      return () => {
        node.removeEventListener("mousedown", onMouseDown);
      };
    },
    [hasSelectedBlock, hasMultiSelection, clearSelectedBlock, isEnabled]
  );
}
function BlockSelectionClearer(props) {
  return /* @__PURE__ */ jsx("div", { ref: useBlockSelectionClearer(), ...props });
}
export {
  BlockSelectionClearer as default,
  useBlockSelectionClearer
};
//# sourceMappingURL=index.js.map
