"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "DynamicCellMetaMod", {
    enumerable: true,
    get: function() {
        return DynamicCellMetaMod;
    }
});
const _hooks = require("../../../core/hooks");
const _object = require("../../../helpers/object");
const _function = require("../../../helpers/function");
class DynamicCellMetaMod {
    /**
   * Extends the cell meta object by user-specific properties.
   *
   * The cell meta object can be extended dynamically,
   * either by Handsontable's hooks (`beforeGetCellMeta` and`afterGetCellMeta`),
   * or by Handsontable's `cells` option.
   *
   * To boost performance, the extending process is triggered only once per one slow Handsontable render cycle.
   *
   * @param {object} cellMeta The cell meta object.
   */ extendCellMeta(cellMeta) {
        const { row: physicalRow, col: physicalColumn } = cellMeta;
        if (this.metaSyncMemo.get(physicalRow)?.has(physicalColumn)) {
            return;
        }
        const { visualRow, visualCol } = cellMeta;
        const hot = this.metaManager.hot;
        const prop = hot.colToProp(visualCol);
        cellMeta.prop = prop;
        hot.runHooks('beforeGetCellMeta', visualRow, visualCol, cellMeta);
        // extend a `type` value, added or changed in the `beforeGetCellMeta` hook
        const cellType = (0, _object.hasOwnProperty)(cellMeta, 'type') ? cellMeta.type : null;
        let cellSettings = (0, _function.isFunction)(cellMeta.cells) ? cellMeta.cells(physicalRow, physicalColumn, prop) : null;
        if (cellType) {
            if (cellSettings) {
                cellSettings.type = cellSettings.type ?? cellType;
            } else {
                cellSettings = {
                    type: cellType
                };
            }
        }
        if (cellSettings) {
            this.metaManager.updateCellMeta(physicalRow, physicalColumn, cellSettings);
        }
        hot.runHooks('afterGetCellMeta', visualRow, visualCol, cellMeta);
        if (!this.metaSyncMemo.has(physicalRow)) {
            this.metaSyncMemo.set(physicalRow, new Set());
        }
        this.metaSyncMemo.get(physicalRow).add(physicalColumn);
    }
    constructor(metaManager){
        /**
   * @type {Map}
   */ this.metaSyncMemo = new Map();
        this.metaManager = metaManager;
        metaManager.addLocalHook('afterGetCellMeta', (...args)=>this.extendCellMeta(...args));
        _hooks.Hooks.getSingleton().add('beforeRender', (forceFullRender)=>{
            if (forceFullRender) {
                this.metaSyncMemo.clear();
            }
        }, this.metaManager.hot);
    }
}
