package r2;

import android.graphics.RectF;
import android.text.Layout;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import r2.y;

/* loaded from: classes.dex */
public abstract class v0 {
    private static final float a(int i10, int i11, float[] fArr) {
        return fArr[(i10 - i11) * 2];
    }

    private static final float b(int i10, int i11, float[] fArr) {
        return fArr[((i10 - i11) * 2) + 1];
    }

    /* JADX WARN: Code restructure failed: missing block: B:31:0x00d6, code lost:
    
        return -1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static final int c(y.a aVar, RectF rectF, int i10, int i11, int i12, float f10, float f11, float[] fArr, s2.d dVar, dn.p pVar) {
        int b10;
        int d10;
        int d11;
        int g10;
        if (!g(rectF, f10, f11)) {
            return -1;
        }
        if ((aVar.c() || rectF.right < f11) && (!aVar.c() || rectF.left > f10)) {
            b10 = aVar.b();
            int a10 = aVar.a();
            while (a10 - b10 > 1) {
                int i13 = (a10 + b10) / 2;
                float a11 = a(i13, i10, fArr);
                if ((aVar.c() || a11 <= rectF.right) && (!aVar.c() || a11 >= rectF.left)) {
                    b10 = i13;
                } else {
                    a10 = i13;
                }
            }
            if (aVar.c()) {
                b10 = a10;
            }
        } else {
            b10 = aVar.a() - 1;
        }
        int c10 = dVar.c(b10 + 1);
        if (c10 == -1 || (d10 = dVar.d(c10)) <= aVar.b()) {
            return -1;
        }
        d11 = jn.o.d(c10, aVar.b());
        g10 = jn.o.g(d10, aVar.a());
        RectF rectF2 = new RectF(BitmapDescriptorFactory.HUE_RED, i11, BitmapDescriptorFactory.HUE_RED, i12);
        while (true) {
            rectF2.left = aVar.c() ? a(g10 - 1, i10, fArr) : a(d11, i10, fArr);
            rectF2.right = aVar.c() ? b(d11, i10, fArr) : b(g10 - 1, i10, fArr);
            if (((Boolean) pVar.invoke(rectF2, rectF)).booleanValue()) {
                return g10;
            }
            g10 = dVar.b(g10);
            if (g10 == -1 || g10 <= aVar.b()) {
                break;
            }
            d11 = jn.o.d(dVar.c(g10), aVar.b());
        }
    }

    public static final int[] d(u0 u0Var, Layout layout, y yVar, RectF rectF, int i10, dn.p pVar) {
        int i11;
        int i12;
        s2.d hVar = i10 == 1 ? new s2.h(u0Var.G(), u0Var.I()) : s2.e.a(u0Var.G(), u0Var.H());
        int lineForVertical = layout.getLineForVertical((int) rectF.top);
        if (rectF.top > u0Var.l(lineForVertical) && (lineForVertical = lineForVertical + 1) >= u0Var.m()) {
            return null;
        }
        int i13 = lineForVertical;
        int lineForVertical2 = layout.getLineForVertical((int) rectF.bottom);
        if (lineForVertical2 == 0 && rectF.bottom < u0Var.w(0)) {
            return null;
        }
        int f10 = f(u0Var, layout, yVar, i13, rectF, hVar, pVar, true);
        while (true) {
            i11 = i13;
            i12 = f10;
            if (i12 != -1 || i11 >= lineForVertical2) {
                break;
            }
            i13 = i11 + 1;
            f10 = f(u0Var, layout, yVar, i13, rectF, hVar, pVar, true);
        }
        if (i12 == -1) {
            return null;
        }
        int f11 = f(u0Var, layout, yVar, lineForVertical2, rectF, hVar, pVar, false);
        while (f11 == -1 && i11 < lineForVertical2) {
            lineForVertical2--;
            f11 = f(u0Var, layout, yVar, lineForVertical2, rectF, hVar, pVar, false);
        }
        if (f11 == -1) {
            return null;
        }
        return new int[]{hVar.c(i12 + 1), hVar.d(f11 - 1)};
    }

    private static final int e(y.a aVar, RectF rectF, int i10, int i11, int i12, float f10, float f11, float[] fArr, s2.d dVar, dn.p pVar) {
        int b10;
        int c10;
        int d10;
        int g10;
        if (!g(rectF, f10, f11)) {
            return -1;
        }
        if ((aVar.c() || rectF.left > f10) && (!aVar.c() || rectF.right < f11)) {
            b10 = aVar.b();
            int a10 = aVar.a();
            while (a10 - b10 > 1) {
                int i13 = (a10 + b10) / 2;
                float a11 = a(i13, i10, fArr);
                if ((aVar.c() || a11 <= rectF.left) && (!aVar.c() || a11 >= rectF.right)) {
                    b10 = i13;
                } else {
                    a10 = i13;
                }
            }
            if (aVar.c()) {
                b10 = a10;
            }
        } else {
            b10 = aVar.b();
        }
        int d11 = dVar.d(b10);
        if (d11 == -1 || (c10 = dVar.c(d11)) >= aVar.a()) {
            return -1;
        }
        d10 = jn.o.d(c10, aVar.b());
        g10 = jn.o.g(d11, aVar.a());
        RectF rectF2 = new RectF(BitmapDescriptorFactory.HUE_RED, i11, BitmapDescriptorFactory.HUE_RED, i12);
        while (true) {
            rectF2.left = aVar.c() ? a(g10 - 1, i10, fArr) : a(d10, i10, fArr);
            rectF2.right = aVar.c() ? b(d10, i10, fArr) : b(g10 - 1, i10, fArr);
            if (((Boolean) pVar.invoke(rectF2, rectF)).booleanValue()) {
                return d10;
            }
            d10 = dVar.a(d10);
            if (d10 == -1 || d10 >= aVar.a()) {
                break;
            }
            g10 = jn.o.g(dVar.d(d10), aVar.a());
        }
        return -1;
    }

    private static final int f(u0 u0Var, Layout layout, y yVar, int i10, RectF rectF, s2.d dVar, dn.p pVar, boolean z10) {
        int Z;
        jn.g m10;
        int i11;
        y.a[] aVarArr;
        int i12;
        int c10;
        int lineTop = layout.getLineTop(i10);
        int lineBottom = layout.getLineBottom(i10);
        int lineStart = layout.getLineStart(i10);
        int lineEnd = layout.getLineEnd(i10);
        if (lineStart == lineEnd) {
            return -1;
        }
        float[] fArr = new float[(lineEnd - lineStart) * 2];
        u0Var.b(i10, fArr);
        y.a[] d10 = yVar.d(i10);
        if (z10) {
            m10 = sm.p.V(d10);
        } else {
            Z = sm.p.Z(d10);
            m10 = jn.o.m(Z, 0);
        }
        int g10 = m10.g();
        int m11 = m10.m();
        int r10 = m10.r();
        if ((r10 <= 0 || g10 > m11) && (r10 >= 0 || m11 > g10)) {
            return -1;
        }
        int i13 = g10;
        while (true) {
            y.a aVar = d10[i13];
            float a10 = aVar.c() ? a(aVar.a() - 1, lineStart, fArr) : a(aVar.b(), lineStart, fArr);
            float b10 = aVar.c() ? b(aVar.b(), lineStart, fArr) : b(aVar.a() - 1, lineStart, fArr);
            if (z10) {
                i11 = i13;
                aVarArr = d10;
                i12 = m11;
                c10 = e(aVar, rectF, lineStart, lineTop, lineBottom, a10, b10, fArr, dVar, pVar);
            } else {
                i11 = i13;
                aVarArr = d10;
                i12 = m11;
                c10 = c(aVar, rectF, lineStart, lineTop, lineBottom, a10, b10, fArr, dVar, pVar);
            }
            if (c10 >= 0) {
                return c10;
            }
            if (i11 == i12) {
                return -1;
            }
            i13 = i11 + r10;
            m11 = i12;
            d10 = aVarArr;
        }
    }

    private static final boolean g(RectF rectF, float f10, float f11) {
        return f11 >= rectF.left && f10 <= rectF.right;
    }
}
