"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/input-rules.js
var input_rules_exports = {};
__export(input_rules_exports, {
  default: () => input_rules_default,
  findSelection: () => findSelection
});
module.exports = __toCommonJS(input_rules_exports);
var import_rich_text = require("@wordpress/rich-text");
var import_blocks = require("@wordpress/blocks");
var import_compose = require("@wordpress/compose");
var import_store = require("../../../store/index.cjs");
var import_prevent_event_discovery = require("../prevent-event-discovery.cjs");
var import_selection = require("../../../utils/selection.cjs");
var import_lock_unlock = require("../../../lock-unlock.cjs");
var { subscribeDelegatedListener } = (0, import_lock_unlock.unlock)(import_compose.privateApis);
function findSelection(blocks) {
  let i = blocks.length;
  while (i--) {
    const attributeKey = (0, import_selection.retrieveSelectedAttribute)(
      blocks[i].attributes
    );
    if (attributeKey) {
      blocks[i].attributes[attributeKey] = blocks[i].attributes[attributeKey].toString().replace(import_selection.START_OF_SELECTED_AREA, "");
      return [blocks[i].clientId, attributeKey, 0, 0];
    }
    const nestedSelection = findSelection(blocks[i].innerBlocks);
    if (nestedSelection) {
      return nestedSelection;
    }
  }
  return [];
}
var input_rules_default = (props) => (element) => {
  function inputRule() {
    const { getValue, onReplace, selectionChange, registry } = props.current;
    if (!onReplace) {
      return;
    }
    const value = getValue();
    const { start, text } = value;
    const characterBefore = text.slice(start - 1, start);
    if (characterBefore !== " ") {
      return;
    }
    const trimmedTextBefore = text.slice(0, start).trim();
    const prefixTransforms = (0, import_blocks.getBlockTransforms)("from").filter(
      ({ type }) => type === "prefix"
    );
    const transformation = (0, import_blocks.findTransform)(
      prefixTransforms,
      ({ prefix }) => {
        return trimmedTextBefore === prefix;
      }
    );
    if (!transformation) {
      return;
    }
    const content = (0, import_rich_text.toHTMLString)({
      value: (0, import_rich_text.insert)(value, import_selection.START_OF_SELECTED_AREA, 0, start)
    });
    const block = transformation.transform(content);
    selectionChange(...findSelection([block]));
    onReplace([block]);
    registry.dispatch(import_store.store).__unstableMarkAutomaticChange();
    return true;
  }
  function onInput(event) {
    const { inputType, type } = event;
    const {
      getValue,
      onChange,
      __unstableAllowPrefixTransformations,
      formatTypes,
      registry,
      onReplace
    } = props.current;
    if (inputType !== "insertText" && type !== "compositionend") {
      return;
    }
    if (__unstableAllowPrefixTransformations && inputRule()) {
      return;
    }
    const value = getValue();
    const transforms = (0, import_blocks.getBlockTransforms)("from").filter(
      (transform) => transform.type === "input"
    );
    const transformation = (0, import_blocks.findTransform)(transforms, (item) => {
      return item.regExp.test(value.text);
    });
    if (transformation) {
      onReplace(transformation.transform());
      registry.dispatch(import_store.store).__unstableMarkAutomaticChange();
      return;
    }
    const transformed = formatTypes.reduce(
      (accumulator, { __unstableInputRule }) => {
        if (__unstableInputRule) {
          accumulator = __unstableInputRule(accumulator);
        }
        return accumulator;
      },
      (0, import_prevent_event_discovery.preventEventDiscovery)(value)
    );
    const {
      __unstableMarkLastChangeAsPersistent,
      __unstableMarkAutomaticChange
    } = registry.dispatch(import_store.store);
    if (transformed !== value) {
      __unstableMarkLastChangeAsPersistent();
      onChange({
        ...transformed,
        activeFormats: value.activeFormats
      });
      __unstableMarkAutomaticChange();
    }
  }
  const unsubscribeInput = subscribeDelegatedListener(
    element,
    "input",
    onInput,
    true
  );
  const unsubscribeCompositionEnd = subscribeDelegatedListener(
    element,
    "compositionend",
    onInput,
    true
  );
  return () => {
    unsubscribeInput();
    unsubscribeCompositionEnd();
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findSelection
});
//# sourceMappingURL=input-rules.cjs.map
