import { Node_Type } from "./../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { LINK_TYPE } from "../../../consts.js";
import { mergeTextNodes } from "../../draft/toDraft/decoration-parsers.js";
const parseTextNodes = (node, addTextLinks) => {
  const {
    text,
    decorationMap: { [LINK_TYPE]: linkDecorations }
  } = mergeTextNodes(node.nodes);
  const normalizedText = normalizeAdjacentWhitespace(text);
  const finalText = addTextLinks ? addLinksToText(normalizedText, linkDecorations) : normalizedText;
  return removeTrailingNewLine(finalText);
};
const listNodeToLines = (listNode, addTextLinks, depth = 0) => listNode.nodes?.flatMap((listItemNode, listItemIndex) => {
  return listItemNode.nodes?.flatMap((listItemNodeChild, index) => {
    if (listItemNodeChild.type === Node_Type.PARAGRAPH || listItemNodeChild.type === Node_Type.HEADING) {
      const text = parseTextNodes(listItemNodeChild, addTextLinks);
      if (index === 0) {
        if (listNode.type === Node_Type.ORDERED_LIST) {
          return "   ".repeat(depth) + (listItemIndex + 1) + ". " + text;
        }
        if (listNode.type === Node_Type.CHECKBOX_LIST) {
          return "   ".repeat(depth) + ` [${listItemNode.listItemData?.checked ? "x" : " "}] ` + text;
        }
        if (listNode.type === Node_Type.BULLETED_LIST) {
          return "   ".repeat(depth) + ` • ` + text;
        }
      }
      return "   ".repeat(depth + 1) + text;
    }
    if (listItemNodeChild.type === Node_Type.ORDERED_LIST || listItemNodeChild.type === Node_Type.BULLETED_LIST || listItemNodeChild.type === Node_Type.CHECKBOX_LIST) {
      return listNodeToLines(listItemNodeChild, addTextLinks, depth + 1);
    }
    return [];
  }) ?? [];
}) ?? [];
const parseListNode = (node, delimiter, addTextLinks) => listNodeToLines(node, addTextLinks).join(delimiter);
const addLinksToText = (text, linkDecorations = []) => linkDecorations.sort((a, b) => a.end - b.end).reduce(
  (newText, link) => insertInText(
    newText,
    link.end + newText.length - text.length + 1,
    `(${link.linkData?.link?.url})`
  ),
  text
);
const insertInText = (text, pos, insertedText) => text.substring(0, pos) + insertedText + text.substring(pos);
const removeTrailingNewLine = (text) => text.endsWith("\n") ? text.substring(0, text.length - 1) : text;
const normalizeAdjacentWhitespace = (text) => text.replace(/ \u00A0/g, " ").replace(/\u00A0 /g, " ");
const getImagePlainTextUrl = (src = {}) => {
  const id = src.id || src.custom;
  if (id) {
    return `https://static.wixstatic.com/media/${id.replace("media/", "")}`;
  }
  return src.url || "";
};
function* parseImage({ imageData }, delimiter) {
  const { caption } = imageData || {};
  const { src } = imageData?.image || {};
  const imageUrl = getImagePlainTextUrl(src);
  if (!imageUrl) {
    return caption || "";
  }
  const url = yield {
    command: "getImageUrl",
    data: imageUrl
  };
  return [caption, url].filter(Boolean).join(delimiter);
}
const getDefaultVideoUrl = (fileId) => `https://video.wixstatic.com/${fileId}`;
const getDefaultAudioUrl = (fileId) => `https://static.wixstatic.com/${fileId}`;
function* parseVideo({ videoData }, delimiter) {
  const { video, title } = videoData || {};
  const { custom, id, url } = video?.src || {};
  const vidId = id || custom;
  const vidUrl = url || "";
  let videoUrl;
  if (vidId) {
    videoUrl = yield {
      command: "getVideoUrl",
      data: vidId
    };
  } else {
    videoUrl = yield {
      command: "getVideoEmbeddedUrl",
      data: vidUrl
    };
  }
  return title ? title + delimiter + videoUrl : videoUrl;
}
function* parseAudio({ audioData }, delimiter) {
  const { audio, name } = audioData || {};
  const { id, url } = audio?.src || {};
  let audioUrl = url || "";
  if (id) {
    audioUrl = yield {
      command: "getAudioUrl",
      data: id
    };
  }
  return name ? name + delimiter + audioUrl : audioUrl;
}
function* parseGiphy({ gifData }) {
  let gifUrl = gifData?.original?.gif || "";
  if (gifUrl) {
    gifUrl = yield {
      command: "getGifUrl",
      data: gifUrl
    };
  }
  return gifUrl;
}
const parseAppEmbed = ({ appEmbedData }, delimiter) => {
  const { url, name } = appEmbedData || {};
  return [name, url].filter(Boolean).join(delimiter);
};
const parseLinkPreview = ({ linkPreviewData }) => {
  const { url } = linkPreviewData?.link || {};
  return url || "";
};
const parseEmbed = ({ embedData }, delimiter) => {
  return [embedData?.oembed?.title, embedData?.src].filter(Boolean).join(delimiter);
};
export {
  getDefaultAudioUrl,
  getDefaultVideoUrl,
  parseAppEmbed,
  parseAudio,
  parseEmbed,
  parseGiphy,
  parseImage,
  parseLinkPreview,
  parseListNode,
  parseTextNodes,
  parseVideo
};
//# sourceMappingURL=convert-nodes.js.map
