import { BaseRenderer } from './_base.mjs';
import { warn } from '../../../../helpers/console.mjs';
import { toSingleLine } from '../../../../helpers/templateLiteralTag.mjs';
import { addClass } from '../../../../helpers/dom/element.mjs';
import { OrderView } from '../utils/orderView/index.mjs';
let performanceWarningAppeared = false;
/**
 * Colgroup renderer responsible for managing (inserting, tracking, rendering) COL elements.
 *
 *   <colgroup> (root node)
 *     ├ <col>   \
 *     ├ <col>    \
 *     ├ <col>     - ColGroupRenderer
 *     ├ <col>    /
 *     └ <col>   /.
 *
 * @class {ColGroupRenderer}
 */ export class ColGroupRenderer extends BaseRenderer {
    /**
   * Renders the col group elements.
   */ render() {
        const { columnsToRender, rowHeadersCount } = this.table;
        const allColumnsToRender = columnsToRender + rowHeadersCount;
        if (!performanceWarningAppeared && columnsToRender > 1000) {
            performanceWarningAppeared = true;
            warn(toSingleLine`Performance tip: Handsontable rendered more than 1000 visible columns.\x20
        Consider limiting the number of rendered columns by specifying the table width and/or\x20
        turning off the "renderAllColumns" option.`);
        }
        this.orderView.setSize(allColumnsToRender).setOffset(0).start();
        for(let visibleColumnIndex = 0; visibleColumnIndex < allColumnsToRender; visibleColumnIndex++){
            this.orderView.render();
            const COL = this.orderView.getCurrentNode();
            if (visibleColumnIndex < rowHeadersCount) {
                const sourceColumnIndex = this.table.renderedColumnToSource(visibleColumnIndex);
                COL.style.width = `${this.table.columnUtils.getHeaderWidth(sourceColumnIndex)}px`;
            } else {
                const sourceColumnIndex = this.table.renderedColumnToSource(visibleColumnIndex - rowHeadersCount);
                COL.style.width = `${this.table.columnUtils.getWidth(sourceColumnIndex)}px`;
            }
        }
        this.orderView.end();
        const firstChild = this.rootNode.firstChild;
        if (firstChild) {
            addClass(firstChild, 'rowHeader');
        }
    }
    constructor(rootNode){
        super('COL', rootNode);
        this.orderView = new OrderView(rootNode, (sourceColumnIndex)=>this.nodesPool.obtain(sourceColumnIndex), this.nodeType);
    }
}
