namespace UI_WinForms;

public partial class Main : Form
{
    private readonly Dictionary<int, Action> deferredSaveOperations = new();

    public Main()
    {
        this.InitializeComponent();
    }

    private void Main_FormClosing(object sender, FormClosingEventArgs e)
    {
        foreach (var (_, action) in this.deferredSaveOperations)
            action();
        
        this.deferredSaveOperations.Clear();
    }
    
    internal void AddDeferredSaveOperation(int id, Action action)
    {
        if (this.deferredSaveOperations.ContainsKey(id))
            this.deferredSaveOperations.Remove(id);
        
        this.deferredSaveOperations.Add(id, action);
    }
    
    internal void RemoveDeferredSaveOperation(int id)
    {
        if (this.deferredSaveOperations.ContainsKey(id))
            this.deferredSaveOperations.Remove(id);
    }
}