using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models.Packages; namespace StabilityMatrix.Core.Helper.Factory; [Singleton(typeof(IPackageFactory))] public class PackageFactory : IPackageFactory { /// /// Mapping of package.Name to package /// private readonly Dictionary basePackages; public PackageFactory(IEnumerable basePackages) { this.basePackages = basePackages.ToDictionary(x => x.Name); } public IEnumerable GetAllAvailablePackages() { return basePackages.Values.OrderBy(p => p.InstallerSortOrder).ThenBy(p => p.DisplayName); } public BasePackage? FindPackageByName(string? packageName) { return packageName == null ? null : basePackages.GetValueOrDefault(packageName); } public BasePackage? this[string packageName] => basePackages[packageName]; /// public PackagePair? GetPackagePair(InstalledPackage? installedPackage) { if (installedPackage?.PackageName is not { } packageName) return null; return !basePackages.TryGetValue(packageName, out var basePackage) ? null : new PackagePair(installedPackage, basePackage); } }