"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExcalidrawNode = void 0;
exports.$createExcalidrawNode = $createExcalidrawNode;
exports.$isExcalidrawNode = $isExcalidrawNode;
const tslib_1 = require("tslib");
const lexical_1 = require("lexical");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const ExcalidrawComponent = React.lazy(
// @ts-ignore
() => Promise.resolve().then(() => tslib_1.__importStar(require('./ExcalidrawComponent'))));
function convertExcalidrawElement(domNode) {
    const excalidrawData = domNode.getAttribute('data-lexical-excalidraw-json');
    if (excalidrawData) {
        const node = $createExcalidrawNode();
        node.__data = excalidrawData;
        return {
            node,
        };
    }
    return null;
}
class ExcalidrawNode extends lexical_1.DecoratorNode {
    static getType() {
        return 'excalidraw';
    }
    static clone(node) {
        return new ExcalidrawNode(node.__data, node.__key);
    }
    static importJSON(serializedNode) {
        return new ExcalidrawNode(serializedNode.data);
    }
    exportJSON() {
        return {
            data: this.__data,
            type: 'excalidraw',
            version: 1,
        };
    }
    constructor(data = '[]', key) {
        super(key);
        this.__data = data;
    }
    // View
    createDOM(config) {
        const span = document.createElement('span');
        const theme = config.theme;
        const className = theme.image;
        if (className !== undefined) {
            span.className = className;
        }
        return span;
    }
    updateDOM() {
        return false;
    }
    static importDOM() {
        return {
            span: (domNode) => {
                if (!domNode.hasAttribute('data-lexical-excalidraw-json')) {
                    return null;
                }
                return {
                    conversion: convertExcalidrawElement,
                    priority: 1,
                };
            },
        };
    }
    exportDOM(editor) {
        const element = document.createElement('span');
        const content = editor.getElementByKey(this.getKey());
        if (content !== null) {
            const svg = content.querySelector('svg');
            if (svg !== null) {
                element.innerHTML = svg.outerHTML;
            }
        }
        element.setAttribute('data-lexical-excalidraw-json', this.__data);
        return { element };
    }
    setData(data) {
        const self = this.getWritable();
        self.__data = data;
    }
    decorate() {
        return (React.createElement(react_1.Suspense, { fallback: null },
            React.createElement(ExcalidrawComponent, { data: this.__data, nodeKey: this.getKey() })));
    }
}
exports.ExcalidrawNode = ExcalidrawNode;
function $createExcalidrawNode() {
    return new ExcalidrawNode();
}
function $isExcalidrawNode(node) {
    return node instanceof ExcalidrawNode;
}
//# sourceMappingURL=index.js.map