package androidx.media3.exoplayer.text;

import androidx.media3.common.text.Cue;
import androidx.media3.common.util.Assertions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.z0;
import java.util.ArrayList;
import n2.b;

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

    private int getIndexOfCuesStartingAfter(long j10) {
        for (int i10 = 0; i10 < this.cuesWithTimingList.size(); i10++) {
            if (j10 < this.cuesWithTimingList.get(i10).f30760b) {
                return i10;
            }
        }
        return this.cuesWithTimingList.size();
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0025  */
    @Override // androidx.media3.exoplayer.text.CuesResolver
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public boolean addCues(b bVar, long j10) {
        boolean z10;
        Assertions.checkArgument(bVar.f30760b != -9223372036854775807L);
        long j11 = bVar.f30760b;
        if (j11 <= j10) {
            long j12 = bVar.f30762d;
            z10 = j12 == -9223372036854775807L || j10 < j12;
        }
        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;
            }
            if (this.cuesWithTimingList.get(size).f30760b <= j10) {
                z10 = false;
            }
        }
        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 indexOfCuesStartingAfter = getIndexOfCuesStartingAfter(j10);
        if (indexOfCuesStartingAfter == 0) {
            return;
        }
        long j11 = this.cuesWithTimingList.get(indexOfCuesStartingAfter - 1).f30762d;
        if (j11 == -9223372036854775807L || j11 >= j10) {
            indexOfCuesStartingAfter--;
        }
        this.cuesWithTimingList.subList(0, indexOfCuesStartingAfter).clear();
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public ImmutableList<Cue> getCuesAtTimeUs(long j10) {
        int indexOfCuesStartingAfter = getIndexOfCuesStartingAfter(j10);
        if (indexOfCuesStartingAfter == 0) {
            return ImmutableList.t();
        }
        b bVar = this.cuesWithTimingList.get(indexOfCuesStartingAfter - 1);
        long j11 = bVar.f30762d;
        return (j11 == -9223372036854775807L || j10 < j11) ? bVar.f30759a : ImmutableList.t();
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public long getNextCueChangeTimeUs(long j10) {
        if (this.cuesWithTimingList.isEmpty()) {
            return Long.MIN_VALUE;
        }
        if (j10 < this.cuesWithTimingList.get(0).f30760b) {
            return this.cuesWithTimingList.get(0).f30760b;
        }
        for (int i10 = 1; i10 < this.cuesWithTimingList.size(); i10++) {
            b bVar = this.cuesWithTimingList.get(i10);
            if (j10 < bVar.f30760b) {
                long j11 = this.cuesWithTimingList.get(i10 - 1).f30762d;
                long j12 = bVar.f30760b;
                return (j11 == -9223372036854775807L || j11 <= j10 || j11 >= j12) ? j12 : j11;
            }
        }
        long j13 = ((b) z0.n(this.cuesWithTimingList)).f30762d;
        if (j13 == -9223372036854775807L || j10 >= j13) {
            return Long.MIN_VALUE;
        }
        return j13;
    }

    @Override // androidx.media3.exoplayer.text.CuesResolver
    public long getPreviousCueChangeTimeUs(long j10) {
        if (this.cuesWithTimingList.isEmpty() || j10 < this.cuesWithTimingList.get(0).f30760b) {
            return -9223372036854775807L;
        }
        for (int i10 = 1; i10 < this.cuesWithTimingList.size(); i10++) {
            long j11 = this.cuesWithTimingList.get(i10).f30760b;
            if (j10 == j11) {
                return j11;
            }
            if (j10 < j11) {
                b bVar = this.cuesWithTimingList.get(i10 - 1);
                long j12 = bVar.f30762d;
                return (j12 == -9223372036854775807L || j12 > j10) ? bVar.f30760b : j12;
            }
        }
        b bVar2 = (b) z0.n(this.cuesWithTimingList);
        long j13 = bVar2.f30762d;
        return (j13 == -9223372036854775807L || j10 < j13) ? bVar2.f30760b : j13;
    }
}
