/**
 * DevExtreme (esm/__internal/grids/grid_core/keyboard_navigation/scrollable_a11y.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 {
    isDefined,
    isEmptyObject
} from "../../../../core/utils/type";
import eventsEngine from "../../../../events/core/events_engine";
export var keyboardNavigationScrollableA11yExtender = Base => class extends Base {
    init() {
        var _a;
        super.init();
        this.rowsViewFocusOutHandlerContext = null !== (_a = this.rowsViewFocusOutHandlerContext) && void 0 !== _a ? _a : this.rowsViewFocusOutHandler.bind(this)
    }
    subscribeToRowsViewFocusEvent() {
        var _a;
        super.subscribeToRowsViewFocusEvent();
        var $rowsView = null === (_a = this._rowsView) || void 0 === _a ? void 0 : _a.element();
        eventsEngine.on($rowsView, "focusout", this.rowsViewFocusOutHandlerContext)
    }
    unsubscribeFromRowsViewFocusEvent() {
        var _a;
        super.unsubscribeFromRowsViewFocusEvent();
        var $rowsView = null === (_a = this._rowsView) || void 0 === _a ? void 0 : _a.element();
        eventsEngine.off($rowsView, "focusout", this.rowsViewFocusOutHandlerContext)
    }
    rowsViewFocusHandler(event) {
        var $target = $(event.target);
        this.translateFocusIfNeed(event, $target);
        super.rowsViewFocusHandler(event)
    }
    rowsViewFocusOutHandler() {
        this.makeScrollableFocusableIfNeed()
    }
    translateFocusIfNeed(event, $target) {
        var needTranslateFocus = this.isScrollableNeedFocusable();
        var isFirstCellFixed = this._isFixedColumn(0);
        if (!needTranslateFocus || !isFirstCellFixed) {
            return
        }
        var $firstCell = this._rowsView.getCell({
            rowIndex: 0,
            columnIndex: 0
        });
        var firstCellHasTabIndex = !!$firstCell.attr("tabindex");
        var notFixedCellIsTarget = $target.is(this._$firstNotFixedCell);
        if (firstCellHasTabIndex && notFixedCellIsTarget) {
            event.preventDefault();
            this._focus($firstCell)
        }
    }
    renderCompleted(e) {
        this._$firstNotFixedCell = this.getFirstNotFixedCell();
        this.makeScrollableFocusableIfNeed();
        super.renderCompleted(e)
    }
    _focus($cell, disableFocus, skipFocusEvent) {
        super._focus($cell, disableFocus, skipFocusEvent);
        this.makeScrollableFocusableIfNeed()
    }
    _tabKeyHandler(eventArgs, isEditing) {
        var _a;
        var isCellPositionDefined = isDefined(this._focusedCellPosition) && !isEmptyObject(this._focusedCellPosition);
        var isOriginalHandlerRequired = !isCellPositionDefined || !eventArgs.shift && this._isLastValidCell(this._focusedCellPosition) || eventArgs.shift && this._isFirstValidCell(this._focusedCellPosition);
        var isNeedFocusable = this.isScrollableNeedFocusable();
        if (isOriginalHandlerRequired && isNeedFocusable) {
            null === (_a = this._$firstNotFixedCell) || void 0 === _a ? void 0 : _a.removeAttr("tabIndex")
        }
        super._tabKeyHandler(eventArgs, isEditing)
    }
    getFirstNotFixedCell() {
        var columns = this._columnsController.getVisibleColumns();
        var columnIndex = columns.findIndex(_ref => {
            var {
                fixed: fixed
            } = _ref;
            return !fixed
        });
        return -1 === columnIndex ? void 0 : this._rowsView._getCellElement(0, columnIndex)
    }
    isScrollableNeedFocusable() {
        var _a, _b;
        var hasScrollable = !!this._rowsView.getScrollable();
        var hasFixedTable = !!(null === (_a = this._rowsView._fixedTableElement) || void 0 === _a ? void 0 : _a.length);
        var isCellsRendered = !!(null === (_b = this._rowsView.getCellElements(0)) || void 0 === _b ? void 0 : _b.length);
        return hasScrollable && hasFixedTable && isCellsRendered
    }
    makeScrollableFocusableIfNeed() {
        var needFocusable = this.isScrollableNeedFocusable();
        if (!needFocusable || !this._$firstNotFixedCell) {
            return
        }
        this._applyTabIndexToElement(this._$firstNotFixedCell)
    }
};
