import { jsx } from "react/jsx-runtime";
import { $applyNodeReplacement, DecoratorNode, createEditor } from "lexical";
import * as __rspack_external_react from "react";
const InlineImageComponent = /*#__PURE__*/ __rspack_external_react.lazy(async ()=>await import("./inline-image-node-component.js"));
function convertInlineImageElement(domNode) {
    if (domNode instanceof HTMLImageElement) {
        const { alt: altText, src, width, height } = domNode;
        const node = $createInlineImageNode({
            targetDocumentId: domNode.dataset.id ?? '',
            targetCollectionId: '',
            targetCollectionPath: domNode.dataset.collection ?? '',
            src,
            altText,
            height,
            width
        });
        return {
            node
        };
    }
    return null;
}
class InlineImageNode extends DecoratorNode {
    static getType() {
        return 'inline-image';
    }
    static clone(node) {
        return new InlineImageNode(node.__relation, node.__src, node.__position, node.__altText, node.__width, node.__height, node.__showCaption, node.__caption, node.__key);
    }
    static importJSON(serializedNode) {
        const { src, position, altText, height, width, showCaption, caption, targetDocumentId, targetCollectionId, targetCollectionPath, document: document1 } = serializedNode;
        const node = $createInlineImageNode({
            targetDocumentId,
            targetCollectionId,
            targetCollectionPath,
            document: document1,
            src,
            position,
            altText,
            width,
            height,
            showCaption
        });
        const nestedEditor = node.__caption;
        const editorState = nestedEditor.parseEditorState(caption.editorState);
        if (!editorState.isEmpty()) nestedEditor.setEditorState(editorState);
        return node;
    }
    static importDOM() {
        return {
            img: (_node)=>({
                    conversion: convertInlineImageElement,
                    priority: 0
                })
        };
    }
    exportDOM() {
        const element = document.createElement('img');
        element.setAttribute('data-id', this.__relation.targetDocumentId);
        element.setAttribute('data-collection', this.__relation.targetCollectionPath);
        element.setAttribute('src', this.__src);
        element.setAttribute('alt', this.__altText);
        if (null != this.__width) element.setAttribute('width', this.__width.toString());
        if (null != this.__height) element.setAttribute('height', this.__height.toString());
        return {
            element
        };
    }
    exportJSON() {
        return {
            ...this.__relation,
            src: this.getSrc(),
            position: this.__position,
            altText: this.getAltText(),
            height: this.__height,
            width: this.__width,
            showCaption: this.__showCaption,
            caption: this.__caption.toJSON(),
            type: 'inline-image',
            version: 1
        };
    }
    getRelation() {
        return this.__relation;
    }
    getSrc() {
        return this.__src;
    }
    getAltText() {
        return this.__altText;
    }
    setAltText(altText) {
        const writable = this.getWritable();
        writable.__altText = altText;
    }
    setWidthAndHeight(width, height) {
        const writable = this.getWritable();
        writable.__width = width;
        writable.__height = height;
    }
    getShowCaption() {
        return this.__showCaption;
    }
    setShowCaption(showCaption) {
        const writable = this.getWritable();
        writable.__showCaption = showCaption;
    }
    getPosition() {
        return this.__position;
    }
    setPosition(position) {
        const writable = this.getWritable();
        writable.__position = position;
    }
    update(payload) {
        const writable = this.getWritable();
        const { targetDocumentId, targetCollectionId, targetCollectionPath, document: document1, src, position, altText, height, width, showCaption } = payload;
        if (null != targetDocumentId) writable.__relation = {
            targetDocumentId,
            targetCollectionId,
            targetCollectionPath,
            document: document1
        };
        if (null != src) writable.__src = src;
        if (null != position) writable.__position = position;
        if (null != altText) writable.__altText = altText;
        if (null != width) writable.__width = width;
        if (null != height) writable.__height = height;
        if (null != showCaption) writable.__showCaption = showCaption;
    }
    createDOM(config) {
        const span = document.createElement('span');
        const className = `${config.theme.inlineImage} position-${this.__position}`;
        if (void 0 !== className) span.className = className;
        return span;
    }
    updateDOM(prevNode, dom, config) {
        const position = this.__position;
        if (position !== prevNode.__position) {
            const className = `${config.theme.inlineImage} position-${position}`;
            if (void 0 !== className) dom.className = className;
            return true;
        }
        if (this.__showCaption !== prevNode.__showCaption) return true;
        return false;
    }
    decorate() {
        return /*#__PURE__*/ jsx(__rspack_external_react.Suspense, {
            fallback: null,
            children: /*#__PURE__*/ jsx(InlineImageComponent, {
                relation: this.__relation,
                src: this.__src,
                position: this.__position,
                altText: this.__altText,
                width: this.__width,
                height: this.__height,
                showCaption: this.__showCaption,
                caption: this.__caption,
                nodeKey: this.getKey()
            })
        });
    }
    constructor(relation, src, position, altText, width, height, showCaption, caption, key){
        super(key);
        this.__relation = relation;
        this.__src = src;
        this.__position = position;
        this.__altText = altText;
        this.__width = width;
        this.__height = height;
        this.__showCaption = showCaption ?? false;
        this.__caption = caption ?? createEditor();
    }
}
function $createInlineImageNode({ targetDocumentId, targetCollectionId, targetCollectionPath, document: document1, src, position, altText, height, width, showCaption, caption, key }) {
    const relation = {
        targetDocumentId,
        targetCollectionId,
        targetCollectionPath,
        document: document1
    };
    return $applyNodeReplacement(new InlineImageNode(relation, src, position, altText, width, height, showCaption, caption, key));
}
function $isInlineImageNode(node) {
    return node instanceof InlineImageNode;
}
export { $createInlineImageNode, $isInlineImageNode, InlineImageNode };
