import { $applyNodeReplacement, $isTextNode, TextNode, } from 'lexical';
// @ts-ignore
export class ExtendedTextNode extends TextNode {
    constructor(text, key) {
        super(text, key);
    }
    // @ts-ignore
    static getType() {
        return 'extended-text';
    }
    // @ts-ignore
    static clone(node) {
        return new ExtendedTextNode(node.__text, node.__key);
    }
    // @ts-ignore
    static importDOM() {
        const importers = TextNode.importDOM();
        return {
            ...importers,
            code: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.code),
                priority: 1,
            }),
            em: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.em),
                priority: 1,
            }),
            span: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.span),
                priority: 1,
            }),
            strong: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.strong),
                priority: 1,
            }),
            sub: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.sub),
                priority: 1,
            }),
            sup: () => ({
                conversion: patchStyleConversion(importers === null || importers === void 0 ? void 0 : importers.sup),
                priority: 1,
            }),
        };
    }
    // @ts-ignore
    static importJSON(serializedNode) {
        return TextNode.importJSON(serializedNode);
    }
    // @ts-ignore
    isSimpleText() {
        return this.__type === 'extended-text' && this.__mode === 0;
    }
    // @ts-ignore
    exportJSON() {
        return {
            ...super.exportJSON(),
            type: 'extended-text',
            version: 1,
        };
    }
}
export function $createExtendedTextNode(text) {
    // @ts-ignore
    return $applyNodeReplacement(new ExtendedTextNode(text));
}
export function $isExtendedTextNode(node) {
    return node instanceof ExtendedTextNode;
}
function patchStyleConversion(originalDOMConverter) {
    return (node) => {
        const original = originalDOMConverter === null || originalDOMConverter === void 0 ? void 0 : originalDOMConverter(node);
        if (!original) {
            return null;
        }
        const originalOutput = original.conversion(node);
        if (!originalOutput) {
            return originalOutput;
        }
        const backgroundColor = node.style.backgroundColor;
        const color = node.style.color;
        const fontFamily = node.style.fontFamily;
        const fontWeight = node.style.fontWeight;
        const fontSize = node.style.fontSize;
        const textDecoration = node.style.textDecoration;
        return {
            ...originalOutput,
            forChild: (lexicalNode, parent) => {
                var _a;
                const originalForChild = (_a = originalOutput === null || originalOutput === void 0 ? void 0 : originalOutput.forChild) !== null && _a !== void 0 ? _a : ((x) => x);
                const result = originalForChild(lexicalNode, parent);
                if ($isTextNode(result)) {
                    const style = [
                        backgroundColor ? `background-color: ${backgroundColor}` : null,
                        color ? `color: ${color}` : null,
                        fontFamily ? `font-family: ${fontFamily}` : null,
                        fontWeight ? `font-weight: ${fontWeight}` : null,
                        fontSize ? `font-size: ${fontSize}` : null,
                        textDecoration ? `text-decoration: ${textDecoration}` : null,
                    ]
                        .filter((value) => value != null)
                        .join('; ');
                    if (style.length > 0) {
                        return result.setStyle(style);
                    }
                }
                return result;
            },
        };
    };
}
//# sourceMappingURL=ExtendedTextNode.js.map