using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace RudderSdk.Core;
///
/// A scenario quest node. Report objective progress; the server auto-completes
/// the node once every objective is satisfied.
///
public sealed class QuestEffect
{
private readonly EffectHandle _handle;
internal QuestEffect(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;
/// Quest name from the node data.
public string Name => _handle.Get("name", string.Empty);
/// Objective definitions from the node data.
public IReadOnlyList Objectives
{
get
{
if (_handle.Data["objectives"] is not JArray array)
return Array.Empty();
var list = new List(array.Count);
foreach (var item in array)
{
if (item is JObject obj)
list.Add(obj);
}
return list;
}
}
/// Node data payload.
public JObject Data => _handle.Data;
/// Reads a typed value from the node data.
public T Get(string key, T defaultValue = default!) => _handle.Get(key, defaultValue);
/// Reports progress toward an objective via POST /sdk/v1/scenarios/counter.
public Task ReportProgressAsync(string objectiveId, long amount = 1, CancellationToken cancellationToken = default)
=> _handle.ReportProgressAsync(objectiveId, amount, cancellationToken);
/// Reports progress toward an objective (fire-and-forget).
public void ReportProgress(string objectiveId, long amount = 1) => _handle.ReportProgress(objectiveId, amount);
}