import { getSingletonComponent, AppObjectEntity, } from "@vived/core";
export class UndoRedoEntity extends AppObjectEntity {
}
UndoRedoEntity.type = "UndoRedoEntity";
UndoRedoEntity.get = (appObjects) => getSingletonComponent(UndoRedoEntity.type, appObjects);
export function makeUndoRedoEntity(appObject) {
    return new UndoRedoEntityImp(appObject);
}
export function submitUndoRedoAction(action, appObjects) {
    const undoRedoEntity = UndoRedoEntity.get(appObjects);
    if (!undoRedoEntity) {
        appObjects.submitWarning("submitUndoRedoAction", "Unable to find UndoRedoEntity");
        return;
    }
    undoRedoEntity.submitAction(action);
}
class UndoRedoEntityImp extends UndoRedoEntity {
    get hasUndo() {
        return this.undoStack.length > 0;
    }
    get hasRedo() {
        return this.redoStack.length > 0;
    }
    constructor(appObject) {
        super(appObject, UndoRedoEntity.type);
        this.undoStack = [];
        this.redoStack = [];
        this.doUndo = () => {
            const action = this.undoStack.pop();
            if (!action) {
                this.warn("Nothing to undo");
                return;
            }
            action.undoAction();
            this.redoStack.push(action);
            this.notifyOnChange();
        };
        this.doRedo = () => {
            const action = this.redoStack.pop();
            if (!action) {
                this.warn("Nothing to redo");
                return;
            }
            action.redoAction();
            this.undoStack.push(action);
            this.notifyOnChange();
        };
        this.submitAction = (action) => {
            this.undoStack.push(action);
            this.redoStack = [];
            this.notifyOnChange();
        };
        this.clearStack = () => {
            this.redoStack = [];
            this.undoStack = [];
            this.notifyOnChange();
        };
    }
}
//# sourceMappingURL=UndoRedoEntity.js.map