"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/paste-handler.js
var paste_handler_exports = {};
__export(paste_handler_exports, {
  default: () => paste_handler_default
});
module.exports = __toCommonJS(paste_handler_exports);
var import_blocks = require("@wordpress/blocks");
var import_rich_text = require("@wordpress/rich-text");
var import_url = require("@wordpress/url");
var import_compose = require("@wordpress/compose");
var import_store = require("../../../store/index.cjs");
var import_utils = require("../utils.cjs");
var import_pasting = require("../../../utils/pasting.cjs");
var import_lock_unlock = require("../../../lock-unlock.cjs");
var { subscribeDelegatedListener } = (0, import_lock_unlock.unlock)(import_compose.privateApis);
var paste_handler_default = (props) => (element) => {
  function _onPaste(event) {
    const {
      disableFormats,
      onChange,
      value,
      formatTypes,
      tagName,
      onReplace,
      __unstableEmbedURLOnPaste,
      preserveWhiteSpace,
      pastePlainText,
      registry
    } = props.current;
    if (!element.contains(event.target)) {
      return;
    }
    if (event.defaultPrevented) {
      return;
    }
    const { plainText, html } = (0, import_pasting.getPasteEventData)(event);
    event.preventDefault();
    window.console.log("Received HTML (RichText):\n\n", html);
    window.console.log("Received plain text (RichText):\n\n", plainText);
    if (disableFormats) {
      onChange((0, import_rich_text.insert)(value, plainText));
      return;
    }
    const isInternal = event.clipboardData.getData("rich-text") === "true";
    function pasteInline(content2) {
      const transformed = formatTypes.reduce(
        (accumulator, { __unstablePasteRule }) => {
          if (__unstablePasteRule && accumulator === value) {
            accumulator = __unstablePasteRule(value, {
              html,
              plainText
            });
          }
          return accumulator;
        },
        value
      );
      if (transformed !== value) {
        onChange(transformed);
      } else {
        const valueToInsert = (0, import_rich_text.create)({ html: content2 });
        (0, import_utils.addActiveFormats)(valueToInsert, value.activeFormats);
        onChange((0, import_rich_text.insert)(value, valueToInsert));
      }
    }
    if (isInternal) {
      pasteInline(html);
      return;
    }
    if (pastePlainText) {
      onChange((0, import_rich_text.insert)(value, (0, import_rich_text.create)({ text: plainText })));
      return;
    }
    let mode = "INLINE";
    const trimmedPlainText = plainText.trim();
    if (__unstableEmbedURLOnPaste && (0, import_rich_text.isEmpty)(value) && (0, import_url.isURL)(trimmedPlainText) && // For the link pasting feature, allow only http(s) protocols.
    /^https?:/.test(trimmedPlainText)) {
      mode = "BLOCKS";
    }
    const content = (0, import_blocks.pasteHandler)({
      HTML: html,
      plainText,
      mode,
      tagName,
      preserveWhiteSpace
    });
    if (typeof content === "string") {
      pasteInline(content);
      return;
    }
    if (!content.length || !onReplace || !(0, import_rich_text.isEmpty)(value)) {
      return;
    }
    if (mode === "BLOCKS") {
      pasteInline(html);
      registry.dispatch(import_store.store).__unstableMarkLastChangeAsPersistent();
    }
    onReplace(content, content.length - 1, -1);
  }
  const { defaultView } = element.ownerDocument;
  return subscribeDelegatedListener(defaultView, "paste", _onPaste);
};
//# sourceMappingURL=paste-handler.cjs.map
