namespace HistoryView.Universal;

public class FindVisualChildren
{
    public static List<T> Find<T>(DependencyObject depObj) where T : DependencyObject
    {
        List<T> list = [];
        if (depObj is null)
            return list;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child is null)
                continue;

            if (child is T t)
                list.Add(t);

            List<T> childItems = FindVisualChildren.Find<T>(child);

            if (childItems is null || childItems.Count == 0)
                continue;

            foreach (var item in childItems)
                list.Add(item);
        }

        return list;
    }
}
