// Copyright (c) Arlo Godfrey. All Rights Reserved.
// Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms.
// See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Diagnostics;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.CoreModels;
namespace StrixMusic.Sdk.AdapterModels
{
///
/// Aggregates multiple instances into one.
///
public sealed class MergedSearchQuery : ISearchQuery, IMergedMutable
{
private readonly List _sources;
///
/// Creates a new instance of .
///
/// The initial source items merged to form this instance.
public MergedSearchQuery(IReadOnlyList sources)
{
_sources = sources.ToList();
// Make sure all items in the given collection match.
for (var i = 0; i < sources.Count; i++)
{
var item = sources[i];
var nextItem = sources.ElementAtOrDefault(i + 1);
if (nextItem == null)
continue;
if (item.Query != nextItem.Query || item.CreatedAt != nextItem.CreatedAt)
throw new ArgumentException($"Search queries passed to {nameof(MergedSearchQuery)} do not match");
}
// If they all match, we can assign any of them as the value.
Query = sources[0].Query;
CreatedAt = sources[0].CreatedAt;
}
///
public string Query { get; }
///
public DateTime CreatedAt { get; }
///
public bool Equals(ICoreSearchQuery other) => other.Query == Query && other.CreatedAt == CreatedAt;
///
public IReadOnlyList Sources => _sources;
///
public event EventHandler? SourcesChanged;
///
public void AddSource(ICoreSearchQuery itemToMerge)
{
Guard.IsNotNull(itemToMerge, nameof(itemToMerge));
_sources.Add(itemToMerge);
SourcesChanged?.Invoke(this, EventArgs.Empty);
}
///
public void RemoveSource(ICoreSearchQuery itemToRemove)
{
Guard.IsNotNull(itemToRemove, nameof(itemToRemove));
_sources.Remove(itemToRemove);
SourcesChanged?.Invoke(this, EventArgs.Empty);
}
}
}