"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_draft_wysiwyg_1 = require("react-draft-wysiwyg");
const draft_js_1 = require("draft-js");
const draftjs_to_html_1 = __importDefault(require("draftjs-to-html"));
const html_to_draftjs_1 = __importDefault(require("html-to-draftjs"));
const BindContext_1 = __importDefault(require("./BindContext"));
const JSONBind = (_props, forwardedRef) => {
    const { value, setValue } = (0, react_1.useContext)(BindContext_1.default);
    const contentBlock = (0, html_to_draftjs_1.default)(value || '');
    const initContentState = draft_js_1.ContentState.createFromBlockArray(contentBlock.contentBlocks);
    const [editorState, setEditorState] = (0, react_1.useState)(draft_js_1.EditorState.createWithContent(initContentState));
    const editorRef = (0, react_1.useRef)(null);
    const replaceProps = (str) => {
        const contentState = editorState.getCurrentContent();
        const targetRange = editorState.getSelection();
        const newContentState = draft_js_1.Modifier.insertText(contentState, targetRange, str);
        const newEditorState = draft_js_1.EditorState.push(editorState, newContentState, 'insert-characters');
        setEditorState(newEditorState);
        saveValue(newEditorState);
        setTimeout(() => {
            var _a;
            (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.editor.focus();
        });
    };
    (0, react_1.useImperativeHandle)(forwardedRef, () => ({
        replaceProps,
    }));
    const handleStateChange = (editorState) => {
        setEditorState(editorState);
        saveValue(editorState);
    };
    const saveValue = (editorState) => {
        setValue((0, draftjs_to_html_1.default)((0, draft_js_1.convertToRaw)(editorState.getCurrentContent())));
    };
    return (react_1.default.createElement("div", { style: { height: '70%' } },
        react_1.default.createElement(react_draft_wysiwyg_1.Editor, { ref: editorRef, placeholder: "Enter some text...", toolbar: Object.assign(Object.assign({}, toolbar), { inline: { inDropdown: true }, list: { inDropdown: true }, textAlign: { inDropdown: true }, link: { inDropdown: true }, history: { inDropdown: true } }), editorState: editorState, onEditorStateChange: handleStateChange })));
};
exports.default = (0, react_1.forwardRef)(JSONBind);
