"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "NodesPool", {
    enumerable: true,
    get: function() {
        return NodesPool;
    }
});
const _browser = require("../../../../helpers/browser");
/**
 * Multiplier used to pack (row, column) into a single numeric Map key.
 * 2^20 (1 048 576) supports up to ~1M columns while keeping the combined
 * value well within the 2^53 safe-integer range for any realistic row count.
 *
 * @type {number}
 */ const COLUMN_OFFSET = 1048576;
class NodesPool {
    /**
   * Set document owner for this instance.
   *
   * @param {Document} rootDocument The document window owner.
   */ setRootDocument(rootDocument) {
        this.rootDocument = rootDocument;
    }
    /**
   * Obtains an element.
   *
   * @param {number} rowIndex The row index.
   * @param {number} [columnIndex] The column index.
   * @returns {HTMLElement}
   */ obtain(rowIndex, columnIndex) {
        // Firefox requires creating new DOM elements instead of reusing pooled nodes to avoid
        // rendering issues. This bypasses the pooling mechanism and is consistent with the
        // Direct DOM renderer adapter used in OrderView.
        if ((0, _browser.isFirefox)()) {
            return this.rootDocument.createElement(this.nodeType);
        }
        const key = typeof columnIndex === 'number' ? rowIndex * COLUMN_OFFSET + columnIndex : rowIndex;
        let node = this.pool.get(key);
        if (node !== undefined) {
            return node;
        }
        node = this.rootDocument.createElement(this.nodeType);
        this.pool.set(key, node);
        return node;
    }
    constructor(nodeType){
        /**
   * The holder for all created DOM nodes (THs, TDs).
   *
   * @type {Map<number, HTMLElement>}
   */ this.pool = new Map();
        this.nodeType = nodeType.toUpperCase();
    }
}
