// packages/block-editor/src/components/rich-text/event-listeners/paste-handler.js
import { pasteHandler } from "@wordpress/blocks";
import { isEmpty, insert, create } from "@wordpress/rich-text";
import { isURL } from "@wordpress/url";
import { privateApis as composePrivateApis } from "@wordpress/compose";
import { store as blockEditorStore } from "../../../store/index.mjs";
import { addActiveFormats } from "../utils.mjs";
import { getPasteEventData } from "../../../utils/pasting.mjs";
import { unlock } from "../../../lock-unlock.mjs";
var { subscribeDelegatedListener } = unlock(composePrivateApis);
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 } = 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(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 = create({ html: content2 });
        addActiveFormats(valueToInsert, value.activeFormats);
        onChange(insert(value, valueToInsert));
      }
    }
    if (isInternal) {
      pasteInline(html);
      return;
    }
    if (pastePlainText) {
      onChange(insert(value, create({ text: plainText })));
      return;
    }
    let mode = "INLINE";
    const trimmedPlainText = plainText.trim();
    if (__unstableEmbedURLOnPaste && isEmpty(value) && isURL(trimmedPlainText) && // For the link pasting feature, allow only http(s) protocols.
    /^https?:/.test(trimmedPlainText)) {
      mode = "BLOCKS";
    }
    const content = pasteHandler({
      HTML: html,
      plainText,
      mode,
      tagName,
      preserveWhiteSpace
    });
    if (typeof content === "string") {
      pasteInline(content);
      return;
    }
    if (!content.length || !onReplace || !isEmpty(value)) {
      return;
    }
    if (mode === "BLOCKS") {
      pasteInline(html);
      registry.dispatch(blockEditorStore).__unstableMarkLastChangeAsPersistent();
    }
    onReplace(content, content.length - 1, -1);
  }
  const { defaultView } = element.ownerDocument;
  return subscribeDelegatedListener(defaultView, "paste", _onPaste);
};
export {
  paste_handler_default as default
};
//# sourceMappingURL=paste-handler.mjs.map
