/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { SortDirection } from './types';
export class MultiSortManager {
    constructor() {
        this._sortedColumns = [];
    }
    get sortedColumns() {
        return [...this._sortedColumns.map(c => (Object.assign({}, c)))];
    }
    addSortColumn(column) {
        this._sortedColumns.push(Object.assign({}, column));
        this._updateSortNumbers();
    }
    updateSortColumn(column) {
        const col = this._sortedColumns.find(c => c.columnIndex === column.columnIndex);
        if (col) {
            switch (col.direction) {
                case SortDirection.Ascending:
                    this.removeSortColumn(column);
                    break;
                case SortDirection.Descending:
                    col.direction = SortDirection.Ascending;
                    break;
                case SortDirection.Unset:
                    col.direction = SortDirection.Descending;
                    break;
            }
        }
    }
    setSortColumns(columns) {
        this._sortedColumns = [...columns.map(c => (Object.assign({}, c)))];
    }
    removeSortColumn(columnOrIndex) {
        if (typeof columnOrIndex === 'number') {
            this._sortedColumns.splice(columnOrIndex, 1);
        }
        else {
            this._sortedColumns.splice(this._sortedColumns.indexOf(columnOrIndex), 1);
        }
        this._updateSortNumbers();
    }
    isSortColumn(columnOrIndex) {
        if (typeof columnOrIndex === 'number') {
            return this._sortedColumns.some(c => c.columnIndex === columnOrIndex);
        }
        else {
            return this._sortedColumns.includes(columnOrIndex);
        }
    }
    clearMultiSort() {
        this._sortedColumns = [];
    }
    getSortColumn(columnIndex) {
        return this._sortedColumns.find(c => c.columnIndex === columnIndex);
    }
    _updateSortNumbers() {
        // only do a real sort if every sortNumber is actually a number
        if (this._sortedColumns.every(c => typeof c.sortOrder === 'number')) {
            this._sortedColumns.sort((col1, col2) => col1.sortOrder - col2.sortOrder);
        }
        this._sortedColumns.forEach((column, index) => column.sortOrder = index + 1);
    }
}
