import { generateId, toSizeInKb } from "./../ricos-content/index.js";
import { ButtonData_Type, Decoration_Type, Node_Type } from "./../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { getValidColorValue } from "../ricos-content/utils.js";
import { filterUploadingNodes } from "./tiptap/active-uploads.js";
const isEmptyText = (node) => node.type === Node_Type.TEXT && !node.textData?.text;
const normalizeNodes = (nodes) => nodes.filter((node) => !isEmptyText(node)).map((originalNode) => {
  const node = { ...originalNode, id: originalNode.id || generateId() };
  if (node.type === Node_Type.TABLE_CELL) {
    return normalizeTableCellNode(node);
  }
  if (node.type === Node_Type.FILE) {
    return normalizeFileNode(node);
  }
  if (node.type === Node_Type.BUTTON) {
    return normalizeButtonNode(node);
  }
  if (node.type === Node_Type.IMAGE) {
    return normalizeImageNode(node);
  }
  return {
    ...node,
    nodes: Array.isArray(node.nodes) ? normalizeNodes(node.nodes) : node.nodes
  };
});
const normalizeTableCellNode = (node) => {
  return {
    ...node,
    nodes: Array.isArray(node.nodes) ? normalizeNodes(node.nodes) : node.nodes,
    tableCellData: node.tableCellData ? {
      ...node.tableCellData,
      borderColors: node.tableCellData.borderColors ? {
        left: getValidColorValue(node.tableCellData?.borderColors?.left),
        right: getValidColorValue(node.tableCellData?.borderColors?.right),
        top: getValidColorValue(node.tableCellData?.borderColors?.top),
        bottom: getValidColorValue(node.tableCellData?.borderColors?.bottom)
      } : node.tableCellData.borderColors
    } : node.tableCellData
  };
};
const normalizeFileNode = (node) => {
  const size = node.fileData?.size;
  if (size === void 0) {
    return node;
  }
  const FILE_MAX_SIZE_IN_BYTES = 2e9;
  return {
    ...node,
    fileData: {
      ...node.fileData,
      size: Math.min(size, FILE_MAX_SIZE_IN_BYTES),
      sizeInKb: toSizeInKb(size)
    }
  };
};
const normalizeButtonNode = (node) => {
  const borderRadius = node.buttonData?.styles?.border?.radius;
  const borderWidth = node.buttonData?.styles?.border?.width;
  if (borderRadius === void 0 && borderWidth === void 0) {
    return node;
  }
  const normalizeBorderValue = (value) => value !== void 0 ? Math.round(value) : void 0;
  const normalized = {
    ...node,
    buttonData: {
      type: ButtonData_Type.ACTION,
      // adding button type to resolve TS error
      ...node.buttonData,
      styles: {
        ...node.buttonData?.styles,
        border: {
          radius: normalizeBorderValue(borderRadius),
          width: normalizeBorderValue(borderWidth)
        }
      }
    }
  };
  return normalized;
};
const normalizeImageNode = (node) => {
  const caption = node.imageData?.caption;
  if (caption === "") {
    return {
      ...node,
      imageData: {
        ...node.imageData,
        caption: void 0
      }
    };
  }
  if (!caption || node.nodes?.length) {
    return node;
  }
  const normalized = {
    ...node,
    nodes: [
      {
        type: Node_Type.CAPTION,
        id: generateId(),
        nodes: [
          {
            type: Node_Type.TEXT,
            id: "",
            nodes: [],
            textData: {
              text: caption,
              // Ensures backwards compatibility. Previously it wasn't possible to have a spoiler without a caption.
              decorations: node.imageData?.containerData?.spoiler?.enabled ? [{ type: Decoration_Type.SPOILER }] : []
            }
          }
        ]
      }
    ]
  };
  return normalized;
};
const normalizeContent = (content) => ({
  ...content,
  nodes: normalizeNodes(filterUploadingNodes(content.nodes ?? []))
});
export {
  normalizeContent
};
//# sourceMappingURL=normalize-content.js.map
