// packages/block-editor/src/components/inserter/media-tab/utils.js
import { createBlock } from "@wordpress/blocks";
import { jsx } from "react/jsx-runtime";
var mediaTypeTag = { image: "img", video: "video", audio: "audio" };
function getBlockAndPreviewFromMedia(media, mediaType) {
  const attributes = {
    id: media.id || void 0,
    caption: media.caption || void 0
  };
  const mediaSrc = media.url;
  const alt = media.alt || void 0;
  if (mediaType === "image") {
    attributes.url = mediaSrc;
    attributes.alt = alt;
  } else if (["video", "audio"].includes(mediaType)) {
    attributes.src = mediaSrc;
  }
  const PreviewTag = mediaTypeTag[mediaType];
  const preview = /* @__PURE__ */ jsx(
    PreviewTag,
    {
      src: media.previewUrl || mediaSrc,
      alt,
      controls: mediaType === "audio" ? true : void 0,
      inert: "true",
      onError: ({ currentTarget }) => {
        if (currentTarget.src === media.previewUrl) {
          currentTarget.src = mediaSrc;
        }
      }
    }
  );
  return [createBlock(`core/${mediaType}`, attributes), preview];
}
export {
  getBlockAndPreviewFromMedia
};
//# sourceMappingURL=utils.js.map
