"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/rich-text/event-listeners/delete.js
var delete_exports = {};
__export(delete_exports, {
  default: () => delete_default
});
module.exports = __toCommonJS(delete_exports);
var import_keycodes = require("@wordpress/keycodes");
var import_rich_text = require("@wordpress/rich-text");
var import_compose = require("@wordpress/compose");
var import_lock_unlock = require("../../../lock-unlock.cjs");
var { subscribeDelegatedListener } = (0, import_lock_unlock.unlock)(import_compose.privateApis);
var delete_default = (props) => (element) => {
  function onKeyDown(event) {
    const { keyCode } = event;
    if (event.defaultPrevented) {
      return;
    }
    const { value, onMerge, onRemove } = props.current;
    if (keyCode === import_keycodes.DELETE || keyCode === import_keycodes.BACKSPACE) {
      const { start, end, text } = value;
      const isReverse = keyCode === import_keycodes.BACKSPACE;
      const hasActiveFormats = value.activeFormats && !!value.activeFormats.length;
      if (!(0, import_rich_text.isCollapsed)(value) || hasActiveFormats || isReverse && start !== 0 || !isReverse && end !== text.length) {
        return;
      }
      if (onMerge) {
        onMerge(!isReverse);
      } else if (onRemove && (0, import_rich_text.isEmpty)(value) && isReverse) {
        onRemove(!isReverse);
      }
      event.preventDefault();
    }
  }
  return subscribeDelegatedListener(element, "keydown", onKeyDown);
};
//# sourceMappingURL=delete.cjs.map
