package org.ghidra.bridge.session;

import java.util.ArrayList;
import java.util.List;

import ghidra.program.model.listing.Program;

public final class TransactionHandle {
    private final Program program;
    private final int transactionId;
    private final List<RollbackEdit> rollbackEdits = new ArrayList<>();

    public TransactionHandle(Program program, int transactionId) {
        this.program = program;
        this.transactionId = transactionId;
    }

    public Program program() {
        return program;
    }

    public int transactionId() {
        return transactionId;
    }

    public void addRollbackEdit(RollbackEdit edit) {
        rollbackEdits.add(edit);
    }

    public void rollbackEdits() {
        for (int i = rollbackEdits.size() - 1; i >= 0; i--) {
            rollbackEdits.get(i).apply();
        }
    }

    public interface RollbackEdit {
        void apply();
    }
}
