// packages/block-library/src/embed/transforms.js
import { createBlock } from "@wordpress/blocks";
import { isURL, getFilename } from "@wordpress/url";
import metadata from "./block.json";
import {
  findMoreSuitableBlock,
  rewriteXToTwitter,
  removeAspectRatioClasses
} from "./util.mjs";
var { name: EMBED_BLOCK } = metadata;
var transforms = {
  from: [
    {
      type: "raw",
      isMatch: (node) => {
        if (node.nodeName !== "P") {
          return false;
        }
        const trimmed = node.textContent.trim();
        if (!isURL(trimmed) || !/^https:\/\//i.test(trimmed) || trimmed.match(/https:\/\//gi)?.length !== 1) {
          return false;
        }
        return !/\.(?!(html?|php)$)[a-z0-9]+$/i.test(
          getFilename(trimmed) || ""
        );
      },
      transform: (node) => {
        const url = rewriteXToTwitter(node.textContent.trim());
        return createBlock(EMBED_BLOCK, {
          url,
          ...findMoreSuitableBlock(url)?.attributes
        });
      }
    },
    {
      type: "shortcode",
      tag: "embed",
      transform: (_attrs, { shortcode }) => {
        const url = rewriteXToTwitter(shortcode.content?.trim());
        return createBlock(EMBED_BLOCK, {
          url,
          ...findMoreSuitableBlock(url)?.attributes
        });
      }
    }
  ],
  to: [
    {
      type: "block",
      blocks: ["core/paragraph"],
      isMatch: ({ url }) => !!url,
      transform: ({ url, caption, className }) => {
        let value = `<a href="${url}">${url}</a>`;
        if (caption?.trim()) {
          value += `<br />${caption}`;
        }
        return createBlock("core/paragraph", {
          content: value,
          className: removeAspectRatioClasses(className)
        });
      }
    }
  ]
};
var transforms_default = transforms;
export {
  transforms_default as default
};
//# sourceMappingURL=transforms.mjs.map
