// packages/block-editor/src/components/block-editing-mode/index.js
import { useSelect, useRegistry } from "@wordpress/data";
import { useEffect } from "@wordpress/element";
import { store as blockEditorStore } from "../../store/index.mjs";
import {
  useBlockEditContext,
  blockEditingModeKey
} from "../block-edit/context.mjs";
function useBlockEditingMode(mode) {
  const context = useBlockEditContext();
  const { clientId = "" } = context;
  const registry = useRegistry();
  const globalBlockEditingMode = useSelect(
    (select) => (
      // Avoid adding the subscription if not needed!
      clientId ? null : select(blockEditorStore).getBlockEditingMode()
    ),
    [clientId]
  );
  useEffect(() => {
    if (!mode) {
      return;
    }
    const {
      setBlockEditingMode,
      unsetBlockEditingMode,
      __unstableMarkNextChangeAsNotPersistent
    } = registry.dispatch(blockEditorStore);
    __unstableMarkNextChangeAsNotPersistent();
    setBlockEditingMode(clientId, mode);
    return () => {
      __unstableMarkNextChangeAsNotPersistent();
      unsetBlockEditingMode(clientId);
    };
  }, [registry, clientId, mode]);
  return clientId ? context[blockEditingModeKey] : globalBlockEditingMode;
}
export {
  useBlockEditingMode
};
//# sourceMappingURL=index.mjs.map
