import { Effect } from 'effect';
import { EngineHistoryError } from '../errors.js';
export function createEngineHistoryService(args) {
    return {
        undo() {
            return Effect.try({
                try: () => {
                    const entry = args.state.undoStack.pop();
                    if (!entry) {
                        return false;
                    }
                    args.state.setTransactionReplayDepth(args.state.getTransactionReplayDepth() + 1);
                    try {
                        args.executeTransaction(entry.inverse, 'undo');
                    }
                    finally {
                        args.state.setTransactionReplayDepth(args.state.getTransactionReplayDepth() - 1);
                    }
                    args.state.redoStack.push(entry);
                    return true;
                },
                catch: (cause) => new EngineHistoryError({
                    message: 'Failed to undo transaction',
                    cause,
                }),
            });
        },
        redo() {
            return Effect.try({
                try: () => {
                    const entry = args.state.redoStack.pop();
                    if (!entry) {
                        return false;
                    }
                    args.state.setTransactionReplayDepth(args.state.getTransactionReplayDepth() + 1);
                    try {
                        args.executeTransaction(entry.forward, 'redo');
                    }
                    finally {
                        args.state.setTransactionReplayDepth(args.state.getTransactionReplayDepth() - 1);
                    }
                    args.state.undoStack.push(entry);
                    return true;
                },
                catch: (cause) => new EngineHistoryError({
                    message: 'Failed to redo transaction',
                    cause,
                }),
            });
        },
    };
}
//# sourceMappingURL=history-service.js.map