"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ColumnHeadersRenderer", {
    enumerable: true,
    get: function() {
        return ColumnHeadersRenderer;
    }
});
const _element = require("../../../../helpers/dom/element");
const _base = require("./_base");
const _orderView = require("../utils/orderView");
const _a11y = require("../../../../helpers/a11y");
class ColumnHeadersRenderer extends _base.BaseRenderer {
    /**
   * Obtains the instance of the OrderView class which is responsible for rendering the nodes to the root node.
   *
   * @param {HTMLTableRowElement} rootNode The TR element, which is root element for column headers (TH).
   * @returns {OrderView}
   */ obtainOrderView(rootNode) {
        let orderView;
        if (this.orderViews.has(rootNode)) {
            orderView = this.orderViews.get(rootNode);
        } else {
            orderView = new _orderView.OrderView(rootNode, (sourceColumnIndex)=>this.nodesPool.obtain(this.sourceRowIndex, sourceColumnIndex), this.nodeType);
            this.orderViews.set(rootNode, orderView);
        }
        return orderView;
    }
    /**
   * Renders the TH elements.
   */ render() {
        const { columnHeadersCount, columnHeaderFunctions, columnsToRender, rowHeadersCount, columnHeaderRows } = this.table;
        const allColumnsToRender = columnsToRender + rowHeadersCount;
        for(let visibleRowIndex = 0; visibleRowIndex < columnHeadersCount; visibleRowIndex++){
            const TR = columnHeaderRows.getRenderedNode(visibleRowIndex);
            this.sourceRowIndex = visibleRowIndex;
            const orderView = this.obtainOrderView(TR);
            orderView.setSize(allColumnsToRender).setOffset(0).start();
            for(let visibleColumnIndex = 0; visibleColumnIndex < allColumnsToRender; visibleColumnIndex++){
                orderView.render();
                const renderedColumnIndex = visibleColumnIndex - rowHeadersCount;
                const sourceColumnIndex = this.table.renderedColumnToSource(renderedColumnIndex);
                const TH = orderView.getCurrentNode();
                TH.className = '';
                TH.removeAttribute('style');
                // Remove all accessibility-related attributes for the header to start fresh.
                (0, _element.removeAttribute)(TH, [
                    new RegExp('aria-(.*)'),
                    new RegExp('role')
                ]);
                if (this.table.isAriaEnabled()) {
                    (0, _element.setAttribute)(TH, [
                        (0, _a11y.A11Y_COLINDEX)(visibleColumnIndex + 1),
                        (0, _a11y.A11Y_TABINDEX)(-1),
                        (0, _a11y.A11Y_COLUMNHEADER)(),
                        ...renderedColumnIndex >= 0 ? [
                            (0, _a11y.A11Y_SCOPE_COL)()
                        ] : [
                            // Adding `role=row` to the corner headers to prevent
                            // https://github.com/handsontable/dev-handsontable/issues/1574
                            (0, _a11y.A11Y_GRIDCELL_BUTTON)(),
                            (0, _a11y.A11Y_LABEL)('Select whole grid')
                        ]
                    ]);
                }
                columnHeaderFunctions[visibleRowIndex](sourceColumnIndex, TH, visibleRowIndex);
            }
            orderView.end();
        }
    }
    constructor(){
        super('TH'), /**
   * Cache for OrderView classes connected to specified node.
   *
   * @type {WeakMap}
   */ this.orderViews = new WeakMap(), /**
   * Row index which specifies the row position of the processed column header.
   *
   * @type {number}
   */ this.sourceRowIndex = 0;
    }
}
