import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { TIPTAP_CAPTION_TYPE } from "../consts.js";
const findAllMediaNodes = (root) => {
  const types = [Node_Type.IMAGE, Node_Type.VIDEO, Node_Type.GALLERY, Node_Type.GIF];
  const nodes = root.nodes ?? [];
  const result = [];
  const stack = [...nodes];
  while (stack.length) {
    const node = stack.pop();
    if (!node) {
      continue;
    }
    if (types.includes(node.type)) {
      result.push(node);
    }
    if (node.nodes) {
      stack.push(...node.nodes);
    }
  }
  return result;
};
const extractUrl = (source, type, urlResolver) => {
  if (!source) {
    return;
  }
  const { url, id } = source;
  try {
    if (url) {
      new URL(url);
      return url;
    }
  } catch {
  }
  const sourceId = id || url;
  if (sourceId) {
    const subdomain = type === "media" ? "static" : "video";
    return urlResolver ? urlResolver(sourceId) : `https://${subdomain}.wixstatic.com/${type}/${sourceId.replace(`${type}/`, "")}`;
  }
};
const extractImageUrl = (image, urlResolver) => extractUrl(image, "media", urlResolver);
const extractVideoUrl = (video, urlResolver) => extractUrl(video, "video", urlResolver);
const imageNodeToImageMediaConverter = (node, urlResolver) => {
  if (node.type !== Node_Type.IMAGE) {
    return;
  }
  const captionPlainText = captionToPlainText(node);
  return {
    type: "image",
    url: extractImageUrl(node.imageData?.image?.src, urlResolver),
    width: node.imageData?.image?.width,
    height: node.imageData?.image?.height,
    alt: node.imageData?.altText,
    caption: captionPlainText ?? node.imageData?.caption
  };
};
const gifNodeToVideoMediaConverter = (gifData, caption) => {
  return {
    type: "video",
    url: gifData.original?.mp4 ?? gifData.downsized?.mp4,
    thumbnailUrl: gifData.original?.still,
    width: gifData.width,
    height: gifData.height,
    ...caption ? { caption } : {}
  };
};
const gifNodeToImageMediaConverter = (gifData, caption) => {
  return {
    type: "image",
    url: gifData.original?.gif ?? gifData.downsized?.gif,
    width: gifData.width,
    height: gifData.height,
    ...caption ? { caption } : {}
  };
};
const gifNodeToMediaConverter = (node) => {
  if (node.type !== Node_Type.GIF) {
    return;
  }
  const captionPlainText = captionToPlainText(node);
  if (node.gifData?.original?.mp4 || node.gifData?.downsized?.mp4) {
    return gifNodeToVideoMediaConverter(node.gifData, captionPlainText);
  }
  if (node.gifData?.original?.gif || node.gifData?.downsized?.gif) {
    return gifNodeToImageMediaConverter(node.gifData, captionPlainText);
  }
};
const videoNodeToMediaConverter = (node, urlResolvers) => {
  if (node.type !== Node_Type.VIDEO) {
    return;
  }
  const caption = captionToPlainText(node);
  return {
    type: "video",
    url: extractVideoUrl(node.videoData?.video?.src, urlResolvers?.video),
    thumbnailUrl: extractImageUrl(node.videoData?.thumbnail?.src, urlResolvers?.thumbnail),
    width: node.videoData?.video?.width,
    height: node.videoData?.video?.height,
    duration: node.videoData?.video?.duration,
    title: node.videoData?.title,
    ...caption ? { caption } : {}
  };
};
const galleryImageToMediaConverter = (image, urlResolver) => {
  return {
    type: "image",
    url: extractImageUrl(image?.media?.src, urlResolver),
    width: image?.media?.width,
    height: image?.media?.height
  };
};
const galleryVideoToMediaConverter = (video, urlResolvers) => {
  return {
    type: "video",
    url: extractVideoUrl(video?.media?.src, urlResolvers?.video),
    width: video?.media?.width,
    height: video?.media?.height,
    duration: video?.media?.duration,
    thumbnailUrl: extractImageUrl(video?.thumbnail?.src, urlResolvers?.thumbnail)
  };
};
const galleryNodeToMediaConverter = (node, urlResolvers) => {
  if (node.type !== Node_Type.GALLERY) {
    return [];
  }
  const items = node.galleryData?.items || [];
  return items.map((item) => {
    if (item.image) {
      return galleryImageToMediaConverter(item.image, urlResolvers?.image);
    }
    if (item.video) {
      return galleryVideoToMediaConverter(item.video, urlResolvers);
    }
  }).filter(Boolean);
};
const getMediaData = (content, mediaUrlResolvers) => {
  const allNodes = findAllMediaNodes(content);
  return allNodes.map((node) => {
    switch (node.type) {
      case Node_Type.IMAGE:
        return imageNodeToImageMediaConverter(node, mediaUrlResolvers?.image);
      case Node_Type.GIF:
        return gifNodeToMediaConverter(node);
      case Node_Type.VIDEO:
        return videoNodeToMediaConverter(node, mediaUrlResolvers);
      case Node_Type.GALLERY:
        return galleryNodeToMediaConverter(node, mediaUrlResolvers);
      default:
        return void 0;
    }
  }).flat().filter(Boolean);
};
const captionToPlainText = (node) => {
  const captionNode = node.nodes?.find(({ type }) => type === TIPTAP_CAPTION_TYPE);
  return captionNode?.nodes?.reduce(
    (acc, { textData }) => textData?.text !== void 0 ? acc + textData?.text : acc,
    ""
  ) || void 0;
};
export {
  getMediaData
};
//# sourceMappingURL=get-media-data.js.map
