using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json.Linq; namespace RudderSdk.Core; /// A scenario store-offer node. Resolve it with or . public sealed class StoreOfferEffect { private readonly EffectHandle _handle; internal StoreOfferEffect(EffectHandle handle) => _handle = handle; /// Run id. public string RunId => _handle.RunId; /// Scenario id. public string ScenarioSlug => _handle.ScenarioSlug; /// Node id. public string NodeId => _handle.NodeId; /// Store slug from the node data, if present. public string StoreSlug => _handle.Get("storeSlug", string.Empty); /// Optional message from the node data. public string? Message { get { var value = _handle.Get("message", null); return string.IsNullOrEmpty(value) ? null : value; } } /// Node data payload. public JObject Data => _handle.Data; /// True after the offer was resolved once. public bool IsResolved { get; private set; } /// Reads a typed value from the node data. public T Get(string key, T defaultValue = default!) => _handle.Get(key, defaultValue); /// Posts the onPurchase callback. public Task PurchaseAsync(CancellationToken cancellationToken = default) => ResolveAsync("onPurchase", cancellationToken); /// Posts the onPurchase callback (fire-and-forget). public void Purchase() => Resolve("onPurchase"); /// Posts the onDecline callback. public Task DeclineAsync(CancellationToken cancellationToken = default) => ResolveAsync("onDecline", cancellationToken); /// Posts the onDecline callback (fire-and-forget). public void Decline() => Resolve("onDecline"); private async Task ResolveAsync(string handle, CancellationToken cancellationToken) { if (IsResolved) return; IsResolved = true; await _handle.CompleteAsync(handle, cancellationToken).ConfigureAwait(false); } private void Resolve(string handle) { if (IsResolved) return; IsResolved = true; _handle.Complete(handle); } }