"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/before-input-rules.js
var before_input_rules_exports = {};
__export(before_input_rules_exports, {
  default: () => before_input_rules_default
});
module.exports = __toCommonJS(before_input_rules_exports);
var import_rich_text = require("@wordpress/rich-text");
var import_hooks = require("@wordpress/hooks");
var import_store = require("../../../store");
var wrapSelectionSettings = ["`", '"', "'", "\u201C\u201D", "\u2018\u2019"];
var before_input_rules_default = (props) => (element) => {
  function onInput(event) {
    const { inputType, data } = event;
    const { value, onChange, registry } = props.current;
    if (inputType !== "insertText") {
      return;
    }
    if ((0, import_rich_text.isCollapsed)(value)) {
      return;
    }
    const pair = (0, import_hooks.applyFilters)(
      "blockEditor.wrapSelectionSettings",
      wrapSelectionSettings
    ).find(
      ([startChar2, endChar2]) => startChar2 === data || endChar2 === data
    );
    if (!pair) {
      return;
    }
    const [startChar, endChar = startChar] = pair;
    const start = value.start;
    const end = value.end + startChar.length;
    let newValue = (0, import_rich_text.insert)(value, startChar, start, start);
    newValue = (0, import_rich_text.insert)(newValue, endChar, end, end);
    const {
      __unstableMarkLastChangeAsPersistent,
      __unstableMarkAutomaticChange
    } = registry.dispatch(import_store.store);
    __unstableMarkLastChangeAsPersistent();
    onChange(newValue);
    __unstableMarkAutomaticChange();
    const init = {};
    for (const key in event) {
      init[key] = event[key];
    }
    init.data = endChar;
    const { ownerDocument } = element;
    const { defaultView } = ownerDocument;
    const newEvent = new defaultView.InputEvent("input", init);
    window.queueMicrotask(() => {
      event.target.dispatchEvent(newEvent);
    });
    event.preventDefault();
  }
  element.addEventListener("beforeinput", onInput);
  return () => {
    element.removeEventListener("beforeinput", onInput);
  };
};
//# sourceMappingURL=before-input-rules.js.map
