"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnFilterModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const LayoutRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/LayoutRedux"));
const InternalRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/InternalRedux"));
const FilterStatusBarSubPanelPopover_1 = require("../View/Filter/FilterStatusBarSubPanelPopover");
const getLayoutFilterViewItems_1 = require("./Utilities/Layout/getLayoutFilterViewItems");
class ColumnFilterModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.ColumnFilterModuleId, ModuleConstants.ColumnFilterFriendlyName, 'filter', 'FilterPopup', 'Advanced filtering capability allows users to see precisely the data they want', api);
    }
    setModuleEntitlement() {
        if (!this.api.optionsApi.getFilterOptions().useAdaptableFiltering) {
            this.AccessLevel = 'Hidden';
        }
        else {
            this.AccessLevel = this.api.entitlementApi.getEntitlementAccessLevelForModule(this.moduleInfo.ModuleName);
        }
    }
    getModuleAdaptableObjects() {
        return this.api.filterApi.columnFilterApi.getColumnFilters();
    }
    getExplicitlyReferencedColumnIds(columnFilter) {
        return [columnFilter.ColumnId];
    }
    hasNamedQueryReferences() {
        return false;
    }
    onAdaptableReady() {
        if (this.api.optionsApi.getFilterOptions().columnFilterOptions.showQuickFilter == false) {
            this.api.filterApi.columnFilterApi.hideQuickFilterBar();
        }
        else {
            this.api.filterApi.columnFilterApi.showQuickFilterBar();
        }
        const columnFilters = this.api.filterApi.columnFilterApi.getColumnFilters();
        columnFilters.forEach((cf) => {
            if (!this.api.columnApi.isColumnInGrid(cf.ColumnId)) {
                this.api.logError('Column Filter contains missing column: ' + cf.ColumnId);
            }
        });
    }
    createColumnMenuItems(column) {
        let columnFilterMenuItems = [];
        if (column &&
            column.filterable &&
            this.api.optionsApi.getFilterOptions().useAdaptableFiltering) {
            if (this.api.gridApi.isQuickFilterAvailable()) {
                const isFilterVisible = this.api.filterApi.columnFilterApi.isQuickFilterVisible();
                if (this.isModuleEditable() &&
                    this.api.optionsApi.getFilterOptions().useAdaptableFiltering) {
                    columnFilterMenuItems.push(this.createMenuItemReduxAction(isFilterVisible ? 'column-filter-bar-hide' : 'column-filter-bar-show', isFilterVisible ? 'Hide Filter Bar' : 'Show Filter Bar', isFilterVisible ? 'filter-off' : 'filter', isFilterVisible
                        ? InternalRedux.QuickFilterBarHide()
                        : InternalRedux.QuickFilterBarShow()));
                }
            }
            const existingColumnFilter = this.api.filterApi.columnFilterApi.getColumnFilterForColumn(column.columnId);
            if (existingColumnFilter) {
                columnFilterMenuItems.push(this.createMenuItemReduxAction('column-filter-clear', 'Clear Filter', 'close', LayoutRedux.LayoutColumnFilterClear(existingColumnFilter.ColumnId)));
                columnFilterMenuItems.push(this.createMenuItemReduxAction(existingColumnFilter.IsSuspended ? 'column-filter-unsuspend' : 'column-filter-suspend', existingColumnFilter.IsSuspended ? 'Unsuspend Filter' : 'Suspend Filter', existingColumnFilter.IsSuspended ? 'resume' : 'pause', existingColumnFilter.IsSuspended
                    ? LayoutRedux.LayoutColumnFilterUnSuspend(existingColumnFilter)
                    : LayoutRedux.LayoutColumnFilterSuspend(existingColumnFilter)));
            }
            return columnFilterMenuItems;
        }
    }
    createContextMenuItems(menuContext) {
        if (!menuContext.isRowGroupColumn && this.isModuleAvailable()) {
            if (menuContext.adaptableColumn &&
                menuContext.adaptableColumn.filterable &&
                menuContext.isSingleSelectedColumn &&
                menuContext.gridCell != null &&
                this.api.optionsApi.getFilterOptions().useAdaptableFiltering) {
                const existingColumnFilter = this.api.filterApi.columnFilterApi.getColumnFilterForColumn(menuContext.adaptableColumn.columnId);
                if (!existingColumnFilter) {
                    if (!menuContext.isRowGroupColumn && !menuContext.isGroupedNode) {
                        let isMultiple = menuContext.selectedCellInfo.gridCells.length > 1;
                        let clickFunction = isMultiple
                            ? () => {
                                this.api.filterApi.columnFilterApi.internalApi.createValuesColumnFilterForCells(menuContext.selectedCellInfo.gridCells);
                            }
                            : () => {
                                this.api.filterApi.columnFilterApi.internalApi.createEqualityColumnFilterForCell(menuContext.selectedCellInfo.gridCells[0]);
                            };
                        return [
                            this.createMenuItemClickFunction('column-filter-on-cell-value', isMultiple ? 'Filter on Cell Values' : 'Filter on Cell Value', this.moduleInfo.Glyph, clickFunction),
                        ];
                    }
                }
                else {
                    return [
                        this.createMenuItemReduxAction('column-filter-clear', 'Clear Filter', 'close', LayoutRedux.LayoutColumnFilterClear(existingColumnFilter.ColumnId)),
                        this.createMenuItemClickFunction(existingColumnFilter.IsSuspended
                            ? 'column-filter-unsuspend'
                            : 'column-filter-suspend', existingColumnFilter.IsSuspended ? 'Unsuspend Filter' : 'Suspend Filter', existingColumnFilter.IsSuspended ? 'resume' : 'pause', existingColumnFilter.IsSuspended
                            ? () => {
                                this.api.filterApi.columnFilterApi.unSuspendColumnFilter(existingColumnFilter);
                            }
                            : () => {
                                this.api.filterApi.columnFilterApi.suspendColumnFilter(existingColumnFilter);
                            }),
                    ];
                }
            }
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.filterApi.columnFilterApi.getColumnFilters(),
            AddAction: LayoutRedux.LayoutColumnFilterAdd,
            EditAction: LayoutRedux.LayoutColumnFilterEdit, //TODO need to change this to ColumnFilterSet
        };
    }
    toViewCompact(filter) {
        return {
            item: {
                values: [this.api.filterApi.columnFilterApi.columnFilterToString(filter)],
            },
            abObject: filter,
        };
    }
    toView(filter) {
        return {
            items: [
                {
                    name: 'Column',
                    values: [this.api.columnApi.getFriendlyNameForColumnId(filter.ColumnId)],
                },
                {
                    name: 'Filter',
                    values: [(0, getLayoutFilterViewItems_1.getFilterPreview)(filter, this.api)],
                },
            ],
            abObject: filter,
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((filter) => this.toView(filter));
    }
    getViewProperties() {
        const api = this.api;
        return {
            getSuspendAction: (columnFilter) => LayoutRedux.LayoutColumnFilterSuspend(columnFilter),
            getUnSuspendAction: (columnFilter) => LayoutRedux.LayoutColumnFilterUnSuspend(columnFilter),
            getCompactSuspendAction: (columnFilter) => LayoutRedux.LayoutColumnFilterSuspend(columnFilter),
            getCompactUnSuspendAction: (columnFilter) => LayoutRedux.LayoutColumnFilterUnSuspend(columnFilter),
            getSuspendAllAction: () => LayoutRedux.LayoutColumnFilterSuspendAll(),
            getUnSuspendAllAction: () => LayoutRedux.LayoutColumnFilterUnSuspendAll(),
            getDeleteAction: (columnFilter) => LayoutRedux.LayoutColumnFilterClear(columnFilter.ColumnId),
            getCompactDeleteAction: (columnFilter) => LayoutRedux.LayoutColumnFilterClear(columnFilter.ColumnId),
            getDeleteAllAction: LayoutRedux.LayoutColumnFilterClearAll,
            emptyView: `
        There are no Column Filters applied. \n
        Create Column Filters by using the Filter Components or by editing a Layout.
      `,
            getStatusBarPanelProps() {
                let text;
                const filters = api.filterApi.columnFilterApi.getColumnFilters();
                if (filters.length === 0) {
                    text = '0 Filters';
                }
                else if (filters.length === 1) {
                    text = '1 Filter';
                }
                else {
                    text = filters.length + ' Filters';
                }
                const popover = filters.length ? FilterStatusBarSubPanelPopover_1.FilterStatusBarSubPanelPopover : null;
                return {
                    content: text,
                    popover,
                    popoverMinWidth: 320,
                };
            },
        };
    }
}
exports.ColumnFilterModule = ColumnFilterModule;
