using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace AxCopilot.Core;

public sealed class BulkObservableCollection<T> : ObservableCollection<T>
{
	private bool _suppressNotification;

	protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
	{
		if (!_suppressNotification)
		{
			base.OnCollectionChanged(e);
		}
	}

	protected override void OnPropertyChanged(PropertyChangedEventArgs e)
	{
		if (!_suppressNotification)
		{
			base.OnPropertyChanged(e);
		}
	}

	public void ReplaceAll(IEnumerable<T> items)
	{
		_suppressNotification = true;
		try
		{
			base.Items.Clear();
			foreach (T item in items)
			{
				base.Items.Add(item);
			}
		}
		finally
		{
			_suppressNotification = false;
		}
		OnPropertyChanged(new PropertyChangedEventArgs("Count"));
		OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
		OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
	}
}
