// packages/block-library/src/list-item/hooks/use-space.js
import {
  useRefEffect,
  privateApis as composePrivateApis
} from "@wordpress/compose";
import { SPACE, TAB } from "@wordpress/keycodes";
import { store as blockEditorStore } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import useIndentListItem from "./use-indent-list-item.mjs";
import useOutdentListItem from "./use-outdent-list-item.mjs";
import { unlock } from "../../lock-unlock.mjs";
var { subscribeDelegatedListener } = unlock(composePrivateApis);
function useSpace(clientId) {
  const { getSelectionStart, getSelectionEnd, getBlockIndex } = useSelect(blockEditorStore);
  const indentListItem = useIndentListItem(clientId);
  const outdentListItem = useOutdentListItem();
  return useRefEffect(
    (element) => {
      function onKeyDown(event) {
        const { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event;
        if (event.defaultPrevented || keyCode !== SPACE && keyCode !== TAB || // Only override when no modifiers are pressed.
        altKey || metaKey || ctrlKey) {
          return;
        }
        const selectionStart = getSelectionStart();
        const selectionEnd = getSelectionEnd();
        if (selectionStart.offset === 0 && selectionEnd.offset === 0) {
          if (shiftKey) {
            if (keyCode === TAB) {
              if (outdentListItem()) {
                event.preventDefault();
              }
            }
          } else if (getBlockIndex(clientId) !== 0) {
            if (indentListItem()) {
              event.preventDefault();
            }
          }
        }
      }
      return subscribeDelegatedListener(
        element,
        "keydown",
        onKeyDown,
        true
      );
    },
    [clientId, indentListItem]
  );
}
export {
  useSpace as default
};
//# sourceMappingURL=use-space.mjs.map
