using aeqw89.DataKeys; using Beam; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using Beam.Models; namespace Beam.Fluent { /// /// Type‑safe, staged builder that prevents callers from forgetting the mandatory steps /// (source → link selection → transformer) and surfaces operational knobs as first‑class /// methods instead of magic parameters. /// public static partial class DownloadBuilder { /* ──────────────────────────── Entry points ─────────────────────────── */ public static ILinkStage FromResource(DataKey dict, string kind, BeamDataContext beamDataDictionary) => Create(dict, beamDataDictionary, kind); public static IAlternativeLinkStage FromScratch() => new LinkStage(null!, null!, null!, new()); private static ILinkStage Create(DataKey resourceDict, BeamDataContext data, string kind) { var (source, initial) = Resolve(resourceDict, kind, data); var ctxBuilder = new DownloadContextBuilder().WithLinks(Array.Empty()); // placeholder, filled later. return new LinkStage(source, initial, data, ctxBuilder); } private static (WebResource Source, State Initial) Resolve(DataKey resourceDict, string kind, BeamDataContext data) { if (!data.ResourceDictionaries.TryGetValue(resourceDict, out var dict)) throw new KeyNotFoundException($"Novel '{resourceDict}' not found in BeamDataDictionary."); if (!dict.Resources.TryGetValue(kind, out var sourceKey)) throw new KeyNotFoundException($"Novel kind '{kind}' not found in '{resourceDict}'"); if (!data.Resources.TryGetValue(sourceKey, out var source)) throw new KeyNotFoundException($"Novel source '{sourceKey}' was not found"); if (!data.InitialStates.TryGetValue(sourceKey.To(), out var istate)) throw new KeyNotFoundException($"Immutable state for kind '{kind}' not found"); return (source, istate); } } }