/**
 * DevExtreme (esm/__internal/ui/toolbar/m_toolbar.utils.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 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";
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]
};
export function toggleItemFocusableElementTabIndex(context, item) {
    var _itemData$options;
    if (!context) {
        return
    }
    const $item = context._findItemElementByItem(item);
    if (!$item.length) {
        return
    }
    const itemData = context._getItemData($item);
    const isItemNotFocusable = !!(null !== (_itemData$options = itemData.options) && void 0 !== _itemData$options && _itemData$options.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) {
            var _itemInstance$_focusT, _itemData$options2;
            const itemInstance = getItemInstance($widget);
            if (!itemInstance) {
                return
            }
            let $focusTarget = null === (_itemInstance$_focusT = itemInstance._focusTarget) || void 0 === _itemInstance$_focusT ? void 0 : _itemInstance$_focusT.call(itemInstance);
            if ("dxDropDownButton" === widget) {
                $focusTarget = $focusTarget && $focusTarget.find(".dx-buttongroup")
            } else {
                $focusTarget = $focusTarget ?? $(itemInstance.element())
            }
            const tabIndex = null === (_itemData$options2 = itemData.options) || void 0 === _itemData$options2 ? void 0 : _itemData$options2.tabIndex;
            if (isItemNotFocusable) {
                $focusTarget.attr("tabIndex", -1)
            } else {
                $focusTarget.attr("tabIndex", tabIndex ?? 0)
            }
        }
    }
}
