/**
 * DevExtreme (esm/__internal/grids/grid_core/sorting/m_sorting_mixin.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
} from "../../../../core/utils/type";
import messageLocalization from "../../../../localization/message";
var SORT_CLASS = "dx-sort";
var SORT_NONE_CLASS = "dx-sort-none";
var SORTUP_CLASS = "dx-sort-up";
var SORTDOWN_CLASS = "dx-sort-down";
var SORT_INDEX_CLASS = "dx-sort-index";
var SORT_INDEX_ICON_CLASS = "dx-sort-index-icon";
var HEADERS_ACTION_CLASS = "action";
var sortingMixin = Base => class extends Base {
    _applyColumnState(options) {
        var ariaSortState;
        var $sortIndicator;
        var sortingMode = this.option("sorting.mode");
        var {
            rootElement: rootElement
        } = options;
        var {
            column: column
        } = options;
        var $indicatorsContainer = this._getIndicatorContainer(rootElement);
        if ("sort" === options.name) {
            rootElement.find(".".concat(SORT_CLASS)).remove();
            !$indicatorsContainer.children().length && $indicatorsContainer.remove();
            var isSortingAllowed = "none" !== sortingMode && column.allowSorting;
            var hasSeveralSortIndexes = this.getController && !!this.getController("columns").columnOption("sortIndex:1");
            if (!isDefined(column.groupIndex) && (isSortingAllowed || isDefined(column.sortOrder))) {
                ariaSortState = "asc" === column.sortOrder ? "ascending" : "descending";
                $sortIndicator = super._applyColumnState(options).toggleClass(SORTUP_CLASS, "asc" === column.sortOrder).toggleClass(SORTDOWN_CLASS, "desc" === column.sortOrder);
                if (hasSeveralSortIndexes && this.option("sorting.showSortIndexes") && column.sortIndex >= 0) {
                    $("<span>").addClass(SORT_INDEX_ICON_CLASS).text(column.sortIndex + 1).appendTo($sortIndicator);
                    $sortIndicator.addClass(SORT_INDEX_CLASS)
                }
                if (isSortingAllowed) {
                    options.rootElement.addClass(this.addWidgetPrefix(HEADERS_ACTION_CLASS))
                }
            }
            this._setAriaSortAttribute(column, ariaSortState, rootElement, hasSeveralSortIndexes);
            return $sortIndicator
        }
        return super._applyColumnState(options)
    }
    _setAriaSortAttribute(column, ariaSortState, $rootElement, hasSeveralSortIndexes) {
        $rootElement.removeAttr("aria-roledescription");
        if (column.isGrouped) {
            var description = this.localize("dxDataGrid-ariaNotSortedColumn");
            if (isDefined(column.sortOrder)) {
                description = "asc" === column.sortOrder ? this.localize("dxDataGrid-ariaSortedAscendingColumn") : this.localize("dxDataGrid-ariaSortedDescendingColumn")
            }
            this.setAria("roledescription", description, $rootElement)
        } else if (!isDefined(column.sortOrder)) {
            this.setAria("sort", "none", $rootElement)
        } else {
            this.setAria("sort", ariaSortState, $rootElement);
            if (hasSeveralSortIndexes && column.sortIndex >= 0) {
                var ariaColumnHeader = messageLocalization.format("dxDataGrid-ariaColumnHeader");
                var ariaSortIndex = messageLocalization.format("dxDataGrid-ariaSortIndex", column.sortIndex + 1);
                var _description = "".concat(ariaColumnHeader, ", ").concat(ariaSortIndex);
                this.setAria("roledescription", _description, $rootElement)
            }
        }
    }
    _getIndicatorClassName(name) {
        if ("sort" === name) {
            return SORT_CLASS
        }
        if ("sortIndex" === name) {
            return SORT_INDEX_ICON_CLASS
        }
        return super._getIndicatorClassName(name)
    }
    _renderIndicator(options) {
        var {
            column: column
        } = options;
        var $container = options.container;
        var $indicator = options.indicator;
        if ("sort" === options.name) {
            var rtlEnabled = this.option("rtlEnabled");
            if (!isDefined(column.sortOrder)) {
                $indicator && $indicator.addClass(SORT_NONE_CLASS)
            }
            if ($container.children().length && (!rtlEnabled && "left" === options.columnAlignment || rtlEnabled && "right" === options.columnAlignment)) {
                $container.prepend($indicator);
                return
            }
        }
        super._renderIndicator(options)
    }
    _updateIndicator($cell, column, indicatorName) {
        if ("sort" === indicatorName && isDefined(column.groupIndex)) {
            return
        }
        return super._updateIndicator.apply(this, arguments)
    }
    _getIndicatorElements($cell, returnAll) {
        var $indicatorElements = super._getIndicatorElements($cell);
        return returnAll ? $indicatorElements : $indicatorElements && $indicatorElements.not(".".concat(SORT_NONE_CLASS))
    }
};
export default sortingMixin;
