/**
 * DevExtreme (cjs/__internal/grids/grid_core/sorting/m_sorting_mixin.js)
 * Version: 23.1.12
 * Build date: Fri Aug 09 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.default = void 0;
var _renderer = _interopRequireDefault(require("../../../../core/renderer"));
var _type = require("../../../../core/utils/type");
var _message = _interopRequireDefault(require("../../../../localization/message"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
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 = function(Base) {
    return function(_Base) {
        _inheritsLoose(SortingMixin, _Base);

        function SortingMixin() {
            return _Base.apply(this, arguments) || this
        }
        var _proto = SortingMixin.prototype;
        _proto._applyColumnState = function(options) {
            var ariaSortState;
            var $sortIndicator;
            var sortingMode = this.option("sorting.mode");
            var rootElement = options.rootElement;
            var column = options.column;
            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;
                if (!(0, _type.isDefined)(column.groupIndex) && (isSortingAllowed || (0, _type.isDefined)(column.sortOrder))) {
                    ariaSortState = "asc" === column.sortOrder ? "ascending" : "descending";
                    $sortIndicator = _Base.prototype._applyColumnState.call(this, options).toggleClass(SORTUP_CLASS, "asc" === column.sortOrder).toggleClass(SORTDOWN_CLASS, "desc" === column.sortOrder);
                    var hasSeveralSortIndexes = this.getController && !!this.getController("columns").columnOption("sortIndex:1");
                    if (hasSeveralSortIndexes && this.option("sorting.showSortIndexes") && column.sortIndex >= 0) {
                        (0, _renderer.default)("<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))
                    }
                }
                if (!(0, _type.isDefined)(column.sortOrder)) {
                    this.setAria("sort", "none", rootElement)
                } else {
                    this.setAria("sort", ariaSortState, rootElement)
                }
                return $sortIndicator
            }
            return _Base.prototype._applyColumnState.call(this, options)
        };
        _proto._setAriaSortAttribute = function(column, ariaSortState, $rootElement, hasSeveralSortIndexes) {
            $rootElement.removeAttr("aria-roledescription");
            if (column.isGrouped) {
                var description = this.localize("dxDataGrid-ariaNotSortedColumn");
                if ((0, _type.isDefined)(column.sortOrder)) {
                    description = "asc" === column.sortOrder ? this.localize("dxDataGrid-ariaSortedAscendingColumn") : this.localize("dxDataGrid-ariaSortedDescendingColumn")
                }
                this.setAria("roledescription", description, $rootElement)
            } else if (!(0, _type.isDefined)(column.sortOrder)) {
                this.setAria("sort", "none", $rootElement)
            } else {
                this.setAria("sort", ariaSortState, $rootElement);
                if (hasSeveralSortIndexes && column.sortIndex >= 0) {
                    var ariaColumnHeader = _message.default.format("dxDataGrid-ariaColumnHeader");
                    var ariaSortIndex = _message.default.format("dxDataGrid-ariaSortIndex", column.sortIndex + 1);
                    var _description = "".concat(ariaColumnHeader, ", ").concat(ariaSortIndex);
                    this.setAria("roledescription", _description, $rootElement)
                }
            }
        };
        _proto._getIndicatorClassName = function(name) {
            if ("sort" === name) {
                return SORT_CLASS
            }
            if ("sortIndex" === name) {
                return SORT_INDEX_ICON_CLASS
            }
            return _Base.prototype._getIndicatorClassName.call(this, name)
        };
        _proto._renderIndicator = function(options) {
            var column = options.column;
            var $container = options.container;
            var $indicator = options.indicator;
            if ("sort" === options.name) {
                var rtlEnabled = this.option("rtlEnabled");
                if (!(0, _type.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
                }
            }
            _Base.prototype._renderIndicator.call(this, options)
        };
        _proto._updateIndicator = function($cell, column, indicatorName) {
            if ("sort" === indicatorName && (0, _type.isDefined)(column.groupIndex)) {
                return
            }
            return _Base.prototype._updateIndicator.apply(this, arguments)
        };
        _proto._getIndicatorElements = function($cell, returnAll) {
            var $indicatorElements = _Base.prototype._getIndicatorElements.call(this, $cell);
            return returnAll ? $indicatorElements : $indicatorElements && $indicatorElements.not(".".concat(SORT_NONE_CLASS))
        };
        return SortingMixin
    }(Base)
};
var _default = sortingMixin;
exports.default = _default;
