"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 active_uploads_exports = {};
__export(active_uploads_exports, {
  filterUploadingNodes: () => filterUploadingNodes,
  hasActiveUploads: () => hasActiveUploads
});
module.exports = __toCommonJS(active_uploads_exports);
var import_ricos_types = require("./../../ricos-types/index.js");
const isUploadingAudioRich = (node) => node.type === import_ricos_types.Node_Type.AUDIO && (!node?.audioData?.audio?.src || node.audioData.audio.src.id?.startsWith?.("blob:"));
const isUploadingAudioPm = (node) => {
  if (node.type === import_ricos_types.Node_Type.AUDIO) {
    const pmNode = node;
    return pmNode.attrs.loading;
  }
  return false;
};
const isUploadingImageRich = (node) => node.type === import_ricos_types.Node_Type.IMAGE && !node?.imageData?.image?.src;
const isUploadingImagePm = (node) => {
  if (node.type === import_ricos_types.Node_Type.IMAGE) {
    const pmNode = node;
    return pmNode.attrs.loading;
  }
  return false;
};
const isUploadingLayoutMediaPm = (node) => {
  if (node.type === import_ricos_types.Node_Type.LAYOUT) {
    const pmNode = node;
    return pmNode.attrs.loading?.backgroundImage || pmNode.attrs.loading?.backdropImage;
  }
  return false;
};
const isGeneratingAiWidget = (node) => {
  if (node.type === import_ricos_types.Node_Type.HTML) {
    const pmNode = node;
    return pmNode.attrs.state?.status === "LOADING";
  }
  return false;
};
const isUploadingVideoRich = (node) => node.type === import_ricos_types.Node_Type.VIDEO && (!node?.videoData?.video?.src || node.videoData.video.src.url?.startsWith?.("blob:"));
const isUploadingVideoPm = (node) => {
  if (node.type === import_ricos_types.Node_Type.VIDEO) {
    const pmNode = node;
    return pmNode.attrs.loading;
  }
  return false;
};
const isUploadingFileRich = (node) => {
  return node.type === import_ricos_types.Node_Type.FILE && !node?.fileData?.src;
};
const isUploadingFilePm = (node) => {
  if (node.type === import_ricos_types.Node_Type.FILE) {
    const pmNode = node;
    return pmNode.attrs.loading;
  }
  return false;
};
const isUploadedMedia = (media) => media?.src?.url || media?.src?.id;
const isUploadingGalleryRich = (node) => {
  return node.type === import_ricos_types.Node_Type.GALLERY && node.galleryData?.items?.some(
    (galleryItem) => "image" in galleryItem && !isUploadedMedia(galleryItem.image?.media) || "video" in galleryItem && !isUploadedMedia(galleryItem.video?.media)
  );
};
const isUploadingGalleryPm = (node) => {
  if (node.type === import_ricos_types.Node_Type.GALLERY) {
    const pmNode = node;
    return pmNode.attrs.items.some((item) => item.loading);
  }
  return false;
};
const getAllNestedPmNodes = (pmContent) => {
  const nodes = [];
  const nodesToVisit = [...pmContent?.content ?? []];
  while (nodesToVisit.length) {
    const node = nodesToVisit.pop();
    if (node) {
      nodes.push(node);
    }
    if (node?.content?.length) {
      nodesToVisit.push(...node.content);
    }
  }
  return nodes;
};
const hasActiveUploads = (pmContent) => {
  const allPmNodes = getAllNestedPmNodes(pmContent);
  return allPmNodes.some(
    (node) => isUploadingVideoPm(node) || isUploadingAudioPm(node) || isUploadingFilePm(node) || isUploadingGalleryPm(node) || isUploadingImagePm(node) || isUploadingLayoutMediaPm(node) || isGeneratingAiWidget(node)
  );
};
const removeUploadingGalleryItems = (node) => ({
  ...node,
  galleryData: {
    ...node.galleryData,
    items: node.galleryData?.items?.filter(
      (item) => item.image ? isUploadedMedia(item.image.media) : item.video ? isUploadedMedia(item.video.media) : item
    ) || []
  }
});
const filterUploadingNodes = (nodes) => nodes.map(cleanUpUploadingNode).filter(Boolean);
const cleanUpUploadingNode = (node) => isUploadingAudioRich(node) || isUploadingFileRich(node) || isUploadingImageRich(node) || isUploadingVideoRich(node) ? void 0 : isUploadingGalleryRich(node) ? removeUploadingGalleryItems(node) : node.nodes?.length ? {
  ...node,
  nodes: filterUploadingNodes(node.nodes)
} : node;
//# sourceMappingURL=active-uploads.js.map
