"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CellsRenderer", {
    enumerable: true,
    get: function() {
        return CellsRenderer;
    }
});
const _element = require("../../../../helpers/dom/element");
const _orderView = require("../utils/orderView");
const _base = require("./_base");
const _a11y = require("../../../../helpers/a11y");
class CellsRenderer extends _base.BaseRenderer {
    /**
   * Obtains the instance of the SharedOrderView class which is responsible for rendering the nodes to the root node.
   *
   * @param {HTMLTableRowElement} rootNode The TR element, which is root element for cells (TD).
   * @returns {SharedOrderView}
   */ obtainOrderView(rootNode) {
        let orderView;
        if (this.orderViews.has(rootNode)) {
            orderView = this.orderViews.get(rootNode);
        } else {
            orderView = new _orderView.SharedOrderView(rootNode, (sourceColumnIndex)=>this.nodesPool.obtain(this.sourceRowIndex, sourceColumnIndex), this.nodeType);
            this.orderViews.set(rootNode, orderView);
        }
        return orderView;
    }
    /**
   * Renders the cells.
   */ render() {
        const { rowsToRender, columnsToRender, rows, rowHeaders } = this.table;
        for(let visibleRowIndex = 0; visibleRowIndex < rowsToRender; visibleRowIndex++){
            const sourceRowIndex = this.table.renderedRowToSource(visibleRowIndex);
            const TR = rows.getRenderedNode(visibleRowIndex);
            this.sourceRowIndex = sourceRowIndex;
            const orderView = this.obtainOrderView(TR);
            const rowHeadersView = rowHeaders.obtainOrderView(TR);
            orderView.prependView(rowHeadersView).setSize(columnsToRender).setOffset(0).start();
            for(let visibleColumnIndex = 0; visibleColumnIndex < columnsToRender; visibleColumnIndex++){
                orderView.render();
                const sourceColumnIndex = this.table.renderedColumnToSource(visibleColumnIndex);
                const TD = orderView.getCurrentNode();
                if (!(0, _element.hasClass)(TD, 'hide')) {
                    TD.className = '';
                }
                TD.removeAttribute('style');
                TD.removeAttribute('dir');
                // Remove all accessibility-related attributes for the cell to start fresh.
                (0, _element.removeAttribute)(TD, [
                    new RegExp('aria-(.*)'),
                    new RegExp('role')
                ]);
                this.table.cellRenderer(sourceRowIndex, sourceColumnIndex, TD);
                if (this.table.isAriaEnabled()) {
                    (0, _element.setAttribute)(TD, [
                        ...TD.hasAttribute('role') ? [] : [
                            (0, _a11y.A11Y_GRIDCELL)()
                        ],
                        (0, _a11y.A11Y_TABINDEX)(-1),
                        // `aria-colindex` is incremented by both tbody and thead rows.
                        (0, _a11y.A11Y_COLINDEX)(sourceColumnIndex + (this.table.rowUtils?.dataAccessObject?.rowHeaders.length ?? 0) + 1)
                    ]);
                }
            }
            orderView.end();
        }
    }
    constructor(){
        super('TD'), /**
   * Cache for OrderView classes connected to specified node.
   *
   * @type {WeakMap}
   */ this.orderViews = new WeakMap(), /**
   * Row index which specifies the row position of the processed cell.
   *
   * @type {number}
   */ this.sourceRowIndex = 0;
    }
}
