package androidx.media3.exoplayer.text;

import androidx.media3.common.text.Cue;
import androidx.media3.common.util.Assertions;
import com.google.common.base.e;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.j1;
import com.google.common.collect.m0;
import java.util.ArrayList;
import java.util.List;
import n2.b;

/* JADX INFO: loaded from: classes.dex */
final class MergingCuesResolver implements CuesResolver {
    private static final j1 CUES_DISPLAY_PRIORITY_COMPARATOR;
    private final List<b> cuesWithTimingList = new ArrayList();

    static {
        final int i10 = 0;
        final int i11 = 1;
        CUES_DISPLAY_PRIORITY_COMPARATOR = j1.c().d(new e() { // from class: androidx.media3.exoplayer.text.a
            @Override // com.google.common.base.e
            public final Object apply(Object obj) {
                b bVar = (b) obj;
                switch (i10) {
                    case 0:
                        return MergingCuesResolver.lambda$static$0(bVar);
                    default:
                        return MergingCuesResolver.lambda$static$1(bVar);
                }
            }
        }).a(j1.c().e().d(new e() { // from class: androidx.media3.exoplayer.text.a
            @Override // com.google.common.base.e
            public final Object apply(Object obj) {
                b bVar = (b) obj;
                switch (i11) {
                    case 0:
                        return MergingCuesResolver.lambda$static$0(bVar);
                    default:
                        return MergingCuesResolver.lambda$static$1(bVar);
                }
            }
        }));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ Long lambda$static$0(b bVar) {
        return Long.valueOf(bVar.f30760b);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ Long lambda$static$1(b bVar) {
        return Long.valueOf(bVar.f30761c);
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public boolean addCues(b bVar, long j10) {
        Assertions.checkArgument(bVar.f30760b != -9223372036854775807L);
        Assertions.checkArgument(bVar.f30761c != -9223372036854775807L);
        long j11 = bVar.f30760b;
        boolean z10 = j11 <= j10 && j10 < bVar.f30762d;
        for (int size = this.cuesWithTimingList.size() - 1; size >= 0; size--) {
            if (j11 >= this.cuesWithTimingList.get(size).f30760b) {
                this.cuesWithTimingList.add(size + 1, bVar);
                return z10;
            }
        }
        this.cuesWithTimingList.add(0, bVar);
        return z10;
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public void clear() {
        this.cuesWithTimingList.clear();
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public void discardCuesBeforeTimeUs(long j10) {
        int i10 = 0;
        while (i10 < this.cuesWithTimingList.size()) {
            long j11 = this.cuesWithTimingList.get(i10).f30760b;
            if (j10 > j11 && j10 > this.cuesWithTimingList.get(i10).f30762d) {
                this.cuesWithTimingList.remove(i10);
                i10--;
            } else if (j10 < j11) {
                return;
            }
            i10++;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // androidx.media3.exoplayer.text.CuesResolver
    public ImmutableList<Cue> getCuesAtTimeUs(long j10) {
        if (!this.cuesWithTimingList.isEmpty()) {
            if (j10 >= this.cuesWithTimingList.get(0).f30760b) {
                ArrayList arrayList = new ArrayList();
                for (int i10 = 0; i10 < this.cuesWithTimingList.size(); i10++) {
                    b bVar = this.cuesWithTimingList.get(i10);
                    if (j10 >= bVar.f30760b && j10 < bVar.f30762d) {
                        arrayList.add(bVar);
                    }
                    if (j10 < bVar.f30760b) {
                        break;
                    }
                }
                ImmutableList immutableListY = ImmutableList.y(CUES_DISPLAY_PRIORITY_COMPARATOR, arrayList);
                m0 m0VarM = ImmutableList.m();
                for (int i11 = 0; i11 < immutableListY.size(); i11++) {
                    m0VarM.g(((b) immutableListY.get(i11)).f30759a);
                }
                return m0VarM.i();
            }
        }
        return ImmutableList.t();
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public long getNextCueChangeTimeUs(long j10) {
        int i10 = 0;
        long jMin = -9223372036854775807L;
        while (true) {
            if (i10 >= this.cuesWithTimingList.size()) {
                break;
            }
            long j11 = this.cuesWithTimingList.get(i10).f30760b;
            long j12 = this.cuesWithTimingList.get(i10).f30762d;
            if (j10 < j11) {
                jMin = jMin == -9223372036854775807L ? j11 : Math.min(jMin, j11);
            } else {
                if (j10 < j12) {
                    jMin = jMin == -9223372036854775807L ? j12 : Math.min(jMin, j12);
                }
                i10++;
            }
        }
        if (jMin != -9223372036854775807L) {
            return jMin;
        }
        return Long.MIN_VALUE;
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public long getPreviousCueChangeTimeUs(long j10) {
        if (this.cuesWithTimingList.isEmpty()) {
            return -9223372036854775807L;
        }
        if (j10 < this.cuesWithTimingList.get(0).f30760b) {
            return -9223372036854775807L;
        }
        long jMax = this.cuesWithTimingList.get(0).f30760b;
        for (int i10 = 0; i10 < this.cuesWithTimingList.size(); i10++) {
            long j11 = this.cuesWithTimingList.get(i10).f30760b;
            long j12 = this.cuesWithTimingList.get(i10).f30762d;
            if (j12 > j10) {
                if (j11 > j10) {
                    break;
                }
                jMax = Math.max(jMax, j11);
            } else {
                jMax = Math.max(jMax, j12);
            }
        }
        return jMax;
    }
}
