import { AdaptableModuleBase } from './AdaptableModuleBase';
import * as ModuleConstants from '../Utilities/Constants/ModuleConstants';
import * as FreeTextColumnRedux from '../Redux/ActionsReducers/FreeTextColumnRedux';
import { FreeTextColumnWizard } from '../View/FreeTextColumn/Wizard/FreeTextColumnWizard';
import { getObjectTagsViewItems } from '../Utilities/getObjectTagsViewItems';
import ArrayExtensions from '../Utilities/Extensions/ArrayExtensions';
export class FreeTextColumnModule extends AdaptableModuleBase {
    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.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);
            }
        });
    }
    isModuleAvailable() {
        // FreeTextColumn module doesn't support autogenerated primary keys
        return super.isModuleAvailable() && !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],
                },
                getObjectTagsViewItems(freeTextColumn, this.api),
            ].filter(Boolean),
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((freeTextColumn) => this.toView(freeTextColumn));
    }
    getViewProperties() {
        return {
            getDeleteAction: FreeTextColumnRedux.FreeTextColumnDelete,
            getEditWizard() {
                return FreeTextColumnWizard;
            },
        };
    }
}
