// packages/block-editor/src/components/rich-text/event-listeners/delete.js
import { DELETE, BACKSPACE } from "@wordpress/keycodes";
import { isCollapsed, isEmpty } from "@wordpress/rich-text";
import { privateApis as composePrivateApis } from "@wordpress/compose";
import { unlock } from "../../../lock-unlock.mjs";
var { subscribeDelegatedListener } = unlock(composePrivateApis);
var delete_default = (props) => (element) => {
  function onKeyDown(event) {
    const { keyCode } = event;
    if (event.defaultPrevented) {
      return;
    }
    const { value, onMerge, onRemove } = props.current;
    if (keyCode === DELETE || keyCode === BACKSPACE) {
      const { start, end, text } = value;
      const isReverse = keyCode === BACKSPACE;
      const hasActiveFormats = value.activeFormats && !!value.activeFormats.length;
      if (!isCollapsed(value) || hasActiveFormats || isReverse && start !== 0 || !isReverse && end !== text.length) {
        return;
      }
      if (onMerge) {
        onMerge(!isReverse);
      } else if (onRemove && isEmpty(value) && isReverse) {
        onRemove(!isReverse);
      }
      event.preventDefault();
    }
  }
  return subscribeDelegatedListener(element, "keydown", onKeyDown);
};
export {
  delete_default as default
};
//# sourceMappingURL=delete.mjs.map
