/**
 * DevExtreme (esm/__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/
 */
import $ from "../../../core/renderer";
var BUTTON_GROUP_CLASS = "dx-buttongroup";
var TOOLBAR_ITEMS = ["dxAutocomplete", "dxButton", "dxCheckBox", "dxDateBox", "dxMenu", "dxSelectBox", "dxTabs", "dxTextBox", "dxButtonGroup", "dxDropDownButton"];
var getItemInstance = function($element) {
    var itemData = $element.data && $element.data();
    var dxComponents = itemData && itemData.dxComponents;
    var widgetName = dxComponents && dxComponents[0];
    return widgetName && itemData[widgetName]
};
export function toggleItemFocusableElementTabIndex(context, item) {
    var _a, _b, _c;
    if (!context) {
        return
    }
    var $item = context._findItemElementByItem(item);
    if (!$item.length) {
        return
    }
    var itemData = context._getItemData($item);
    var isItemNotFocusable = !!((null === (_a = itemData.options) || void 0 === _a ? void 0 : _a.disabled) || itemData.disabled || context.option("disabled"));
    var {
        widget: widget
    } = itemData;
    if (widget && TOOLBAR_ITEMS.includes(widget)) {
        var $widget = $item.find(widget.toLowerCase().replace("dx", ".dx-"));
        if ($widget.length) {
            var itemInstance = getItemInstance($widget);
            if (!itemInstance) {
                return
            }
            var $focusTarget = null === (_b = itemInstance._focusTarget) || void 0 === _b ? void 0 : _b.call(itemInstance);
            if ("dxDropDownButton" === widget) {
                $focusTarget = $focusTarget && $focusTarget.find(".".concat(BUTTON_GROUP_CLASS))
            } else {
                $focusTarget = null !== $focusTarget && void 0 !== $focusTarget ? $focusTarget : $(itemInstance.element())
            }
            var 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)
            }
        }
    }
}
