/**
 * DevExtreme (esm/__internal/grids/grid_core/focus/m_focus.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../../../core/renderer";
import {
    equalByValue
} from "../../../../core/utils/common";
import {
    Deferred,
    when
} from "../../../../core/utils/deferred";
import {
    each
} from "../../../../core/utils/iterator";
import {
    isBoolean,
    isDefined
} from "../../../../core/utils/type";
import {
    isNewRowTempKey
} from "../editing/m_editing_utils";
import core from "../m_modules";
import gridCoreUtils from "../m_utils";
import {
    UiGridCoreFocusUtils
} from "./m_focus_utils";
var ROW_FOCUSED_CLASS = "dx-row-focused";
var FOCUSED_ROW_SELECTOR = ".dx-row.".concat(ROW_FOCUSED_CLASS);
var TABLE_POSTFIX_CLASS = "table";
var CELL_FOCUS_DISABLED_CLASS = "dx-cell-focus-disabled";
export class FocusController extends core.ViewController {
    getKeyboardController() {
        return this.getController("keyboardNavigation")
    }
    getDataController() {
        return this.getController("data")
    }
    init() {
        this.component._optionsByReference.focusedRowKey = true
    }
    optionChanged(args) {
        var {
            name: name,
            value: value,
            previousValue: previousValue
        } = args;
        switch (name) {
            case "focusedRowIndex":
                this._focusRowByIndex(value);
                this.getKeyboardController()._fireFocusedRowChanged();
                args.handled = true;
                break;
            case "focusedRowKey":
                if (Array.isArray(value) && JSON.stringify(value) === JSON.stringify(previousValue)) {
                    return
                }
                this._focusRowByKey(value);
                this.getKeyboardController()._fireFocusedRowChanged();
                args.handled = true;
                break;
            case "focusedColumnIndex":
            case "focusedRowEnabled":
            case "autoNavigateToFocusedRow":
                args.handled = true;
                break;
            default:
                super.optionChanged(args)
        }
    }
    publicMethods() {
        return ["navigateToRow", "isRowFocused"]
    }
    isAutoNavigateToFocusedRow() {
        return "infinite" !== this.option("scrolling.mode") && this.option("autoNavigateToFocusedRow")
    }
    _focusRowByIndex(index, operationTypes) {
        if (!this.option("focusedRowEnabled")) {
            return
        }
        index = void 0 !== index ? index : this.option("focusedRowIndex");
        if (index < 0) {
            if (this.isAutoNavigateToFocusedRow()) {
                this._resetFocusedRow()
            }
        } else {
            this._focusRowByIndexCore(index, operationTypes)
        }
    }
    _focusRowByIndexCore(index, operationTypes) {
        var pageSize = this.getDataController().pageSize();
        var setKeyByIndex = () => {
            if (this._isValidFocusedRowIndex(index)) {
                var rowIndex = index - this.getDataController().getRowIndexOffset(true);
                if (!operationTypes || operationTypes.paging && !operationTypes.filtering) {
                    var lastItemIndex = this.getDataController()._getLastItemIndex();
                    rowIndex = Math.min(rowIndex, lastItemIndex)
                }
                var focusedRowKey = this.getDataController().getKeyByRowIndex(rowIndex, true);
                if (isDefined(focusedRowKey) && !this.isRowFocused(focusedRowKey)) {
                    this.option("focusedRowKey", focusedRowKey)
                }
            }
        };
        if (pageSize >= 0) {
            if (!this._isLocalRowIndex(index)) {
                var pageIndex = Math.floor(index / this.getDataController().pageSize());
                when(this.getDataController().pageIndex(pageIndex), this.getDataController().waitReady()).done(() => {
                    setKeyByIndex()
                })
            } else {
                setKeyByIndex()
            }
        }
    }
    _isLocalRowIndex(index) {
        var isVirtualScrolling = this.getKeyboardController()._isVirtualScrolling();
        if (isVirtualScrolling) {
            var pageIndex = Math.floor(index / this.getDataController().pageSize());
            var virtualItems = this.getDataController().virtualItemsCount();
            var virtualItemsBegin = virtualItems ? virtualItems.begin : -1;
            var visibleRowsCount = this.getDataController().getVisibleRows().length + this.getDataController().getRowIndexOffset();
            var visiblePagesCount = Math.ceil(visibleRowsCount / this.getDataController().pageSize());
            return virtualItemsBegin <= index && visiblePagesCount > pageIndex
        }
        return true
    }
    _setFocusedRowKeyByIndex(index) {
        if (this._isValidFocusedRowIndex(index)) {
            var rowIndex = Math.min(index - this.getDataController().getRowIndexOffset(), this.getDataController().items().length - 1);
            var focusedRowKey = this.getDataController().getKeyByRowIndex(rowIndex);
            if (isDefined(focusedRowKey) && !this.isRowFocused(focusedRowKey)) {
                this.option("focusedRowKey", focusedRowKey)
            }
        }
    }
    _focusRowByKey(key) {
        if (!isDefined(key)) {
            this._resetFocusedRow()
        } else {
            this._navigateToRow(key, true)
        }
    }
    _resetFocusedRow() {
        var focusedRowKey = this.option("focusedRowKey");
        var isFocusedRowKeyDefined = isDefined(focusedRowKey);
        if (!isFocusedRowKeyDefined && this.option("focusedRowIndex") < 0) {
            return
        }
        if (isFocusedRowKeyDefined) {
            this.option("focusedRowKey", null)
        }
        this.getKeyboardController().setFocusedRowIndex(-1);
        this.option("focusedRowIndex", -1);
        this.getDataController().updateItems({
            changeType: "updateFocusedRow",
            focusedRowKey: null
        });
        this.getKeyboardController()._fireFocusedRowChanged()
    }
    _isValidFocusedRowIndex(rowIndex) {
        var row = this.getDataController().getVisibleRows()[rowIndex];
        return !row || "data" === row.rowType || "group" === row.rowType
    }
    navigateToRow(key) {
        if (!this.isAutoNavigateToFocusedRow()) {
            this.option("focusedRowIndex", -1)
        }
        return this._navigateToRow(key)
    }
    _navigateToRow(key, needFocusRow) {
        var that = this;
        var isAutoNavigate = that.isAutoNavigateToFocusedRow();
        var d = new Deferred;
        if (void 0 === key || !this.getDataController().dataSource()) {
            return d.reject().promise()
        }
        var rowIndexByKey = that.getFocusedRowIndexByKey(key);
        if (!isAutoNavigate && needFocusRow || rowIndexByKey >= 0) {
            that._navigateTo(key, d, needFocusRow)
        } else {
            this.getDataController().getPageIndexByKey(key).done(pageIndex => {
                if (pageIndex < 0) {
                    d.resolve(-1);
                    return
                }
                if (pageIndex === this.getDataController().pageIndex()) {
                    this.getDataController().reload().done(() => {
                        if (that.isRowFocused(key) && this.getDataController().getRowIndexByKey(key) >= 0) {
                            d.resolve(that.getFocusedRowIndexByKey(key))
                        } else {
                            that._navigateTo(key, d, needFocusRow)
                        }
                    }).fail(d.reject)
                } else {
                    this.getDataController().pageIndex(pageIndex).done(() => {
                        that._navigateTo(key, d, needFocusRow)
                    }).fail(d.reject)
                }
            }).fail(d.reject)
        }
        return d.promise()
    }
    _navigateTo(key, deferred, needFocusRow) {
        var visibleRowIndex = this.getDataController().getRowIndexByKey(key);
        var isVirtualRowRenderingMode = gridCoreUtils.isVirtualRowRendering(this);
        var isAutoNavigate = this.isAutoNavigateToFocusedRow();
        if (isAutoNavigate && isVirtualRowRenderingMode && visibleRowIndex < 0) {
            this._navigateToVirtualRow(key, deferred, needFocusRow)
        } else {
            this._navigateToVisibleRow(key, deferred, needFocusRow)
        }
    }
    _navigateToVisibleRow(key, deferred, needFocusRow) {
        if (needFocusRow) {
            this._triggerUpdateFocusedRow(key, deferred)
        } else {
            var focusedRowIndex = this.getFocusedRowIndexByKey(key);
            this.getView("rowsView").scrollToRowElement(key, deferred).done(() => {
                deferred.resolve(focusedRowIndex)
            })
        }
    }
    _navigateToVirtualRow(key, deferred, needFocusRow) {
        var rowsScrollController = this.getDataController()._rowsScrollController;
        var rowIndex = gridCoreUtils.getIndexByKey(key, this.getDataController().items(true));
        var scrollable = this.getView("rowsView").getScrollable();
        if (rowsScrollController && scrollable && rowIndex >= 0) {
            var focusedRowIndex = rowIndex + this.getDataController().getRowIndexOffset(true);
            var offset = rowsScrollController.getItemOffset(focusedRowIndex);
            var triggerUpdateFocusedRow = () => {
                if (this.getDataController().totalCount() && !this.getDataController().items().length) {
                    return
                }
                this.component.off("contentReady", triggerUpdateFocusedRow);
                if (needFocusRow) {
                    this._triggerUpdateFocusedRow(key, deferred)
                } else {
                    deferred.resolve(focusedRowIndex)
                }
            };
            this.component.on("contentReady", triggerUpdateFocusedRow);
            this.getView("rowsView").scrollTopPosition(offset)
        } else {
            deferred.resolve(-1)
        }
    }
    _triggerUpdateFocusedRow(key, deferred) {
        var focusedRowIndex = this.getFocusedRowIndexByKey(key);
        if (this._isValidFocusedRowIndex(focusedRowIndex)) {
            var d;
            if (this.option("focusedRowEnabled")) {
                this.getDataController().updateItems({
                    changeType: "updateFocusedRow",
                    focusedRowKey: key
                })
            } else {
                d = this.getView("rowsView").scrollToRowElement(key)
            }
            when(d).done(() => {
                this.getKeyboardController().setFocusedRowIndex(focusedRowIndex);
                deferred && deferred.resolve(focusedRowIndex)
            })
        } else {
            deferred && deferred.resolve(-1)
        }
    }
    getFocusedRowIndexByKey(key) {
        var loadedRowIndex = this.getDataController().getRowIndexByKey(key, true);
        return loadedRowIndex >= 0 ? loadedRowIndex + this.getDataController().getRowIndexOffset(true) : -1
    }
    _focusRowByKeyOrIndex() {
        var focusedRowKey = this.option("focusedRowKey");
        var currentFocusedRowIndex = this.option("focusedRowIndex");
        if (isDefined(focusedRowKey)) {
            var visibleRowIndex = this.getDataController().getRowIndexByKey(focusedRowKey);
            if (visibleRowIndex >= 0) {
                if (this.getKeyboardController()._isVirtualScrolling()) {
                    currentFocusedRowIndex = visibleRowIndex + this.getDataController().getRowIndexOffset()
                }
                this.getKeyboardController().setFocusedRowIndex(currentFocusedRowIndex);
                this._triggerUpdateFocusedRow(focusedRowKey)
            } else {
                this._navigateToRow(focusedRowKey, true).done(focusedRowIndex => {
                    if (currentFocusedRowIndex >= 0 && focusedRowIndex < 0) {
                        this._focusRowByIndex()
                    } else if (currentFocusedRowIndex < 0 && focusedRowIndex >= 0) {
                        this.getKeyboardController().setFocusedRowIndex(focusedRowIndex)
                    }
                })
            }
        } else if (currentFocusedRowIndex >= 0) {
            this._focusRowByIndex(currentFocusedRowIndex)
        }
    }
    isRowFocused(key) {
        var focusedRowKey = this.option("focusedRowKey");
        if (isDefined(focusedRowKey)) {
            return equalByValue(key, this.option("focusedRowKey"))
        }
        return
    }
    updateFocusedRow(e) {
        var _a;
        var that = this;
        var focusedRowIndex = null !== (_a = e.focusedRowIndex) && void 0 !== _a ? _a : that.getDataController().getRowIndexByKey(e.focusedRowKey);
        var rowsView = that.getView("rowsView");
        var $tableElement;
        var $mainRow;
        each(rowsView.getTableElements(), (index, element) => {
            var isMainTable = 0 === index;
            $tableElement = $(element);
            that._clearPreviousFocusedRow($tableElement, focusedRowIndex);
            var $row = that._prepareFocusedRow({
                changedItem: that.getDataController().getVisibleRows()[focusedRowIndex],
                $tableElement: $tableElement,
                focusedRowIndex: focusedRowIndex
            });
            if (isMainTable) {
                $mainRow = $row
            }
        });
        if (!e.preventScroll && $mainRow) {
            rowsView.scrollToElementVertically($mainRow)
        }
    }
    _clearPreviousFocusedRow($tableElement, focusedRowIndex) {
        var $prevRowFocusedElement = $tableElement.find(FOCUSED_ROW_SELECTOR).filter((_, focusedRow) => {
            var $focusedRowTable = $(focusedRow).closest(".".concat(this.addWidgetPrefix(TABLE_POSTFIX_CLASS)));
            return $tableElement.is($focusedRowTable)
        });
        $prevRowFocusedElement.removeClass(ROW_FOCUSED_CLASS).removeClass(CELL_FOCUS_DISABLED_CLASS).removeAttr("tabindex");
        $prevRowFocusedElement.children("td").removeAttr("tabindex");
        if (0 !== focusedRowIndex) {
            var $firstRow = $(this.getView("rowsView").getRowElement(0));
            $firstRow.removeClass(CELL_FOCUS_DISABLED_CLASS).removeAttr("tabIndex")
        }
    }
    _prepareFocusedRow(options) {
        var $row;
        var {
            changedItem: changedItem
        } = options;
        if (changedItem && ("data" === changedItem.rowType || "group" === changedItem.rowType)) {
            var {
                focusedRowIndex: focusedRowIndex
            } = options;
            var {
                $tableElement: $tableElement
            } = options;
            var tabIndex = this.option("tabindex") || 0;
            var _rowsView = this.getView("rowsView");
            $row = $(_rowsView._getRowElements($tableElement).eq(focusedRowIndex));
            $row.addClass(ROW_FOCUSED_CLASS).attr("tabindex", tabIndex)
        }
        return $row
    }
}
var keyboardNavigation = Base => class extends Base {
    init() {
        var rowIndex = this.option("focusedRowIndex");
        var columnIndex = this.option("focusedColumnIndex");
        this.createAction("onFocusedRowChanging", {
            excludeValidators: ["disabled", "readOnly"]
        });
        this.createAction("onFocusedRowChanged", {
            excludeValidators: ["disabled", "readOnly"]
        });
        this.createAction("onFocusedCellChanging", {
            excludeValidators: ["disabled", "readOnly"]
        });
        this.createAction("onFocusedCellChanged", {
            excludeValidators: ["disabled", "readOnly"]
        });
        super.init();
        this.setRowFocusType();
        this._focusedCellPosition = {};
        if (isDefined(rowIndex) && rowIndex >= 0) {
            this._focusedCellPosition.rowIndex = rowIndex
        }
        if (isDefined(columnIndex) && columnIndex >= 0) {
            this._focusedCellPosition.columnIndex = columnIndex
        }
    }
    setFocusedRowIndex(rowIndex) {
        super.setFocusedRowIndex(rowIndex);
        this.option("focusedRowIndex", rowIndex)
    }
    setFocusedColumnIndex(columnIndex) {
        super.setFocusedColumnIndex(columnIndex);
        this.option("focusedColumnIndex", columnIndex)
    }
    _escapeKeyHandler(eventArgs, isEditing) {
        if (isEditing || !this.option("focusedRowEnabled")) {
            return super._escapeKeyHandler(eventArgs, isEditing)
        }
        if (this.isCellFocusType()) {
            this.setRowFocusType();
            this._focus(this._getCellElementFromTarget(eventArgs.originalEvent.target), true);
            return true
        }
        return false
    }
    _updateFocusedCellPosition($cell, direction) {
        var position = super._updateFocusedCellPosition($cell, direction);
        if (position && position.columnIndex >= 0) {
            this._fireFocusedCellChanged($cell)
        }
        return position
    }
};
var editorFactory = Base => class extends Base {
    renderFocusOverlay($element, isHideBorder) {
        var _a;
        var focusedRowEnabled = this.option("focusedRowEnabled");
        var $cell;
        if (!focusedRowEnabled || !(null === (_a = this._keyboardNavigationController) || void 0 === _a ? void 0 : _a.isRowFocusType()) || this._editingController.isEditing()) {
            super.renderFocusOverlay($element, isHideBorder)
        } else if (focusedRowEnabled) {
            var isRowElement = "row" === this._keyboardNavigationController._getElementType($element);
            if (isRowElement && !$element.hasClass(ROW_FOCUSED_CLASS)) {
                $cell = this._keyboardNavigationController.getFirstValidCellInRow($element);
                this._keyboardNavigationController.focus($cell)
            }
        }
    }
};
var columns = Base => class extends Base {
    getSortDataSourceParameters(_, sortByKey) {
        var result = super.getSortDataSourceParameters.apply(this, arguments);
        var dataSource = this._dataController._dataSource;
        var store = this._dataController.store();
        var key = store && store.key();
        var remoteOperations = dataSource && dataSource.remoteOperations() || {};
        var isLocalOperations = Object.keys(remoteOperations).every(operationName => !remoteOperations[operationName]);
        if (key && (this.option("focusedRowEnabled") && false !== this._focusController.isAutoNavigateToFocusedRow() || sortByKey)) {
            key = Array.isArray(key) ? key : [key];
            var notSortedKeys = key.filter(key => !this.columnOption(key, "sortOrder"));
            if (notSortedKeys.length) {
                result = result || [];
                if (isLocalOperations) {
                    result.push({
                        selector: dataSource.getDataIndexGetter(),
                        desc: false
                    })
                } else {
                    notSortedKeys.forEach(notSortedKey => result.push({
                        selector: notSortedKey,
                        desc: false
                    }))
                }
            }
        }
        return result
    }
};
var data = Base => class extends Base {
    _applyChange(change) {
        if (change && "updateFocusedRow" === change.changeType) {
            return
        }
        return super._applyChange.apply(this, arguments)
    }
    _fireChanged(e) {
        super._fireChanged(e);
        if (this.option("focusedRowEnabled") && this._dataSource) {
            var isPartialUpdate = "update" === e.changeType && e.repaintChangesOnly;
            var isPartialUpdateWithDeleting = isPartialUpdate && e.changeTypes && e.changeTypes.indexOf("remove") >= 0;
            if ("refresh" === e.changeType && e.items.length || isPartialUpdateWithDeleting) {
                this._updatePageIndexes();
                this._updateFocusedRow(e)
            } else if ("append" === e.changeType || "prepend" === e.changeType) {
                this._updatePageIndexes()
            } else if ("update" === e.changeType && e.repaintChangesOnly) {
                this._updateFocusedRow(e)
            }
        }
    }
    _updatePageIndexes() {
        var prevRenderingPageIndex = this._lastRenderingPageIndex || 0;
        var renderingPageIndex = this._rowsScrollController ? this._rowsScrollController.pageIndex() : 0;
        this._lastRenderingPageIndex = renderingPageIndex;
        this._isPagingByRendering = renderingPageIndex !== prevRenderingPageIndex
    }
    isPagingByRendering() {
        return this._isPagingByRendering
    }
    _updateFocusedRow(e) {
        var operationTypes = e.operationTypes || {};
        var {
            reload: reload,
            fullReload: fullReload,
            pageIndex: pageIndex,
            paging: paging
        } = operationTypes;
        var isVirtualScrolling = this._keyboardNavigationController._isVirtualScrolling();
        var pagingWithoutVirtualScrolling = paging && !isVirtualScrolling;
        var focusedRowKey = this.option("focusedRowKey");
        var isAutoNavigate = this._focusController.isAutoNavigateToFocusedRow();
        var isReload = reload && false === pageIndex;
        if (isReload && !fullReload && isDefined(focusedRowKey)) {
            this._focusController._navigateToRow(focusedRowKey, true).done(focusedRowIndex => {
                if (focusedRowIndex < 0) {
                    this._focusController._focusRowByIndex(void 0, operationTypes)
                }
            })
        } else if (pagingWithoutVirtualScrolling && isAutoNavigate) {
            var rowIndexByKey = this.getRowIndexByKey(focusedRowKey);
            var focusedRowIndex = this.option("focusedRowIndex");
            var isValidRowIndexByKey = rowIndexByKey >= 0;
            var isValidFocusedRowIndex = focusedRowIndex >= 0;
            var isSameRowIndex = focusedRowIndex === rowIndexByKey;
            if (isValidFocusedRowIndex && (isSameRowIndex || !isValidRowIndexByKey)) {
                this._focusController._focusRowByIndex(focusedRowIndex, operationTypes)
            }
        } else if (pagingWithoutVirtualScrolling && !isAutoNavigate && this.getRowIndexByKey(focusedRowKey) < 0) {
            this.option("focusedRowIndex", -1)
        } else if (operationTypes.fullReload) {
            this._focusController._focusRowByKeyOrIndex()
        }
    }
    getPageIndexByKey(key) {
        var that = this;
        var d = new Deferred;
        that.getGlobalRowIndexByKey(key).done(globalIndex => {
            d.resolve(globalIndex >= 0 ? Math.floor(globalIndex / that.pageSize()) : -1)
        }).fail(d.reject);
        return d.promise()
    }
    getGlobalRowIndexByKey(key) {
        if (this._dataSource.group()) {
            return this._calculateGlobalRowIndexByGroupedData(key)
        }
        return this._calculateGlobalRowIndexByFlatData(key)
    }
    _calculateGlobalRowIndexByFlatData(key, groupFilter, useGroup) {
        var that = this;
        var deferred = new Deferred;
        var dataSource = that._dataSource;
        if (Array.isArray(key) || isNewRowTempKey(key)) {
            return deferred.resolve(-1).promise()
        }
        var filter = that._generateFilterByKey(key);
        dataSource.load({
            filter: that._concatWithCombinedFilter(filter),
            skip: 0,
            take: 1
        }).done(data => {
            if (data.length > 0) {
                filter = that._generateOperationFilterByKey(key, data[0], useGroup);
                dataSource.load({
                    filter: that._concatWithCombinedFilter(filter, groupFilter),
                    skip: 0,
                    take: 1,
                    requireTotalCount: true
                }).done((_, extra) => {
                    deferred.resolve(extra.totalCount)
                })
            } else {
                deferred.resolve(-1)
            }
        });
        return deferred.promise()
    }
    _concatWithCombinedFilter(filter, groupFilter) {
        var combinedFilter = this.getCombinedFilter();
        return gridCoreUtils.combineFilters([filter, combinedFilter, groupFilter])
    }
    _generateBooleanFilter(selector, value, sortInfo) {
        var {
            desc: desc
        } = sortInfo;
        switch (true) {
            case false === value && desc:
                return [selector, "=", true];
            case false === value && !desc:
                return [selector, "=", null];
            case true === value && !desc:
            case !isBoolean(value) && desc:
                return [selector, "<>", value];
            default:
                return
        }
    }
    _generateOperationFilterByKey(key, rowData, useGroup) {
        var that = this;
        var dateSerializationFormat = that.option("dateSerializationFormat");
        var isRemoteFiltering = that._dataSource.remoteOperations().filtering;
        var isRemoteSorting = that._dataSource.remoteOperations().sorting;
        var filter = that._generateFilterByKey(key, "<");
        var sort = that._columnsController.getSortDataSourceParameters(!isRemoteFiltering, true);
        if (useGroup) {
            var group = that._columnsController.getGroupDataSourceParameters(!isRemoteFiltering);
            if (group) {
                sort = sort ? group.concat(sort) : group
            }
        }
        if (sort) {
            sort.slice().reverse().forEach(sortInfo => {
                var {
                    selector: selector,
                    desc: desc,
                    compare: compare
                } = sortInfo;
                var {
                    getter: getter,
                    rawValue: rawValue,
                    safeValue: safeValue
                } = UiGridCoreFocusUtils.getSortFilterValue(sortInfo, rowData, {
                    isRemoteFiltering: isRemoteFiltering,
                    dateSerializationFormat: dateSerializationFormat,
                    getSelector: selector => that._columnsController.columnOption(selector, "selector")
                });
                filter = [
                    [selector, "=", safeValue], "and", filter
                ];
                if (null === rawValue || isBoolean(rawValue)) {
                    var booleanFilter = that._generateBooleanFilter(selector, safeValue, desc);
                    if (booleanFilter) {
                        filter = [booleanFilter, "or", filter]
                    }
                } else {
                    var filterOperation = desc ? ">" : "<";
                    var sortFilter;
                    if (compare && !isRemoteSorting) {
                        sortFilter = data => {
                            if ("<" === filterOperation) {
                                return compare(rawValue, getter(data)) >= 1
                            }
                            return compare(rawValue, getter(data)) <= -1
                        }
                    } else {
                        sortFilter = [selector, filterOperation, safeValue];
                        if (!desc) {
                            sortFilter = [sortFilter, "or", [selector, "=", null]]
                        }
                    }
                    filter = [sortFilter, "or", filter]
                }
            })
        }
        return filter
    }
    _generateFilterByKey(key, operation) {
        var dataSourceKey = this._dataSource.key();
        var filter = [];
        if (!operation) {
            operation = "="
        }
        if (Array.isArray(dataSourceKey)) {
            for (var i = 0; i < dataSourceKey.length; ++i) {
                var keyPart = key[dataSourceKey[i]];
                if (keyPart) {
                    if (filter.length > 0) {
                        filter.push("and")
                    }
                    filter.push([dataSourceKey[i], operation, keyPart])
                }
            }
        } else {
            filter = [dataSourceKey, operation, key]
        }
        return filter
    }
    _getLastItemIndex() {
        return this.items(true).length - 1
    }
};
var editing = Base => class extends Base {
    _deleteRowCore(rowIndex) {
        var deferred = super._deleteRowCore.apply(this, arguments);
        var rowKey = this._dataController.getKeyByRowIndex(rowIndex);
        deferred.done(() => {
            var rowIndex = this._dataController.getRowIndexByKey(rowKey);
            var visibleRows = this._dataController.getVisibleRows();
            if (-1 === rowIndex && !visibleRows.length) {
                this._focusController._resetFocusedRow()
            }
        })
    }
};
var rowsView = Base => class extends Base {
    _createRow(row) {
        var $row = super._createRow.apply(this, arguments);
        if (this.option("focusedRowEnabled") && row) {
            if (this._focusController.isRowFocused(row.key)) {
                $row.addClass(ROW_FOCUSED_CLASS)
            }
        }
        return $row
    }
    _checkRowKeys(options) {
        super._checkRowKeys.apply(this, arguments);
        if (this.option("focusedRowEnabled") && this.option("dataSource")) {
            var store = this._dataController.store();
            if (store && !store.key()) {
                this._dataController.fireError("E1042", "Row focusing")
            }
        }
    }
    _update(change) {
        if ("updateFocusedRow" === change.changeType) {
            if (this.option("focusedRowEnabled")) {
                this._focusController.updateFocusedRow(change)
            }
        } else {
            super._update(change)
        }
    }
    updateFocusElementTabIndex($cellElements, preventScroll) {
        if (this.option("focusedRowEnabled")) {
            this._setFocusedRowElementTabIndex(preventScroll)
        } else {
            super.updateFocusElementTabIndex($cellElements)
        }
    }
    _setFocusedRowElementTabIndex(preventScroll) {
        var _a;
        var focusedRowKey = this.option("focusedRowKey");
        var tabIndex = null !== (_a = this.option("tabIndex")) && void 0 !== _a ? _a : 0;
        var columnsController = this._columnsController;
        var rowIndex = this._dataController.getRowIndexByKey(focusedRowKey);
        var columnIndex = this.option("focusedColumnIndex");
        var $row = this._findRowElementForTabIndex();
        var dataSource = this._dataController.dataSource();
        var operationTypes = null === dataSource || void 0 === dataSource ? void 0 : dataSource.operationTypes();
        var isPaging = !operationTypes || operationTypes.paging;
        if (!isDefined(this._scrollToFocusOnResize)) {
            this._scrollToFocusOnResize = () => {
                this.scrollToElementVertically(this._findRowElementForTabIndex());
                this.resizeCompleted.remove(this._scrollToFocusOnResize)
            }
        }
        $row.attr("tabIndex", tabIndex);
        var rowIndexFromOption = this.option("focusedRowIndex") - this._dataController.getRowIndexOffset(true);
        if (!isPaging && rowIndex < 0 && rowIndexFromOption >= 0) {
            this._focusController.updateFocusedRow({
                focusedRowIndex: rowIndexFromOption,
                preventScroll: preventScroll
            })
        }
        if (rowIndex >= 0 && !preventScroll) {
            if (columnIndex < 0) {
                columnIndex = 0
            }
            rowIndex += this._dataController.getRowIndexOffset();
            columnIndex += columnsController.getColumnIndexOffset();
            this._keyboardNavigationController.setFocusedCellPosition(rowIndex, columnIndex);
            if (this._focusController.isAutoNavigateToFocusedRow()) {
                if (!isPaging && !this._dataController.isPagingByRendering()) {
                    this.resizeCompleted.remove(this._scrollToFocusOnResize);
                    this.resizeCompleted.add(this._scrollToFocusOnResize)
                }
            }
        }
    }
    _findRowElementForTabIndex() {
        var focusedRowKey = this.option("focusedRowKey");
        var rowIndex = this._dataController.getRowIndexByKey(focusedRowKey);
        return $(this.getRowElement(rowIndex >= 0 ? rowIndex : 0))
    }
    scrollToRowElement(key) {
        var rowIndex = this._dataController.getRowIndexByKey(key);
        var $row = $(this.getRow(rowIndex));
        return this.scrollToElementVertically($row)
    }
    scrollToElementVertically($row) {
        var scrollable = this.getScrollable();
        if (scrollable && $row.length) {
            var position = scrollable.getScrollElementPosition($row, "vertical");
            return this.scrollTopPosition(position)
        }
        return (new Deferred).resolve()
    }
    scrollTopPosition(scrollTop) {
        var d = new Deferred;
        var scrollable = this.getScrollable();
        if (scrollable) {
            var currentScrollTop = scrollable.scrollTop();
            var scrollHandler = () => {
                scrollable.off("scroll", scrollHandler);
                d.resolve()
            };
            if (scrollTop !== currentScrollTop) {
                scrollable.on("scroll", scrollHandler);
                this._dataController.resetFilterApplying();
                scrollable.scrollTo({
                    top: scrollTop
                });
                return d.promise()
            }
        }
        return d.resolve()
    }
};
export var focusModule = {
    defaultOptions: () => ({
        focusedRowEnabled: false,
        autoNavigateToFocusedRow: true,
        focusedRowKey: null,
        focusedRowIndex: -1,
        focusedColumnIndex: -1
    }),
    controllers: {
        focus: FocusController
    },
    extenders: {
        controllers: {
            keyboardNavigation: keyboardNavigation,
            editorFactory: editorFactory,
            columns: columns,
            data: data,
            editing: editing
        },
        views: {
            rowsView: rowsView
        }
    }
};
