namespace BigRegister.Api.Data;

/// <summary>
/// In-memory idempotency-key ledger for the submit endpoints (Program.cs's `Submit`
/// helper): a replayed `Idempotency-Key` returns the exact result computed the first
/// time, instead of re-running the submission and minting a new reference.
/// ponytail: one global lock + no TTL/eviction — fine for a single-process demo;
/// swap for a TTL'd cache before this ever serves real traffic (an unbounded
/// dictionary keyed on client-supplied strings is a memory leak at scale).
/// </summary>
public static class IdempotencyStore
{
  private static readonly Dictionary<string, IResult> _results = new();
  private static readonly object _gate = new();

  public static bool TryGet(string key, out IResult? result)
  {
    lock (_gate) return _results.TryGetValue(key, out result);
  }

  public static void Set(string key, IResult result)
  {
    lock (_gate) _results[key] = result;
  }
}
