"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotePopup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const InfiniteTable_1 = require("../../components/InfiniteTable");
const NoteRedux_1 = require("../../Redux/ActionsReducers/NoteRedux");
const AdaptableContext_1 = require("../AdaptableContext");
const AdaptableButton_1 = require("../Components/AdaptableButton");
const PopupPanel_1 = require("../Components/Popups/AdaptablePopup/PopupPanel");
const StringExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/StringExtensions"));
const FormatHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/FormatHelper"));
const tableDOMProps = {
    style: {
        height: '100%',
        minWidth: '10rem',
        // minHeight: 600,
    },
};
const columnTypes = {
    default: {
        align: 'start',
        defaultFlex: 1,
        defaultSortable: false,
        style: { fontSize: 'var(--ab-font-size-3)' },
    },
};
const headerOptions = {
    alwaysReserveSpaceForSortIcon: false,
};
const NotePopup = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const primaryKeyHeader = adaptable.api.columnApi.getPrimaryKeyColumn()
        ? adaptable.api.columnApi.getFriendlyNameForColumnId(adaptable.api.columnApi.getPrimaryKeyColumn().columnId)
        : StringExtensions_1.default.Humanize(adaptable.adaptableOptions.primaryKey);
    const allNotes = (0, react_redux_1.useSelector)((state) => (0, NoteRedux_1.GetAllNotesSelector)(state.Note));
    const isReadOnlyModule = adaptable.api.entitlementApi.getEntitlementAccessLevelForModule('Note') === 'ReadOnly';
    const formatDate = (date, format) => {
        return FormatHelper_1.default.DateFormatter(date, { Pattern: format });
    };
    const columnsMap = React.useMemo(() => {
        const columns = {
            primaryKey: {
                field: 'PrimaryKeyValue',
                header: primaryKeyHeader,
            },
            column: {
                field: 'ColumnId',
                header: 'Column',
                valueGetter: (params) => {
                    return adaptable.api.columnApi.getFriendlyNameForColumnId(params.data.ColumnId);
                },
            },
            text: {
                field: 'Text',
                header: 'Text',
                defaultEditable: !isReadOnlyModule,
                defaultFlex: 3,
            },
            timestamp: {
                field: 'Timestamp',
                valueGetter: (params) => {
                    return formatDate(params.data.Timestamp, adaptable.api.noteApi.internalApi.getNotesDateFormat());
                },
            },
            delete: {
                defaultWidth: 50,
                render: (params) => {
                    return (React.createElement(AdaptableButton_1.AdaptableButtonComponent, { disabled: isReadOnlyModule, variant: "text", icon: "delete", onClick: () => {
                            const note = adaptable.api.noteApi.getNoteByUuid(params.data.Uuid);
                            adaptable.api.noteApi.deleteNote(note);
                        } }));
                },
            },
        };
        return columns;
    }, []);
    return (React.createElement(PopupPanel_1.PopupPanel, { headerText: 'Notes', glyphicon: 'import', infoLink: props.moduleInfo.HelpPage },
        React.createElement(InfiniteTable_1.DataSource, { data: allNotes, primaryKey: "Uuid", onDataMutations: (params) => {
                for (const mutation of params.mutations) {
                    const [uuid, changes] = mutation;
                    for (const change of changes) {
                        if (change.type === 'update') {
                            const data = change.data;
                            const prevNote = allNotes.find((x) => x.Uuid === uuid);
                            if (prevNote.Text !== data.Text) {
                                adaptable.api.noteApi.updateNoteText(data.Text, prevNote);
                            }
                        }
                    }
                }
            } },
            React.createElement(InfiniteTable_1.InfiniteTableGrid, { columnTypes: columnTypes, headerOptions: headerOptions, domProps: tableDOMProps, columns: columnsMap }))));
};
exports.NotePopup = NotePopup;
