"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.undoableCommand = exports.UndoableCommand = exports.UndoableCommandController = void 0;
const __1 = require("..");
class UndoableCommandController {
    constructor(historySize = 10, onExecute) {
        this.historySize = historySize;
        this.onExecute = onExecute;
        this.history = [];
    }
    undoLast() {
        const result = this.history.pop();
        if (result) {
            result === null || result === void 0 ? void 0 : result.undo();
        }
        return result;
    }
    addEntry(result) {
        this.history.push(result);
        if (this.history.length >= this.historySize) {
            this.history.shift();
        }
    }
}
exports.UndoableCommandController = UndoableCommandController;
class UndoableCommand extends __1.Command {
}
exports.UndoableCommand = UndoableCommand;
function undoableCommand(controller = new UndoableCommandController()) {
    return ((data, handler, options, instance) => {
        const result = (controller === null || controller === void 0 ? void 0 : controller.onExecute)
            ? controller.onExecute(data, handler, options, instance)
            : handler(data, options);
        if (!(result === null || result === void 0 ? void 0 : result.undo)) {
            throw new Error('UndoableCommandHandler must return an undo function');
        }
        controller.addEntry({
            value: result.value,
            name: result.name,
            undo: result.undo,
        });
        return result;
    });
}
exports.undoableCommand = undoableCommand;
//# sourceMappingURL=undoableCommand.js.map