/**
 * DevExtreme (esm/__internal/grids/tree_list/rows/m_rows.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 eventsEngine from "../../../../events/core/events_engine";
import {
    removeEvent
} from "../../../../events/remove";
import {
    rowsModule,
    RowsView
} from "../../../grids/grid_core/views/m_rows_view";
import treeListCore from "../m_core";
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 $("<div>").addClass(TREELIST_TEXT_CONTENT).appendTo($container)
};
var createIcon = function(hasIcon, isExpanded) {
    var $iconElement = $("<div>").addClass(TREELIST_EMPTY_SPACE);
    if (hasIcon) {
        $iconElement.toggleClass(TREELIST_EXPANDED_CLASS, isExpanded).toggleClass(TREELIST_COLLAPSED_CLASS, !isExpanded).append($("<span>"))
    }
    return $iconElement
};
class TreeListRowsView extends RowsView {
    _renderIconContainer($container, options) {
        var $iconContainer = $("<div>").addClass(TREELIST_EXPAND_ICON_CONTAINER_CLASS).appendTo($container);
        if (options.watch) {
            var dispose = options.watch(() => [options.row.level, options.row.isExpanded, options.row.node.hasChildren], () => {
                $iconContainer.empty();
                this._renderIcons($iconContainer, options)
            });
            eventsEngine.on($iconContainer, removeEvent, dispose)
        }
        $container.addClass(TREELIST_CELL_EXPANDABLE_CLASS);
        return this._renderIcons($iconContainer, options)
    }
    _renderIcons($iconContainer, options) {
        var {
            row: row
        } = options;
        var {
            level: level
        } = row;
        for (var i = 0; i <= level; i++) {
            $iconContainer.append(createIcon(i === level && row.node.hasChildren, row.isExpanded))
        }
        return $iconContainer
    }
    _renderCellCommandContent(container, model) {
        this._renderIconContainer(container, model);
        return true
    }
    _processTemplate(template, options) {
        var _a;
        var that = this;
        var resultTemplate;
        var renderingTemplate = super._processTemplate(template);
        var firstDataColumnIndex = that._columnsController.getFirstDataColumnIndex();
        if (renderingTemplate && (null === (_a = options.column) || void 0 === _a ? void 0 : _a.index) === firstDataColumnIndex) {
            resultTemplate = {
                render(options) {
                    var $container = options.container;
                    if (that._renderCellCommandContent($container, options.model)) {
                        options.container = createCellContent($container)
                    }
                    renderingTemplate.render(options)
                }
            }
        } else {
            resultTemplate = renderingTemplate
        }
        return resultTemplate
    }
    _updateCell($cell, options) {
        $cell = $cell.hasClass(TREELIST_TEXT_CONTENT) ? $cell.parent() : $cell;
        super._updateCell($cell, options)
    }
    _rowClick(e) {
        var dataController = this._dataController;
        var $targetElement = $(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)
        }
        super._rowClick(e)
    }
    _createRow(row) {
        var node = row && row.node;
        var $rowElement = super._createRow.apply(this, arguments);
        if (node) {
            this.setAria("level", row.level + 1, $rowElement);
            if (node.hasChildren) {
                this.setAria("expanded", row.isExpanded, $rowElement)
            }
        }
        return $rowElement
    }
    _getGridRoleName() {
        return "treegrid"
    }
    isExpandIcon($targetElement) {
        return !!$targetElement.closest(".".concat(TREELIST_EXPANDED_CLASS, ", .").concat(TREELIST_COLLAPSED_CLASS)).length
    }
    setAriaExpandedAttribute($row, row) {
        var isRowExpanded = row.isExpanded;
        this.setAria("expanded", isDefined(isRowExpanded) && isRowExpanded.toString(), $row)
    }
}
treeListCore.registerModule("rows", {
    defaultOptions: rowsModule.defaultOptions,
    views: {
        rowsView: TreeListRowsView
    }
});
