using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.Purchasing
{
///
/// Provides helper methods to retrieve products by
/// store independent/store specific id.
///
public class ProductCollection
{
private Dictionary m_IdToProduct;
private Dictionary m_StoreSpecificIdToProduct;
internal ProductCollection(Product[] products)
{
AddProducts(products);
}
internal void AddProducts(IEnumerable products)
{
set.UnionWith(products);
all = set.ToArray();
m_IdToProduct = all.ToDictionary(x => x.definition.id);
m_StoreSpecificIdToProduct = all.ToDictionary(x => x.definition.storeSpecificId);
}
///
/// The hash set of all products
///
public HashSet set { get; } = new HashSet();
///
/// The array of all products
///
public Product[] all { get; private set; }
///
/// Gets a product matching an id
///
/// The id of the desired product
/// The product matching the id, or null if not found
public Product WithID(string id)
{
m_IdToProduct.TryGetValue(id, out var result);
return result;
}
///
/// Gets a product matching a store-specific id
///
/// The store-specific id of the desired product
/// The product matching the id, or null if not found
public Product WithStoreSpecificID(string id)
{
Product result = null;
if (id != null)
{
m_StoreSpecificIdToProduct.TryGetValue(id, out result);
}
return result;
}
}
}