import { DOM } from '@ni/fast-element';
import { UpdateTracker } from '../../utilities/models/update-tracker';
import { isColumnInternalsProperty } from '../../table-column/base/models/column-internals';
const isColumnProperty = (changedProperty, ...args) => {
    for (const arg of args) {
        if (changedProperty === arg) {
            return true;
        }
    }
    return false;
};
const trackedItems = [
    'rowIds',
    'rowParentIds',
    'groupRows',
    'columnIds',
    'columnHidden',
    'columnSort',
    'columnSortDisabled',
    'columnWidths',
    'columnDefinition',
    'actionMenuSlots',
    'selectionMode',
    'actionMenusPreserveSelection'
];
/**
 * Helper class to track what updates are needed to the table based on configuration
 * changes.
 */
export class TableUpdateTracker extends UpdateTracker {
    constructor(table) {
        super(trackedItems);
        this.table = table;
        this.updateQueued = false;
    }
    get updateRowIds() {
        return this.isTracked('rowIds');
    }
    get updateRowParentIds() {
        return this.isTracked('rowParentIds');
    }
    get updateGroupRows() {
        return this.isTracked('groupRows');
    }
    get updateColumnIds() {
        return this.isTracked('columnIds');
    }
    get updateColumnSort() {
        return this.isTracked('columnSort');
    }
    get updateColumnWidths() {
        return this.isTracked('columnWidths');
    }
    get updateColumnDefinition() {
        return this.isTracked('columnDefinition');
    }
    get updateActionMenuSlots() {
        return this.isTracked('actionMenuSlots');
    }
    get updateSelectionMode() {
        return this.isTracked('selectionMode');
    }
    get updateActionMenusPreserveSelection() {
        return this.isTracked('actionMenusPreserveSelection');
    }
    get requiresTanStackUpdate() {
        return (this.isTracked('rowIds')
            || this.isTracked('rowParentIds')
            || this.isTracked('columnSort')
            || this.isTracked('columnDefinition')
            || this.isTracked('groupRows')
            || this.isTracked('selectionMode'));
    }
    get requiresTanStackDataReset() {
        return (this.isTracked('rowIds')
            || this.isTracked('columnDefinition')
            || this.isTracked('rowParentIds'));
    }
    get requiresKeyboardFocusReset() {
        return (this.isTracked('columnSortDisabled')
            || this.isTracked('columnDefinition')
            || this.isTracked('columnHidden')
            || this.isTracked('selectionMode')
            || this.isTracked('actionMenuSlots'));
    }
    trackAllStateChanged() {
        this.trackAll();
        this.queueUpdate();
    }
    get hasPendingUpdates() {
        return this.updateQueued;
    }
    trackColumnPropertyChanged(changedColumnProperty) {
        if (isColumnProperty(changedColumnProperty, 'columnId')) {
            this.track('columnIds');
        }
        else if (isColumnInternalsProperty(changedColumnProperty, 'operandDataRecordFieldName', 'sortOperation')) {
            this.track('columnDefinition');
        }
        else if (isColumnInternalsProperty(changedColumnProperty, 'sortingDisabled')) {
            this.track('columnSort');
            this.track('columnSortDisabled');
        }
        else if (isColumnInternalsProperty(changedColumnProperty, 'currentSortDirection', 'currentSortIndex')) {
            this.track('columnSort');
        }
        else if (isColumnInternalsProperty(changedColumnProperty, 'currentFractionalWidth', 'currentPixelWidth', 'minPixelWidth', 'resizingDisabled')) {
            this.track('columnWidths');
        }
        else if (isColumnProperty(changedColumnProperty, 'columnHidden')) {
            this.track('columnWidths');
            this.track('columnHidden');
        }
        else if (isColumnProperty(changedColumnProperty, 'actionMenuSlot')) {
            this.track('actionMenuSlots');
        }
        else if (isColumnInternalsProperty(changedColumnProperty, 'groupIndex', 'groupingDisabled')) {
            this.track('groupRows');
        }
        this.queueUpdate();
    }
    trackColumnInstancesChanged() {
        this.track('columnIds');
        this.track('columnDefinition');
        this.track('columnSort');
        this.track('columnSortDisabled');
        this.track('columnWidths');
        this.track('actionMenuSlots');
        this.track('groupRows');
        this.queueUpdate();
    }
    trackIdFieldNameChanged() {
        this.track('rowIds');
        this.queueUpdate();
    }
    trackParentIdFieldNameChanged() {
        this.track('rowParentIds');
        this.queueUpdate();
    }
    trackSelectionModeChanged() {
        this.track('selectionMode');
        this.queueUpdate();
    }
    trackActionMenusPreserveSelectionChanged() {
        this.track('actionMenusPreserveSelection');
        this.queueUpdate();
    }
    queueUpdate() {
        if (!this.table.$fastController.isConnected) {
            return;
        }
        if (!this.updateQueued) {
            this.updateQueued = true;
            DOM.queueUpdate(() => {
                this.table.update();
                this.untrackAll();
                this.updateQueued = false;
            });
        }
    }
}
//# sourceMappingURL=table-update-tracker.js.map