/**
 * DevExtreme (cjs/__internal/ui/toolbar/m_toolbar.utils.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.toggleItemFocusableElementTabIndex = toggleItemFocusableElementTabIndex;
var _renderer = _interopRequireDefault(require("../../../core/renderer"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const BUTTON_GROUP_CLASS = "dx-buttongroup";
const TOOLBAR_ITEMS = ["dxAutocomplete", "dxButton", "dxCheckBox", "dxDateBox", "dxMenu", "dxSelectBox", "dxTabs", "dxTextBox", "dxButtonGroup", "dxDropDownButton"];
const getItemInstance = function($element) {
    const itemData = $element.data && $element.data();
    const dxComponents = itemData && itemData.dxComponents;
    const widgetName = dxComponents && dxComponents[0];
    return widgetName && itemData[widgetName]
};

function toggleItemFocusableElementTabIndex(context, item) {
    var _a, _b, _c;
    if (!context) {
        return
    }
    const $item = context._findItemElementByItem(item);
    if (!$item.length) {
        return
    }
    const itemData = context._getItemData($item);
    const isItemNotFocusable = !!((null === (_a = itemData.options) || void 0 === _a ? void 0 : _a.disabled) || itemData.disabled || context.option("disabled"));
    const {
        widget: widget
    } = itemData;
    if (widget && TOOLBAR_ITEMS.includes(widget)) {
        const $widget = $item.find(widget.toLowerCase().replace("dx", ".dx-"));
        if ($widget.length) {
            const itemInstance = getItemInstance($widget);
            if (!itemInstance) {
                return
            }
            let $focusTarget = null === (_b = itemInstance._focusTarget) || void 0 === _b ? void 0 : _b.call(itemInstance);
            if ("dxDropDownButton" === widget) {
                $focusTarget = $focusTarget && $focusTarget.find(".".concat("dx-buttongroup"))
            } else {
                $focusTarget = null !== $focusTarget && void 0 !== $focusTarget ? $focusTarget : (0, _renderer.default)(itemInstance.element())
            }
            const tabIndex = null === (_c = itemData.options) || void 0 === _c ? void 0 : _c.tabIndex;
            if (isItemNotFocusable) {
                $focusTarget.attr("tabIndex", -1)
            } else {
                $focusTarget.attr("tabIndex", null !== tabIndex && void 0 !== tabIndex ? tabIndex : 0)
            }
        }
    }
}
