"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RowMoveAction", {
    enumerable: true,
    get: function() {
        return RowMoveAction;
    }
});
const _base = require("./_base");
const _moves = require("../../../helpers/moves");
class RowMoveAction extends _base.BaseAction {
    static startRegisteringEvents(hot, undoRedoPlugin) {
        hot.addHook('beforeRowMove', (rows, finalIndex)=>{
            if (rows === false) {
                return;
            }
            undoRedoPlugin.done(()=>new RowMoveAction({
                    rows,
                    finalIndex
                }));
        });
    }
    /**
   * @param {Core} hot The Handsontable instance.
   * @param {function(): void} undoneCallback The callback to be called after the action is undone.
   */ undo(hot, undoneCallback) {
        const manualRowMove = hot.getPlugin('manualRowMove');
        hot.addHookOnce('afterViewRender', undoneCallback);
        const rowMoves = (0, _moves.getMoves)(this.rows, this.finalRowIndex, hot.rowIndexMapper.getNumberOfIndexes());
        rowMoves.reverse().forEach(({ from, to })=>{
            if (from < to) {
                to -= 1;
            }
            manualRowMove.moveRow(to, from);
        });
        hot.render();
        hot.deselectCell();
        hot.selectRows(this.rows[0], this.rows[0] + this.rows.length - 1);
    }
    /**
   * @param {Core} hot The Handsontable instance.
   * @param {function(): void} redoneCallback The callback to be called after the action is redone.
   */ redo(hot, redoneCallback) {
        const manualRowMove = hot.getPlugin('manualRowMove');
        hot.addHookOnce('afterViewRender', redoneCallback);
        manualRowMove.moveRows(this.rows.slice(), this.finalRowIndex);
        hot.render();
        hot.deselectCell();
        hot.selectRows(this.finalRowIndex, this.finalRowIndex + this.rows.length - 1);
    }
    constructor({ rows, finalIndex }){
        super('row_move');
        this.rows = rows.slice();
        this.finalRowIndex = finalIndex;
    }
}
