"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.ValuePlugin = void 0;
const react_1 = __importStar(require("react"));
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
const LexicalOnChangePlugin_1 = require("@lexical/react/LexicalOnChangePlugin");
const html_1 = require("@lexical/html");
function ValuePlugin({ defaultValue, onChange, autoFocus, }) {
    const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
    const [isFirstRender, setIsFirstRender] = (0, react_1.useState)(true);
    (0, react_1.useEffect)(() => {
        if (defaultValue && isFirstRender && editor) {
            setIsFirstRender(false);
            editor.update(() => {
                editor.setEditorState(editor.parseEditorState(JSON.parse(defaultValue)));
            });
            console.log(JSON.parse(defaultValue), '🐱');
        }
    }, [isFirstRender, defaultValue, editor]);
    (0, react_1.useEffect)(() => {
        if (autoFocus && editor) {
            editor.focus();
        }
    }, []);
    return (react_1.default.createElement(LexicalOnChangePlugin_1.OnChangePlugin, { onChange: (editorState) => {
            editorState.read(() => {
                onChange?.({
                    json: JSON.stringify(editorState.toJSON()),
                    html: (0, html_1.$generateHtmlFromNodes)(editor, null),
                });
            });
        } }));
}
exports.ValuePlugin = ValuePlugin;
//# sourceMappingURL=ValuePlugin.js.map