/**
 * DevExtreme (cjs/__internal/grids/grid_core/column_state_mixin/m_column_state_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/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = exports.ColumnStateMixin = void 0;
var _renderer = _interopRequireDefault(require("../../../../core/renderer"));
var _extend = require("../../../../core/utils/extend");
var _position = require("../../../../core/utils/position");

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)
}
const COLUMN_INDICATORS_CLASS = "dx-column-indicators";
const GROUP_PANEL_ITEM_CLASS = "dx-group-panel-item";
const ColumnStateMixin = Base => function(_Base) {
    _inheritsLoose(_class, _Base);

    function _class() {
        return _Base.apply(this, arguments) || this
    }
    var _proto = _class.prototype;
    _proto._applyColumnState = function(options) {
        var _a;
        const rtlEnabled = this.option("rtlEnabled");
        const columnAlignment = this._getColumnAlignment(options.column.alignment, rtlEnabled);
        const parameters = (0, _extend.extend)(true, {
            columnAlignment: columnAlignment
        }, options);
        const isGroupPanelItem = parameters.rootElement.hasClass("dx-group-panel-item");
        const $indicatorsContainer = this._createIndicatorContainer(parameters, isGroupPanelItem);
        const $span = (0, _renderer.default)("<span>").addClass(this._getIndicatorClassName(options.name));
        const columnsController = null === (_a = this.component) || void 0 === _a ? void 0 : _a.getController("columns");
        const indicatorAlignment = (null === columnsController || void 0 === columnsController ? void 0 : columnsController.getHeaderContentAlignment(columnAlignment)) || columnAlignment;
        parameters.container = $indicatorsContainer;
        parameters.indicator = $span;
        this._renderIndicator(parameters);
        $indicatorsContainer[(isGroupPanelItem || !options.showColumnLines) && "left" === indicatorAlignment ? "appendTo" : "prependTo"](options.rootElement);
        return $span
    };
    _proto._getIndicatorClassName = function(name) {};
    _proto._getColumnAlignment = function(alignment, rtlEnabled) {
        rtlEnabled = rtlEnabled || this.option("rtlEnabled");
        return alignment && "center" !== alignment ? alignment : (0, _position.getDefaultAlignment)(rtlEnabled)
    };
    _proto._createIndicatorContainer = function(options, ignoreIndicatorAlignment) {
        let $indicatorsContainer = this._getIndicatorContainer(options.rootElement);
        const indicatorAlignment = "left" === options.columnAlignment ? "right" : "left";
        if (!$indicatorsContainer.length) {
            $indicatorsContainer = (0, _renderer.default)("<div>").addClass("dx-column-indicators")
        }
        this.setAria("role", "presentation", $indicatorsContainer);
        return $indicatorsContainer.css("float", options.showColumnLines && !ignoreIndicatorAlignment ? indicatorAlignment : null)
    };
    _proto._getIndicatorContainer = function($cell) {
        return $cell && $cell.find(".".concat("dx-column-indicators"))
    };
    _proto._getIndicatorElements = function($cell) {
        const $indicatorContainer = this._getIndicatorContainer($cell);
        return $indicatorContainer && $indicatorContainer.children()
    };
    _proto._renderIndicator = function(options) {
        const $container = options.container;
        const $indicator = options.indicator;
        $container && $indicator && $container.append($indicator)
    };
    _proto._updateIndicators = function(indicatorName) {
        const that = this;
        const columns = that.getColumns();
        const $cells = that.getColumnElements();
        let $cell;
        if (!$cells || columns.length !== $cells.length) {
            return
        }
        for (let i = 0; i < columns.length; i++) {
            $cell = $cells.eq(i);
            that._updateIndicator($cell, columns[i], indicatorName);
            const rowOptions = $cell.parent().data("options");
            if (rowOptions && rowOptions.cells) {
                rowOptions.cells[$cell.index()].column = columns[i]
            }
        }
    };
    _proto._updateIndicator = function($cell, column, indicatorName) {
        if (!column.command) {
            return this._applyColumnState({
                name: indicatorName,
                rootElement: $cell,
                column: column,
                showColumnLines: this.option("showColumnLines")
            })
        }
        return
    };
    return _class
}(Base);
exports.ColumnStateMixin = ColumnStateMixin;
var _default = ColumnStateMixin;
exports.default = _default;
