"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridCellPopup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const rebass_1 = require("rebass");
const NoteRedux_1 = require("../../../../Redux/ActionsReducers/NoteRedux");
const InternalRedux_1 = require("../../../../Redux/ActionsReducers/InternalRedux");
const AdaptableContext_1 = require("../../../AdaptableContext");
const CommentsEditor_1 = require("../../../Comments/CommentsEditor");
const NoteEditor_1 = require("../../../Note/NoteEditor");
const CellPopup_1 = require("../../CellPopup");
const ConnectedNoteEditor = ({ enableEditMode }) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const isReadonly = api.entitlementApi.isModuleReadOnlyEntitlement('Note');
    const editMode = (0, react_redux_1.useSelector)((state) => (0, InternalRedux_1.CommentsAndNotesEditModeSelector)(state.Internal));
    const annotationsService = api.internalApi.getAnnotationsService();
    const cellAddress = (0, react_redux_1.useSelector)((state) => (0, InternalRedux_1.CommentsAndNotesSelector)(state.Internal));
    const note = (0, react_redux_1.useSelector)((state) => {
        return cellAddress ? (0, NoteRedux_1.GetNoteSelector)(state.Note, cellAddress) : null;
    });
    const handleNoteChange = React.useCallback((value) => {
        api.noteApi.updateNoteText(value, note);
    }, [note]);
    return (React.createElement(rebass_1.Flex, { onClick: () => enableEditMode(), flexDirection: "column", className: "ab-NotePopup" },
        React.createElement(NoteEditor_1.NoteEditor, { isReadonly: isReadonly, editMode: editMode, key: note?.Uuid, onClose: () => annotationsService.hidePopup(), note: note?.Text, onNoteChange: (value) => handleNoteChange(value) })));
};
const GridCellPopup = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const cellPopupRef = React.useRef(null);
    const editMode = (0, react_redux_1.useSelector)((state) => (0, InternalRedux_1.CommentsAndNotesEditModeSelector)(state.Internal));
    const focusedEntity = (0, react_redux_1.useSelector)((state) => (0, InternalRedux_1.CommentsAndNotesFocusedEntitySelector)(state.Internal));
    const cellAddress = (0, react_redux_1.useSelector)((state) => (0, InternalRedux_1.CommentsAndNotesSelector)(state.Internal));
    const enableEditMode = (entity) => {
        if (!editMode) {
            const annotationsService = api.internalApi.getAnnotationsService();
            annotationsService.showPopup(cellAddress, true);
            annotationsService.editFocusedEntity(entity);
        }
    };
    React.useEffect(() => {
        cellPopupRef?.current?.refreshContent?.();
    }, [focusedEntity]);
    if (!cellAddress) {
        return React.createElement(React.Fragment, null);
    }
    const noteEditor = React.createElement(ConnectedNoteEditor, { enableEditMode: () => enableEditMode('Note') });
    const commentEditor = (React.createElement(CommentsEditor_1.CommentsEditor, { onRefreshContent: () => cellPopupRef?.current?.refreshContent?.(), enableEditMode: () => enableEditMode('Comment') }));
    return (React.createElement(CellPopup_1.CellPopup, { ref: cellPopupRef, key: `${cellAddress.PrimaryKeyValue}-${cellAddress.ColumnId}`, isOpen: true, primaryKeyValue: cellAddress.PrimaryKeyValue, columnId: cellAddress.ColumnId }, (() => {
        switch (focusedEntity) {
            case 'Note':
                return noteEditor;
            case 'Comment':
                return commentEditor;
            default:
                return (React.createElement(React.Fragment, null,
                    noteEditor,
                    commentEditor));
        }
    })()));
};
exports.GridCellPopup = GridCellPopup;
