/**
 * DevExtreme (cjs/__internal/grids/tree_list/rows/m_rows.js)
 * Version: 23.1.8 (build 24063-0109)
 * Build date: Sun Mar 03 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
var _renderer = _interopRequireDefault(require("../../../../core/renderer"));
var _events_engine = _interopRequireDefault(require("../../../../events/core/events_engine"));
var _remove = require("../../../../events/remove");
var _m_rows_view = require("../../../grids/grid_core/views/m_rows_view");
var _m_core = _interopRequireDefault(require("../m_core"));

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 TREELIST_TEXT_CONTENT = "dx-treelist-text-content";
var TREELIST_EXPAND_ICON_CONTAINER_CLASS = "dx-treelist-icon-container";
var TREELIST_CELL_EXPANDABLE_CLASS = "dx-treelist-cell-expandable";
var TREELIST_EMPTY_SPACE = "dx-treelist-empty-space";
var TREELIST_EXPANDED_CLASS = "dx-treelist-expanded";
var TREELIST_COLLAPSED_CLASS = "dx-treelist-collapsed";
var createCellContent = function($container) {
    return (0, _renderer.default)("<div>").addClass(TREELIST_TEXT_CONTENT).appendTo($container)
};
var createIcon = function(hasIcon, isExpanded) {
    var $iconElement = (0, _renderer.default)("<div>").addClass(TREELIST_EMPTY_SPACE);
    if (hasIcon) {
        $iconElement.toggleClass(TREELIST_EXPANDED_CLASS, isExpanded).toggleClass(TREELIST_COLLAPSED_CLASS, !isExpanded).append((0, _renderer.default)("<span>"))
    }
    return $iconElement
};
var TreeListRowsView = function(_RowsView) {
    _inheritsLoose(TreeListRowsView, _RowsView);

    function TreeListRowsView() {
        return _RowsView.apply(this, arguments) || this
    }
    var _proto = TreeListRowsView.prototype;
    _proto._renderIconContainer = function($container, options) {
        var _this = this;
        var $iconContainer = (0, _renderer.default)("<div>").addClass(TREELIST_EXPAND_ICON_CONTAINER_CLASS).appendTo($container);
        if (options.watch) {
            var dispose = options.watch((function() {
                return [options.row.level, options.row.isExpanded, options.row.node.hasChildren]
            }), (function() {
                $iconContainer.empty();
                _this._renderIcons($iconContainer, options)
            }));
            _events_engine.default.on($iconContainer, _remove.removeEvent, dispose)
        }
        $container.addClass(TREELIST_CELL_EXPANDABLE_CLASS);
        return this._renderIcons($iconContainer, options)
    };
    _proto._renderIcons = function($iconContainer, options) {
        var row = options.row;
        var level = row.level;
        for (var i = 0; i <= level; i++) {
            $iconContainer.append(createIcon(i === level && row.node.hasChildren, row.isExpanded))
        }
        return $iconContainer
    };
    _proto._renderCellCommandContent = function(container, model) {
        this._renderIconContainer(container, model);
        return true
    };
    _proto._processTemplate = function(template, options) {
        var _a;
        var that = this;
        var resultTemplate;
        var renderingTemplate = _RowsView.prototype._processTemplate.call(this, template);
        var firstDataColumnIndex = that._columnsController.getFirstDataColumnIndex();
        if (renderingTemplate && (null === (_a = options.column) || void 0 === _a ? void 0 : _a.index) === firstDataColumnIndex) {
            resultTemplate = {
                render: function(options) {
                    var $container = options.container;
                    if (that._renderCellCommandContent($container, options.model)) {
                        options.container = createCellContent($container)
                    }
                    renderingTemplate.render(options)
                }
            }
        } else {
            resultTemplate = renderingTemplate
        }
        return resultTemplate
    };
    _proto._updateCell = function($cell, options) {
        $cell = $cell.hasClass(TREELIST_TEXT_CONTENT) ? $cell.parent() : $cell;
        _RowsView.prototype._updateCell.call(this, $cell, options)
    };
    _proto._rowClick = function(e) {
        var dataController = this._dataController;
        var $targetElement = (0, _renderer.default)(e.event.target);
        var isExpandIcon = this.isExpandIcon($targetElement);
        var item = null === dataController || void 0 === dataController ? void 0 : dataController.items()[e.rowIndex];
        if (isExpandIcon && item) {
            dataController.changeRowExpand(item.key)
        }
        _RowsView.prototype._rowClick.call(this, e)
    };
    _proto._createRow = function(row) {
        var node = row && row.node;
        var $rowElement = _RowsView.prototype._createRow.apply(this, arguments);
        if (node) {
            this.setAria("level", row.level + 1, $rowElement);
            if (node.hasChildren) {
                this.setAria("expanded", row.isExpanded, $rowElement)
            }
        }
        return $rowElement
    };
    _proto.isExpandIcon = function($targetElement) {
        return !!$targetElement.closest(".".concat(TREELIST_EXPANDED_CLASS, ", .").concat(TREELIST_COLLAPSED_CLASS)).length
    };
    _proto.setTableRole = function($tableElement) {
        this.setAria("role", "treegrid", $tableElement)
    };
    return TreeListRowsView
}(_m_rows_view.RowsView);
_m_core.default.registerModule("rows", {
    defaultOptions: _m_rows_view.rowsModule.defaultOptions,
    views: {
        rowsView: TreeListRowsView
    }
});
