"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreeTextColumnModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const FreeTextColumnRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/FreeTextColumnRedux"));
const FreeTextColumnWizard_1 = require("../View/FreeTextColumn/Wizard/FreeTextColumnWizard");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../Utilities/Extensions/ArrayExtensions"));
class FreeTextColumnModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    isListeningToCellDataChanges;
    constructor(api) {
        super(ModuleConstants.FreeTextColumnModuleId, ModuleConstants.FreeTextColumnFriendlyName, 'horizontal-lines', 'FreeTextColumnPopup', 'Dynamic column showing custom content (stored in state and not with grid data)', api);
        this.isListeningToCellDataChanges = false;
    }
    onAdaptableReady() {
        this.checkListenToCellDataChanged();
    }
    shouldListenToDataChanges() {
        return ArrayExtensions_1.default.IsNotNullOrEmpty(this.api.freeTextColumnApi.getFreeTextColumns());
    }
    checkListenToCellDataChanged() {
        if (!this.isListeningToCellDataChanges) {
            if (this.shouldListenToDataChanges()) {
                this.setupCellDataChangeListener();
                this.isListeningToCellDataChanges = true;
            }
        }
    }
    setupCellDataChangeListener() {
        // Listen to Data Service to save any Free Text Column Edits
        // We dont refresh here as that is all done in AdaptableAgGrid
        this.api.internalApi
            .getDataService()
            .on('CellDataChanged', (cellDataChangedInfo) => {
            if (cellDataChangedInfo.trigger !== 'aggChange') {
                this.api.freeTextColumnApi.internalApi.handleFreeTextColumnDataChange(cellDataChangedInfo);
            }
        });
    }
    isModuleEnabled() {
        // FreeTextColumn module doesn't support autogenerated primary keys
        return !this.api.optionsApi.isAutogeneratePrimaryKey();
    }
    getModuleAdaptableObjects() {
        return this.api.freeTextColumnApi.getFreeTextColumns();
    }
    hasNamedQueryReferences() {
        return false;
    }
    createColumnMenuItems(column) {
        if (column && column.isFreeTextColumn && this.isModuleEditable()) {
            let popupParam = {
                column: column,
                action: 'Edit',
                source: 'ColumnMenu',
            };
            return [
                this.createMenuItemShowPopup('free-text-column-edit', 'Edit Free Text Column', this.moduleInfo.Popup, this.moduleInfo.Glyph, popupParam),
            ];
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.freeTextColumnApi.getFreeTextColumns(),
            AddAction: FreeTextColumnRedux.FreeTextColumnAdd,
            EditAction: FreeTextColumnRedux.FreeTextColumnEdit,
        };
    }
    toView(freeTextColumn) {
        return {
            abObject: freeTextColumn,
            items: [
                {
                    name: 'Settings',
                    label: 'Column Id',
                    values: [freeTextColumn.ColumnId],
                },
                freeTextColumn.FriendlyName && {
                    name: 'Settings',
                    label: 'Column Name',
                    values: [freeTextColumn.FriendlyName],
                },
                {
                    name: 'Settings',
                    label: 'Data Type',
                    values: [freeTextColumn.FreeTextColumnSettings.DataType ?? 'Not specified'],
                },
                Boolean(freeTextColumn.DefaultValue) && {
                    name: 'Settings',
                    label: 'Default Value',
                    values: [freeTextColumn.DefaultValue],
                },
                freeTextColumn.TextEditor && {
                    name: 'Editor Type',
                    label: 'Editor',
                    values: [freeTextColumn.TextEditor],
                },
                (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(freeTextColumn, this.api),
            ].filter(Boolean),
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((freeTextColumn) => this.toView(freeTextColumn));
    }
    getViewProperties() {
        return {
            getDeleteAction: FreeTextColumnRedux.FreeTextColumnDelete,
            getEditWizard() {
                return FreeTextColumnWizard_1.FreeTextColumnWizard;
            },
        };
    }
}
exports.FreeTextColumnModule = FreeTextColumnModule;
