"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SortService = void 0;
const index_1 = require("../enums/index");
const utilities_1 = require("./utilities");
const sortUtilities_1 = require("../sortComparers/sortUtilities");
const index_2 = require("../core/index");
class SortService {
    constructor(sharedService, pubSubService, backendUtilities, rxjs) {
        this.sharedService = sharedService;
        this.pubSubService = pubSubService;
        this.backendUtilities = backendUtilities;
        this.rxjs = rxjs;
        this._currentLocalSorters = [];
        this._isBackendGrid = false;
        this._eventHandler = new index_2.SlickEventHandler();
        if (this.rxjs) {
            this.httpCancelRequests$ = this.rxjs.createSubject();
        }
    }
    /** Getter of the SlickGrid Event Handler */
    get eventHandler() {
        return this._eventHandler;
    }
    /** Getter for the Grid Options pulled through the Grid Object */
    get _gridOptions() {
        return this._grid?.getOptions() ?? {};
    }
    /** Getter for the Column Definitions pulled through the Grid Object */
    get _columnDefinitions() {
        return this._grid?.getColumns() ?? [];
    }
    dispose() {
        // unsubscribe all SlickGrid events
        if (this._eventHandler?.unsubscribeAll) {
            this._eventHandler.unsubscribeAll();
        }
        if (this.httpCancelRequests$ && this.rxjs?.isObservable(this.httpCancelRequests$)) {
            this.httpCancelRequests$.next(); // this cancels any pending http requests
            this.httpCancelRequests$.complete();
        }
    }
    addRxJsResource(rxjs) {
        this.rxjs = rxjs;
    }
    /**
     * Bind a backend sort (single/multi) hook to the grid
     * @param grid SlickGrid Grid object
     * @param dataView SlickGrid DataView object
     */
    bindBackendOnSort(grid) {
        this._isBackendGrid = true;
        this._grid = grid;
        this._dataView = grid?.getData();
        // subscribe to the SlickGrid event and call the backend execution
        this._eventHandler.subscribe(grid.onSort, this.onBackendSortChanged.bind(this));
    }
    /**
     * Bind a local sort (single/multi) hook to the grid
     * @param grid SlickGrid Grid object
     * @param gridOptions Grid Options object
     * @param dataView
     */
    bindLocalOnSort(grid) {
        this._isBackendGrid = false;
        this._grid = grid;
        this._dataView = grid?.getData();
        this.processTreeDataInitialSort();
        this._eventHandler.subscribe(grid.onSort, this.handleLocalOnSort.bind(this));
    }
    handleLocalOnSort(_e, args) {
        // multiSort and singleSort are not exactly the same, but we want to structure it the same for the (for loop) after
        // also to avoid having to rewrite the for loop in the sort, we will make the singleSort an array of 1 object
        const sortColumns = (args.multiColumnSort)
            ? args.sortCols
            : new Array({
                columnId: args.sortCol?.id ?? '',
                sortAsc: args.sortAsc,
                sortCol: args.sortCol
            });
        // keep current sorters
        this._currentLocalSorters = []; // reset current local sorters
        if (Array.isArray(sortColumns)) {
            sortColumns.forEach((sortColumn) => {
                if (sortColumn.sortCol) {
                    this._currentLocalSorters.push({
                        columnId: sortColumn.sortCol.id,
                        direction: sortColumn.sortAsc ? index_1.SortDirection.ASC : index_1.SortDirection.DESC
                    });
                }
            });
        }
        this.onLocalSortChanged(this._grid, sortColumns);
        this.emitSortChanged(index_1.EmitterType.local);
    }
    clearSortByColumnId(event, columnId) {
        // get previously sorted columns
        const allSortedCols = this.getCurrentColumnSorts();
        const sortedColsWithoutCurrent = this.getCurrentColumnSorts(`${columnId}`);
        if (Array.isArray(allSortedCols) && Array.isArray(sortedColsWithoutCurrent) && allSortedCols.length !== sortedColsWithoutCurrent.length) {
            if (this._gridOptions.backendServiceApi) {
                this.onBackendSortChanged(event, { multiColumnSort: true, sortCols: sortedColsWithoutCurrent, grid: this._grid });
            }
            else if (this._dataView) {
                this.onLocalSortChanged(this._grid, sortedColsWithoutCurrent, true, true);
            }
            else {
                // when using customDataView, we will simply send it as a onSort event with notify
                const isMultiSort = this._gridOptions?.multiColumnSort ?? false;
                const sortOutput = isMultiSort ? sortedColsWithoutCurrent : sortedColsWithoutCurrent[0];
                this._grid.onSort.notify(sortOutput);
            }
            // update the grid sortColumns array which will at the same add the visual sort icon(s) on the UI
            const updatedSortColumns = sortedColsWithoutCurrent.map((col) => {
                return {
                    columnId: col?.sortCol?.id ?? '',
                    sortAsc: col?.sortAsc,
                    sortCol: col?.sortCol,
                };
            });
            this._grid.setSortColumns(updatedSortColumns); // add sort icon in UI
        }
        // when there's no more sorting, we re-sort by the default sort field, user can customize it "defaultColumnSortFieldId", defaults to "id"
        if (Array.isArray(sortedColsWithoutCurrent) && sortedColsWithoutCurrent.length === 0) {
            this.sortLocalGridByDefaultSortFieldId();
        }
    }
    /**
     * Clear Sorting
     * - 1st, remove the SlickGrid sort icons (this setSortColumns function call really does only that)
     * - 2nd, we also need to trigger a sort change
     *   - for a backend grid, we will trigger a backend sort changed with an empty sort columns array
     *   - however for a local grid, we need to pass a sort column and so we will sort by the 1st column
     * @param trigger query event after executing clear filter?
     */
    clearSorting(triggerQueryEvent = true) {
        if (this._grid && this._gridOptions && this._dataView) {
            // remove any sort icons (this setSortColumns function call really does only that)
            this._grid.setSortColumns([]);
            // we also need to trigger a sort change
            // for a backend grid, we will trigger a backend sort changed with an empty sort columns array
            // however for a local grid, we need to pass a sort column and so we will sort by the 1st column
            if (triggerQueryEvent) {
                if (this._isBackendGrid) {
                    this.onBackendSortChanged(undefined, { grid: this._grid, multiColumnSort: true, sortCols: [], clearSortTriggered: true });
                }
                else {
                    if (this._columnDefinitions && Array.isArray(this._columnDefinitions) && this._columnDefinitions.length > 0) {
                        this.sortLocalGridByDefaultSortFieldId();
                    }
                }
            }
            else if (this._isBackendGrid) {
                const backendService = this._gridOptions?.backendServiceApi?.service;
                if (backendService?.clearSorters) {
                    backendService.clearSorters();
                }
            }
        }
        // set current sorter to empty & emit a sort changed event
        this._currentLocalSorters = [];
        // emit an event when sorts are all cleared
        this.pubSubService.publish('onSortCleared', true);
    }
    /**
     * Toggle the Sorting Functionality
     * @param {boolean} isSortingDisabled - optionally force a disable/enable of the Sort Functionality? Defaults to True
     * @param {boolean} clearSortingWhenDisabled - when disabling the sorting, do we also want to clear the sorting as well? Defaults to True
     */
    disableSortFunctionality(isSortingDisabled = true, clearSortingWhenDisabled = true) {
        const prevSorting = this._gridOptions.enableSorting;
        const newSorting = !prevSorting;
        this._gridOptions.enableSorting = newSorting;
        let updatedColumnDefinitions;
        if (isSortingDisabled) {
            if (clearSortingWhenDisabled) {
                this.clearSorting();
            }
            this._eventHandler.unsubscribeAll();
            updatedColumnDefinitions = this.disableAllSortingCommands(true);
        }
        else {
            updatedColumnDefinitions = this.disableAllSortingCommands(false);
            this._eventHandler.subscribe(this._grid.onSort, (e, args) => this.handleLocalOnSort(e, args));
        }
        this._grid.setOptions({ enableSorting: this._gridOptions.enableSorting }, false, true);
        this.sharedService.gridOptions = this._gridOptions;
        // reset columns so that it recreate the column headers and remove/add the sort icon hints
        // basically without this, the sort icon hints were still showing up even after disabling the Sorting
        this._grid.setColumns(updatedColumnDefinitions);
    }
    /**
     * Toggle the Sorting functionality
     * @param {boolean} clearSortingWhenDisabled - when disabling the sorting, do we also want to clear the sorting as well? Defaults to True
     */
    toggleSortFunctionality(clearSortingOnDisable = true) {
        const previousSorting = this._gridOptions.enableSorting;
        this.disableSortFunctionality(previousSorting, clearSortingOnDisable);
    }
    /**
     * A simple function that will be called to emit a change when a sort changes.
     * Other services, like Pagination, can then subscribe to it.
     * @param sender
     */
    emitSortChanged(sender, currentLocalSorters) {
        if (sender === index_1.EmitterType.remote && this._gridOptions?.backendServiceApi) {
            let currentSorters = [];
            const backendService = this._gridOptions.backendServiceApi.service;
            if (backendService?.getCurrentSorters) {
                currentSorters = backendService.getCurrentSorters();
            }
            this.pubSubService.publish('onSortChanged', currentSorters);
        }
        else if (sender === index_1.EmitterType.local) {
            if (currentLocalSorters) {
                this._currentLocalSorters = currentLocalSorters;
            }
            this.pubSubService.publish('onSortChanged', this.getCurrentLocalSorters());
        }
    }
    getCurrentLocalSorters() {
        return this._currentLocalSorters;
    }
    /**
     * Get current column sorts,
     * If a column is passed as an argument, that will be exclusion so we won't add this column to our output array since it is already in the array.
     * The usage of this method is that we want to know the sort prior to calling the next sorting command
     */
    getCurrentColumnSorts(excludedColumnId) {
        // getSortColumns() only returns sortAsc & columnId, we want the entire column definition
        if (this._grid) {
            const oldSortColumns = this._grid.getSortColumns();
            // get the column definition but only keep column which are not equal to our current column
            if (Array.isArray(oldSortColumns)) {
                const sortedCols = oldSortColumns.reduce((cols, col) => {
                    if (col && (!excludedColumnId || col.columnId !== excludedColumnId)) {
                        cols.push({ columnId: col.columnId || '', sortCol: this._columnDefinitions[this._grid.getColumnIndex(col.columnId || '')], sortAsc: col.sortAsc });
                    }
                    return cols;
                }, []);
                return sortedCols;
            }
        }
        return [];
    }
    /** Load defined Sorting (sorters) into the grid */
    loadGridSorters(sorters) {
        this._currentLocalSorters = []; // reset current local sorters
        const sortCols = [];
        if (Array.isArray(sorters)) {
            const tmpSorters = this._gridOptions.multiColumnSort ? sorters : sorters.slice(0, 1);
            tmpSorters.forEach((sorter) => {
                const column = this._columnDefinitions.find((col) => col.id === sorter.columnId);
                if (column) {
                    if (!column.sortable) {
                        let errorMsg = '[Slickgrid-Universal] Cannot add sort icon to a column that is not sortable, please add `sortable: true` to your column or remove it from your list of columns to sort.';
                        if (this._gridOptions.enableTreeData) {
                            errorMsg += ' Also note that TreeData feature requires the column holding the tree (expand/collapse icons) to be sortable.';
                        }
                        throw new Error(errorMsg);
                    }
                    sortCols.push({
                        columnId: column.id,
                        sortAsc: ((sorter.direction.toUpperCase() === index_1.SortDirection.ASC) ? true : false),
                        sortCol: column
                    });
                    // keep current sorters
                    this._currentLocalSorters.push({
                        columnId: String(column.id),
                        direction: sorter.direction.toUpperCase()
                    });
                }
            });
            this.onLocalSortChanged(this._grid, sortCols);
            this._grid.setSortColumns(sortCols.map(col => ({ columnId: col.columnId, sortAsc: col.sortAsc }))); // use this to add sort icon(s) in UI
        }
        return sortCols;
    }
    /** Process the initial sort, typically it will sort ascending by the column that has the Tree Data unless user specifies a different initialSort */
    processTreeDataInitialSort() {
        // when a Tree Data view is defined, we must sort the data so that the UI works correctly
        if (this._gridOptions?.enableTreeData && this._gridOptions.treeDataOptions) {
            // first presort it once by tree level
            const treeDataOptions = this._gridOptions.treeDataOptions;
            const columnWithTreeData = this._columnDefinitions.find((col) => col.id === treeDataOptions.columnId);
            if (columnWithTreeData) {
                let sortDirection = index_1.SortDirection.ASC;
                let sortTreeLevelColumn = { columnId: treeDataOptions.columnId, sortCol: columnWithTreeData, sortAsc: true };
                // user could provide a custom sort field id, if so get that column and sort by it
                if (treeDataOptions?.initialSort?.columnId) {
                    const initialSortColumnId = treeDataOptions.initialSort.columnId;
                    const initialSortColumn = this._columnDefinitions.find((col) => col.id === initialSortColumnId);
                    sortDirection = (treeDataOptions.initialSort.direction || index_1.SortDirection.ASC).toUpperCase();
                    sortTreeLevelColumn = { columnId: initialSortColumnId, sortCol: initialSortColumn, sortAsc: (sortDirection === index_1.SortDirection.ASC) };
                }
                // when we have a valid column with Tree Data, we can sort by that column
                if (sortTreeLevelColumn?.columnId && this.sharedService?.hierarchicalDataset) {
                    this.updateSorting([{ columnId: sortTreeLevelColumn.columnId || '', direction: sortDirection }]);
                }
            }
        }
    }
    /**
     * When working with Backend Service, we'll use the `onBeforeSort` which will return false since we want to manually apply the sort icons only after the server response
     * @param event - optional Event that triggered the sort
     * @param args - sort event arguments
     * @returns - False since we'll apply the sort icon(s) manually only after server responded
     */
    onBackendSortChanged(event, args) {
        if (!args || !args.grid) {
            throw new Error('Something went wrong when trying to bind the "onBackendSortChanged(event, args)" function, it seems that "args" is not populated correctly');
        }
        const gridOptions = args.grid?.getOptions() ?? {};
        const backendApi = gridOptions.backendServiceApi;
        if (!backendApi || !backendApi.process || !backendApi.service) {
            throw new Error(`BackendServiceApi requires at least a "process" function and a "service" defined`);
        }
        // keep start time & end timestamps & return it after process execution
        const startTime = new Date();
        if (backendApi.preProcess) {
            backendApi.preProcess();
        }
        // query backend
        const query = backendApi.service.processOnSortChanged(event, args);
        const totalItems = gridOptions?.pagination?.totalItems || 0;
        this.backendUtilities?.executeBackendCallback(backendApi, query, args, startTime, totalItems, {
            emitActionChangedCallback: this.emitSortChanged.bind(this),
            errorCallback: () => {
                // revert to previous sort icons & also revert backend service query
                this._grid.setSortColumns(args.previousSortColumns || []);
                // we also need to provide the `sortCol` when using the backend service `updateSorters` method
                const sorterData = args.previousSortColumns?.map(cs => ({
                    columnId: cs.columnId,
                    sortAsc: cs.sortAsc,
                    sortCol: this._columnDefinitions.find(col => col.id === cs.columnId)
                }));
                backendApi?.service?.updateSorters?.(sorterData || []);
            },
            httpCancelRequestSubject: this.httpCancelRequests$
        });
    }
    /** When a Sort Changes on a Local grid (JSON dataset) */
    async onLocalSortChanged(grid, sortColumns, forceReSort = false, emitSortChanged = false) {
        const datasetIdPropertyName = this._gridOptions?.datasetIdPropertyName ?? 'id';
        const isTreeDataEnabled = this._gridOptions?.enableTreeData ?? false;
        const dataView = grid.getData();
        await this.pubSubService.publish('onBeforeSortChange', { sortColumns }, 0);
        if (grid && dataView) {
            if (forceReSort && !isTreeDataEnabled) {
                dataView.reSort();
            }
            if (isTreeDataEnabled && this.sharedService && Array.isArray(this.sharedService.hierarchicalDataset)) {
                const datasetSortResult = this.sortHierarchicalDataset(this.sharedService.hierarchicalDataset, sortColumns);
                // we could use the DataView sort but that would require re-sorting again (since the 2nd array that is currently in the DataView would have to be resorted against the 1st array that was sorting from tree sort)
                // it is simply much faster to just replace the entire dataset
                this._dataView.setItems(datasetSortResult.flat, datasetIdPropertyName);
                // also trigger a row count changed to avoid having an invalid filtered item count in the grid footer
                // basically without this the item count in the footer is incorrect and shows the full dataset length instead of the previous filtered count
                // that happens because we just overwrote the entire dataset the DataView.refresh() doesn't detect a row count change so we trigger it manually
                this._dataView.onRowCountChanged.notify({ previous: this._dataView.getFilteredItemCount(), current: this._dataView.getLength(), itemCount: this._dataView.getItemCount(), dataView: this._dataView, callingOnRowsChanged: true });
            }
            else {
                dataView.sort(this.sortComparers.bind(this, sortColumns));
            }
            grid.invalidate();
            if (emitSortChanged) {
                this.emitSortChanged(index_1.EmitterType.local, sortColumns.map(col => {
                    return {
                        columnId: col.sortCol?.id ?? 'id',
                        direction: col.sortAsc ? index_1.SortDirection.ASC : index_1.SortDirection.DESC
                    };
                }));
            }
        }
    }
    /**
     * Takes a hierarchical dataset and sort it recursively by reference and returns both flat & hierarchical sorted datasets.
     * Note: for perf reasons, it mutates the array by adding extra props like `treeLevel`
     */
    sortHierarchicalDataset(hierarchicalDataset, sortColumns, emitSortChanged = false) {
        this.sortTreeData(hierarchicalDataset, sortColumns);
        const dataViewIdIdentifier = this._gridOptions?.datasetIdPropertyName ?? 'id';
        const treeDataOpt = this._gridOptions?.treeDataOptions ?? { columnId: '' };
        const treeDataOptions = { ...treeDataOpt, identifierPropName: treeDataOpt.identifierPropName ?? dataViewIdIdentifier, shouldAddTreeLevelNumber: true };
        const sortedFlatArray = (0, utilities_1.flattenToParentChildArray)(hierarchicalDataset, treeDataOptions);
        if (emitSortChanged) {
            // update current sorters
            this._currentLocalSorters = [];
            sortColumns.forEach(sortCol => {
                this._currentLocalSorters.push({ columnId: sortCol.columnId, direction: sortCol.sortAsc ? 'ASC' : 'DESC' });
            });
            const emitterType = this._gridOptions?.backendServiceApi ? index_1.EmitterType.remote : index_1.EmitterType.local;
            this.emitSortChanged(emitterType);
        }
        return { hierarchical: hierarchicalDataset, flat: sortedFlatArray };
    }
    /** Call a local grid sort by its default sort field id (user can customize default field by configuring "defaultColumnSortFieldId" in the grid options, defaults to "id") */
    sortLocalGridByDefaultSortFieldId() {
        const sortColFieldId = this._gridOptions && this._gridOptions.defaultColumnSortFieldId || this._gridOptions.datasetIdPropertyName || 'id';
        const sortCol = { id: sortColFieldId, field: sortColFieldId };
        this.onLocalSortChanged(this._grid, new Array({ columnId: sortCol.id, sortAsc: true, sortCol, clearSortTriggered: true }), false, true);
    }
    sortComparers(sortColumns, dataRow1, dataRow2) {
        if (Array.isArray(sortColumns)) {
            for (const sortColumn of sortColumns) {
                const result = this.sortComparer(sortColumn, dataRow1, dataRow2);
                if (result !== undefined) {
                    return result;
                }
            }
        }
        return index_1.SortDirectionNumber.neutral;
    }
    sortComparer(sortColumn, dataRow1, dataRow2, querySortField) {
        if (sortColumn?.sortCol) {
            const columnDef = sortColumn.sortCol;
            const sortDirection = sortColumn.sortAsc ? index_1.SortDirectionNumber.asc : index_1.SortDirectionNumber.desc;
            let queryFieldName1 = querySortField || columnDef.queryFieldSorter || columnDef.queryField || columnDef.field;
            let queryFieldName2 = queryFieldName1;
            const fieldType = columnDef.type || index_1.FieldType.string;
            // if user provided a query field name getter callback, we need to get the name on each item independently
            if (typeof columnDef.queryFieldNameGetterFn === 'function') {
                queryFieldName1 = columnDef.queryFieldNameGetterFn(dataRow1);
                queryFieldName2 = columnDef.queryFieldNameGetterFn(dataRow2);
            }
            let value1 = dataRow1[queryFieldName1];
            let value2 = dataRow2[queryFieldName2];
            // when item is a complex object (dot "." notation), we need to filter the value contained in the object tree
            if (queryFieldName1?.indexOf('.') >= 0) {
                value1 = (0, utilities_1.getDescendantProperty)(dataRow1, queryFieldName1);
            }
            if (queryFieldName2?.indexOf('.') >= 0) {
                value2 = (0, utilities_1.getDescendantProperty)(dataRow2, queryFieldName2);
            }
            // user could provide his own custom Sorter
            if (columnDef.sortComparer) {
                const customSortResult = columnDef.sortComparer(value1, value2, sortDirection, columnDef, this._gridOptions);
                if (customSortResult !== index_1.SortDirectionNumber.neutral) {
                    return customSortResult;
                }
            }
            else {
                const sortResult = (0, sortUtilities_1.sortByFieldType)(fieldType, value1, value2, sortDirection, columnDef, this._gridOptions);
                if (sortResult !== index_1.SortDirectionNumber.neutral) {
                    return sortResult;
                }
            }
        }
        return undefined;
    }
    sortTreeData(treeArray, sortColumns) {
        if (Array.isArray(sortColumns)) {
            sortColumns.forEach(sortColumn => {
                this.sortTreeChildren(treeArray, sortColumn, 0);
            });
        }
    }
    /** Sort the Tree Children of a hierarchical dataset by recursion */
    sortTreeChildren(treeArray, sortColumn, treeLevel) {
        const treeDataOptions = this._gridOptions?.treeDataOptions;
        const childrenPropName = treeDataOptions?.childrenPropName ?? 'children';
        treeArray.sort((a, b) => this.sortComparer(sortColumn, a, b) ?? index_1.SortDirectionNumber.neutral);
        // when item has a child, we'll sort recursively
        treeArray.forEach(item => {
            if (item) {
                const hasChildren = item.hasOwnProperty(childrenPropName) && Array.isArray(item[childrenPropName]);
                // when item has a child, we'll sort recursively
                if (hasChildren) {
                    treeLevel++;
                    this.sortTreeChildren(item[childrenPropName], sortColumn, treeLevel);
                    treeLevel--;
                }
            }
        });
    }
    /**
     * Update Sorting (sorters) dynamically just by providing an array of sorter(s).
     * You can also choose emit (default) a Sort Changed event that will be picked by the Grid State Service.
     *
     * Also for backend service only, you can choose to trigger a backend query (default) or not if you wish to do it later,
     * this could be useful when using updateFilters & updateSorting and you wish to only send the backend query once.
     * @param sorters array
     * @param triggerEvent defaults to True, do we want to emit a sort changed event?
     * @param triggerBackendQuery defaults to True, which will query the backend.
     */
    updateSorting(sorters, emitChangedEvent = true, triggerBackendQuery = true) {
        if (!this._gridOptions || !this._gridOptions.enableSorting) {
            throw new Error('[Slickgrid-Universal] in order to use "updateSorting" method, you need to have Sortable Columns defined in your grid and "enableSorting" set in your Grid Options');
        }
        if (Array.isArray(sorters)) {
            const backendApi = this._gridOptions?.backendServiceApi;
            if (backendApi) {
                const backendApiService = backendApi?.service;
                if (backendApiService?.updateSorters) {
                    backendApiService.updateSorters(undefined, sorters);
                    if (triggerBackendQuery) {
                        this.backendUtilities?.refreshBackendDataset(this._gridOptions);
                    }
                }
            }
            else {
                this.loadGridSorters(sorters);
            }
            if (emitChangedEvent) {
                const emitterType = backendApi ? index_1.EmitterType.remote : index_1.EmitterType.local;
                this.emitSortChanged(emitterType);
            }
        }
    }
    // --
    // protected functions
    // -------------------
    /**
     * Loop through all column definitions and do the following 2 things
     * 1. disable/enable the "sortable" property of each column
     * 2. loop through each Header Menu commands and change the "hidden" commands to show/hide depending if it's enabled/disabled
     * Also note that we aren't deleting any properties, we just toggle their flags so that we can reloop through at later point in time.
     * (if we previously deleted these properties we wouldn't be able to change them back since these properties wouldn't exist anymore, hence why we just hide the commands)
     * @param {boolean} isDisabling - are we disabling the sort functionality? Defaults to true
     */
    disableAllSortingCommands(isDisabling = true) {
        const columnDefinitions = this._grid.getColumns();
        // loop through column definition to hide/show header menu commands
        columnDefinitions.forEach((col) => {
            if (col.sortable !== undefined) {
                col.sortable = !isDisabling;
            }
            if (col?.header?.menu) {
                (col.header.menu.commandItems)?.forEach(menuItem => {
                    if (menuItem && typeof menuItem !== 'string') {
                        const menuCommand = menuItem.command;
                        if (menuCommand === 'sort-asc' || menuCommand === 'sort-desc' || menuCommand === 'clear-sort') {
                            menuItem.hidden = isDisabling;
                        }
                    }
                });
            }
        });
        // loop through column definition to hide/show grid menu commands
        const commandItems = this._gridOptions?.gridMenu?.commandItems;
        if (commandItems) {
            commandItems.forEach((menuItem) => {
                if (menuItem && typeof menuItem !== 'string') {
                    const menuCommand = menuItem.command;
                    if (menuCommand === 'clear-sorting') {
                        menuItem.hidden = isDisabling;
                    }
                }
            });
        }
        return columnDefinitions;
    }
}
exports.SortService = SortService;
//# sourceMappingURL=sort.service.js.map