"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoteEditor = void 0;
const tslib_1 = require("tslib");
const throttle_1 = tslib_1.__importDefault(require("../../Utilities/utils/throttle"));
const React = tslib_1.__importStar(require("react"));
const Textarea_1 = tslib_1.__importDefault(require("../../components/Textarea"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const AdaptableContext_1 = require("../AdaptableContext");
const clsx_1 = tslib_1.__importDefault(require("clsx"));
const NoteEditor = ({ note, onNoteChange, onClose, editMode, isReadonly }) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const showPopupCloseButton = api.optionsApi.getNoteOptions()?.showPopupCloseButton ?? true;
    const textAreaRef = React.useRef(null);
    const [liveValue, setLiveValue] = React.useState(note || '');
    const throttledOnChange = React.useMemo(() => {
        const throttled = (0, throttle_1.default)((value) => onNoteChange(value), 300);
        return (value) => {
            setLiveValue(value);
            throttled(value);
        };
    }, []);
    React.useEffect(() => {
        if (editMode) {
            textAreaRef.current?.focus();
        }
    }, [editMode]);
    if (note === undefined || note === null) {
        return null;
    }
    return (React.createElement(React.Fragment, null,
        React.createElement(Textarea_1.default, { readOnly: isReadonly, ref: textAreaRef, className: (0, clsx_1.default)('twa:min-w-[180px] twa:min-h-[120px] twa:w-full', {
                'twa:pr-[30px]': showPopupCloseButton,
                'twa:pr-0': !showPopupCloseButton,
            }), value: liveValue, onBlur: () => onClose(), onKeyDown: (event) => {
                if (event.key === 'Escape') {
                    onClose();
                }
            }, onChange: (event) => throttledOnChange(event.target.value) }),
        showPopupCloseButton && (React.createElement(SimpleButton_1.default, { variant: "text", style: { position: 'absolute', right: 5, top: 5 }, icon: "close" }))));
};
exports.NoteEditor = NoteEditor;
