import { definePmNodeConverter } from "./../../pm-converters/types.js";
import { TIPTAP_FIGURE_TYPE, TIPTAP_IMAGE_TYPE, generateId } from "./../../ricos-content/index.js";
import { Node_Type } from "./../../ricos-schema/next-schema/index.js";
import { fromTiptapLinkConverter } from "./../../_shared/link/link-converters.js";
import { captionToPlainText } from "./caption-to-plain-text.js";
const imageConverter = definePmNodeConverter({
  toPm: {
    types: [Node_Type.IMAGE],
    convert: (node, visit) => {
      const { containerData, caption: imageCaption, ...imageData } = node.imageData;
      const fallbackCaption = {
        id: generateId(),
        type: Node_Type.CAPTION,
        nodes: typeof imageCaption === "string" && imageCaption.length ? [
          {
            type: Node_Type.TEXT,
            id: "",
            nodes: [],
            textData: {
              text: imageCaption,
              decorations: []
            }
          }
        ] : []
      };
      const fallBackNodeWithCaption = { ...node, nodes: [fallbackCaption] };
      return {
        type: TIPTAP_FIGURE_TYPE,
        attrs: {
          id: generateId()
        },
        content: [
          {
            type: TIPTAP_IMAGE_TYPE,
            attrs: {
              ...imageData,
              containerData,
              id: node.id,
              disableDownload: !!imageData.disableDownload,
              disableExpand: !!imageData.disableExpand,
              decorative: !!imageData.decorative
            }
          },
          ...visit?.((node.nodes?.length ?? 0) > 0 ? node : fallBackNodeWithCaption) ?? []
        ]
      };
    }
  },
  fromPm: {
    types: [TIPTAP_FIGURE_TYPE, TIPTAP_IMAGE_TYPE],
    convert: (node, visit) => {
      if (node.type === TIPTAP_IMAGE_TYPE) {
        return void 0;
      }
      const imageNode = node.content?.find(({ type }) => type === TIPTAP_IMAGE_TYPE);
      if (!imageNode) {
        return void 0;
      }
      const { id, loading, error, ...imageData } = imageNode?.attrs || {};
      if (loading || error) {
        return void 0;
      }
      const [captionNode] = visit?.(node) ?? [];
      const captionPlainText = captionToPlainText(node);
      const { spoiler, ...rest } = imageData.containerData || {};
      const containerData = {
        ...rest,
        ...spoiler?.enabled ? { spoiler: { ...spoiler } } : {}
      };
      return {
        type: Node_Type.IMAGE,
        id: id ?? generateId(),
        nodes: captionPlainText ? [captionNode] : [],
        imageData: {
          ...imageData,
          caption: captionPlainText,
          altText: imageData.altText || void 0,
          decorative: imageData.decorative || void 0,
          disableDownload: imageData.disableDownload || void 0,
          disableExpand: imageData.disableExpand || void 0,
          containerData: Object.keys(containerData).length ? containerData : void 0,
          link: fromTiptapLinkConverter(imageData.link)
        }
      };
    }
  }
});
export {
  imageConverter
};
//# sourceMappingURL=image-converter.js.map
