"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.EmbedComponent = void 0;
const react_1 = __importStar(require("react"));
const inlines_1 = require("inlines");
const AddEmbedModal_1 = require("./AddEmbedModal");
const varsUtilities_1 = require("../../../varsUtilities");
const useLexicalNodeSelection_1 = require("@lexical/react/useLexicalNodeSelection");
const Button_1 = require("../../Button");
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
const lexical_1 = require("lexical");
const EmbedNode_1 = require("../nodes/EmbedNode");
const icons_1 = require("../../../icons");
const Text_1 = require("../../Text");
const utils_1 = require("@lexical/utils");
function EmbedComponent({ html, 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_1.BLUR_COMMAND, () => {
            clearSelection();
            return true;
        }, lexical_1.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',
            background: (0, varsUtilities_1.color)('action', 'neutral', 'subtleNormal'),
            padding: '64px 0',
            borderRadius: 8,
            border: `2px solid ${isSelected ? (0, varsUtilities_1.color)('content', 'brand', 'primary') : 'transparent'}`,
            '&:hover .overlay': {
                display: 'flex',
            },
            '& > * + *': {
                marginTop: '8px',
            },
        } },
        react_1.default.createElement(icons_1.IconLayerThree, null),
        react_1.default.createElement(Text_1.Text, null, "Custom HTML embed"),
        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',
                '& > * + *': {
                    marginTop: '12px',
                },
            } },
            react_1.default.createElement(AddEmbedModal_1.AddEmbedModal, { defaultHTML: html, onSave: ({ html }) => {
                    editor.update(() => {
                        const node = (0, lexical_1.$getNodeByKey)(nodeKey);
                        if ((0, EmbedNode_1.$isEmbedNode)(node)) {
                            const writable = node.getWritable();
                            writable.__html = html;
                        }
                    });
                } },
                react_1.default.createElement(Button_1.Button, { icon: react_1.default.createElement(icons_1.IconEdit, null) }, "Edit embed")),
            react_1.default.createElement(Button_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 embed"))));
}
exports.EmbedComponent = EmbedComponent;
//# sourceMappingURL=EmbedComponent.js.map