"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var convert_nodes_exports = {};
__export(convert_nodes_exports, {
  getDefaultAudioUrl: () => getDefaultAudioUrl,
  getDefaultVideoUrl: () => getDefaultVideoUrl,
  parseAppEmbed: () => parseAppEmbed,
  parseAudio: () => parseAudio,
  parseEmbed: () => parseEmbed,
  parseGiphy: () => parseGiphy,
  parseImage: () => parseImage,
  parseLinkPreview: () => parseLinkPreview,
  parseListNode: () => parseListNode,
  parseTextNodes: () => parseTextNodes,
  parseVideo: () => parseVideo
});
module.exports = __toCommonJS(convert_nodes_exports);
var import_v1 = require("./../../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_consts = require("../../../consts.js");
var import_decoration_parsers = require("../../draft/toDraft/decoration-parsers.js");
const parseTextNodes = (node, addTextLinks) => {
  const {
    text,
    decorationMap: { [import_consts.LINK_TYPE]: linkDecorations }
  } = (0, import_decoration_parsers.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 === import_v1.Node_Type.PARAGRAPH || listItemNodeChild.type === import_v1.Node_Type.HEADING) {
      const text = parseTextNodes(listItemNodeChild, addTextLinks);
      if (index === 0) {
        if (listNode.type === import_v1.Node_Type.ORDERED_LIST) {
          return "   ".repeat(depth) + (listItemIndex + 1) + ". " + text;
        }
        if (listNode.type === import_v1.Node_Type.CHECKBOX_LIST) {
          return "   ".repeat(depth) + ` [${listItemNode.listItemData?.checked ? "x" : " "}] ` + text;
        }
        if (listNode.type === import_v1.Node_Type.BULLETED_LIST) {
          return "   ".repeat(depth) + ` • ` + text;
        }
      }
      return "   ".repeat(depth + 1) + text;
    }
    if (listItemNodeChild.type === import_v1.Node_Type.ORDERED_LIST || listItemNodeChild.type === import_v1.Node_Type.BULLETED_LIST || listItemNodeChild.type === import_v1.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);
};
//# sourceMappingURL=convert-nodes.js.map
