"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CellAlignmentAction", {
    enumerable: true,
    get: function() {
        return CellAlignmentAction;
    }
});
const _base = require("./_base");
const _utils = require("../../contextMenu/utils");
const _array = require("../../../helpers/array");
class CellAlignmentAction extends _base.BaseAction {
    static startRegisteringEvents(hot, undoRedoPlugin) {
        hot.addHook('beforeCellAlignment', (stateBefore, range, type, alignment)=>{
            undoRedoPlugin.done(()=>new CellAlignmentAction({
                    stateBefore,
                    range,
                    type,
                    alignment
                }));
        });
    }
    /**
   * @param {Core} hot The Handsontable instance.
   * @param {function(): void} undoneCallback The callback to be called after the action is undone.
   */ undo(hot, undoneCallback) {
        (0, _array.arrayEach)(this.range, (range)=>{
            range.forAll((row, col)=>{
                // Alignment classes should only collected within cell ranges. We skip header coordinates.
                if (row >= 0 && col >= 0) {
                    hot.setCellMeta(row, col, 'className', this.stateBefore[row][col] || ' htLeft');
                }
            });
        });
        hot.addHookOnce('afterViewRender', undoneCallback);
        hot.render();
    }
    /**
   * @param {Core} hot The Handsontable instance.
   * @param {function(): void} redoneCallback The callback to be called after the action is redone.
   */ redo(hot, redoneCallback) {
        (0, _utils.align)(this.range, this.type, this.alignment, (row, col)=>hot.getCellMeta(row, col), (row, col, key, value)=>hot.setCellMeta(row, col, key, value));
        hot.addHookOnce('afterViewRender', redoneCallback);
        hot.render();
    }
    constructor({ stateBefore, range, type, alignment }){
        super('cell_alignment');
        this.stateBefore = stateBefore;
        this.range = range;
        this.type = type;
        this.alignment = alignment;
    }
}
