"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageComponent = void 0;
const useLexicalNodeSelection_1 = require("@lexical/react/useLexicalNodeSelection");
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
const utils_1 = require("@lexical/utils");
const lexical_1 = require("lexical");
const react_1 = __importStar(require("react"));
const inlines_1 = require("inlines");
const varsUtilities_1 = require("../../../varsUtilities");
const icons_1 = require("../../../icons");
const __1 = require("../..");
const ImageUploadModal_1 = require("./ImageUploadModal");
const lexical_2 = require("lexical");
const ImageNode_1 = require("../nodes/ImageNode");
function ImageComponent({ src, caption, nodeKey, }) {
    const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
    const [isSelected, setSelected, clearSelection] = (0, useLexicalNodeSelection_1.useLexicalNodeSelection)(nodeKey);
    const ref = (0, react_1.useRef)(null);
    const onDelete = (0, react_1.useCallback)((event) => {
        if (isSelected && (0, lexical_1.$isNodeSelection)((0, lexical_1.$getSelection)())) {
            event.preventDefault();
            const node = (0, lexical_1.$getNodeByKey)(nodeKey);
            if ((0, lexical_1.$isDecoratorNode)(node)) {
                node.remove();
            }
        }
        return false;
    }, [isSelected, nodeKey]);
    (0, react_1.useEffect)(() => {
        return (0, utils_1.mergeRegister)(editor.registerCommand(lexical_1.CLICK_COMMAND, (event) => {
            if (event.target === ref.current ||
                ref.current?.contains(event.target)) {
                clearSelection();
                setSelected(true);
                return true;
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_DELETE_COMMAND, onDelete, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_BACKSPACE_COMMAND, onDelete, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_2.BLUR_COMMAND, () => {
            clearSelection();
            return true;
        }, lexical_2.COMMAND_PRIORITY_EDITOR));
    }, [clearSelection, editor, isSelected, nodeKey, onDelete, setSelected]);
    return (react_1.default.createElement(inlines_1.styled.div, { ref: ref, style: {
            position: 'relative',
            overflow: 'hidden',
            display: 'flex',
            flexDirection: 'column',
            justifyContent: 'center',
            alignItems: 'center',
            borderRadius: 8,
            border: `2px solid ${isSelected ? (0, varsUtilities_1.color)('content', 'brand', 'primary') : 'transparent'}`,
            '& > * + *': {
                marginTop: '8px',
            },
            '&:hover .overlay': {
                display: 'flex',
            },
        } },
        react_1.default.createElement(inlines_1.styled.img, { src: src, alt: "", style: {
                display: 'block',
                margin: '0 auto',
                maxWidth: '100%',
                height: 'auto',
            } }),
        react_1.default.createElement(inlines_1.styled.div, null, caption),
        react_1.default.createElement(inlines_1.styled.div, { className: "overlay", style: {
                position: 'absolute',
                top: 0,
                left: 0,
                right: 0,
                bottom: 0,
                display: 'none',
                margin: 0,
                flexDirection: 'column',
                justifyContent: 'center',
                alignItems: 'center',
                backdropFilter: 'blur(4px)',
                '& > * + *': {
                    marginTop: '12px',
                },
            } },
            react_1.default.createElement(ImageUploadModal_1.ImageUploadModal, { mode: "edit", defaultCaption: caption, onSave: (val) => {
                    editor.update(() => {
                        const node = (0, lexical_1.$getNodeByKey)(nodeKey);
                        if ((0, ImageNode_1.$isImageNode)(node)) {
                            const writable = node.getWritable();
                            if (val.file) {
                                writable.__src = val.file.src;
                            }
                            writable.__caption = val.caption;
                        }
                    });
                } },
                react_1.default.createElement(__1.Button, { color: "primary", icon: react_1.default.createElement(icons_1.IconEdit, null) }, "Edit image")),
            react_1.default.createElement(__1.Button, { color: "alert", icon: react_1.default.createElement(icons_1.IconDelete, null), onClick: () => {
                    editor.update(() => {
                        const node = (0, lexical_1.$getNodeByKey)(nodeKey);
                        node.remove();
                    });
                } }, "Delete image"))));
}
exports.ImageComponent = ImageComponent;
//# sourceMappingURL=ImageComponent.js.map