package com.ss.android.ugc.aweme.simkit.impl.bitrateselector;

import X.AnonymousClass7dR;
import X.AnonymousClass8FJ;
import X.AnonymousClass8I8;
import X.AnonymousClass8J9;
import X.C03417cw;
import X.PJP;
import com.bytedance.covode.number.Covode;
import com.google.gson.b.a;
import com.ss.android.ugc.aweme.simkit.api.IDimensionBitrateFilterConfig;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public final class BitrateFilter {
    public final AnonymousClass8J9 LIZ = AnonymousClass8I8.LIZ().LIZIZ();

    static {
        Covode.recordClassIndex(114407);
    }

    public BitrateFilter() {
        C03417cw.LIZ().LJ().getDimensionPickConfig();
    }

    private Set<String> LIZ(List<Filter> list) {
        HashSet hashSet = new HashSet();
        if (list == null || list.size() == 0) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        for (Filter next : list) {
            if (!(next == null || next.getAllowGears() == null || next.getAllowGears().size() <= 0)) {
                arrayList.add(next);
            }
        }
        if (arrayList.size() == 0) {
            return null;
        }
        hashSet.addAll(((Filter) arrayList.get(0)).getAllowGears());
        for (int i = 1; i < arrayList.size(); i++) {
            Filter filter = (Filter) arrayList.get(i);
            if (!(filter == null || filter.getAllowGears() == null || filter.getAllowGears().size() <= 0)) {
                hashSet.retainAll(filter.getAllowGears());
            }
        }
        return hashSet;
    }

    private <T extends AnonymousClass8FJ> List<T> LIZ(List<T> list, Set<String> set) {
        ArrayList arrayList = new ArrayList(list);
        for (T t : list) {
            if (set.contains(String.valueOf(t.getQualityType()))) {
                arrayList.remove(t);
            }
        }
        return arrayList;
    }

    private <T extends AnonymousClass8FJ> List<T> LIZIZ(List<T> list, Set<String> set) {
        if (set == null) {
            return list;
        }
        ArrayList arrayList = new ArrayList();
        if (set.size() == 0) {
            return arrayList;
        }
        for (T t : list) {
            if (set.contains(String.valueOf(t.getQualityType()))) {
                arrayList.add(t);
            }
        }
        return arrayList;
    }

    public final <T extends AnonymousClass8FJ> List<T> LIZ(List<T> list, String str) {
        System.currentTimeMillis();
        if (list == null || list.size() == 0) {
            AnonymousClass7dR.LIZ.LIZ(str, list, list, (String) null);
        } else {
            IDimensionBitrateFilterConfig dimensionBitrateFilterConfig = C03417cw.LIZ().LJ().getDimensionBitrateFilterConfig();
            if (dimensionBitrateFilterConfig == null || !dimensionBitrateFilterConfig.LIZ()) {
                AnonymousClass7dR.LIZ.LIZ(str, list, list, (String) null);
            } else {
                PJP LIZIZ = dimensionBitrateFilterConfig.LIZIZ();
                LIZIZ.LJI = str;
                LIZIZ.LJIIIZ = AnonymousClass7dR.LIZ.LIZJ(str);
                List<Filter> LIZ2 = this.LIZ.LIZ(LIZIZ, new a<Filter>() {
                    static {
                        Covode.recordClassIndex(114408);
                    }
                }.type);
                HashSet hashSet = new HashSet();
                if (LIZ2 != null) {
                    for (Filter filter : LIZ2) {
                        if (!(filter == null || filter.getForbiddenGears() == null || filter.getForbiddenGears().size() <= 0)) {
                            hashSet.addAll(filter.getForbiddenGears());
                        }
                    }
                }
                List<T> LIZIZ2 = LIZIZ(LIZ(list, (Set<String>) hashSet), LIZ(LIZ2));
                AnonymousClass7dR.LIZ.LIZ(str, list, LIZIZ2, String.valueOf(LIZ2));
                if (LIZIZ2.size() != 0) {
                    list = LIZIZ2;
                }
            }
        }
        System.currentTimeMillis();
        return list;
    }
}
