using System.Collections; namespace PkmnLib.Dynamic.ScriptHandling; public class ScriptIterator : IEnumerable { private readonly IReadOnlyList> _scripts; private int _index = -1; private int _setIndex = -1; public ScriptIterator(IReadOnlyList> scripts) { _scripts = scripts; } bool IncrementToNext() { if (_index != -1) { var current = _scripts[_index]; if (current is IScriptSet) { _setIndex += 1; if (_setIndex >= current.Count()) { _setIndex = -1; } else { return true; } } } _index += 1; for (; _index < _scripts.Count; _index++) { switch (_scripts[_index]) { case IScriptSet: _setIndex = 0; return true; case ScriptContainer { IsEmpty: false }: return true; } } return false; } /// public IEnumerator GetEnumerator() { while (IncrementToNext()) { var current = _scripts[_index]; yield return current switch { IScriptSet set => set.At(_setIndex), ScriptContainer container => container, _ => throw new InvalidOperationException("Invalid script type") }; } } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }