using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FuzzySharp; using FuzzySharp.Extractor; using Props.Shop.Framework; namespace Props.Shop.Adafruit.Api { public class SearchManager : IDisposable { public float Similarity { get; set; } public IProductListingManager ProductListingManager { get; private set; } private bool disposedValue; public SearchManager(IProductListingManager productListingManager, float similarity = 0.8f) { this.ProductListingManager = productListingManager ?? throw new ArgumentNullException("productListingManager"); this.Similarity = similarity; } public async IAsyncEnumerable Search(string query) { if (ProductListingManager.ProductListings == null) { ProductListingManager.RefreshProductListings(); } IDictionary> productListings = await ProductListingManager.ProductListings; foreach (ExtractedResult listingNames in Process.ExtractAll(query, productListings.Keys, cutoff: (int)(Similarity * 100))) { foreach (ProductListing same in productListings[listingNames.Value]) { yield return same; } } } protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { ProductListingManager.Dispose(); } disposedValue = true; } } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } } }