package r2;

import android.text.Layout;
import android.text.TextUtils;
import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
import java.text.Bidi;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public final class y {

    /* renamed from: a, reason: collision with root package name */
    private final Layout f34416a;

    /* renamed from: b, reason: collision with root package name */
    private final List f34417b;

    /* renamed from: c, reason: collision with root package name */
    private final List f34418c;

    /* renamed from: d, reason: collision with root package name */
    private final boolean[] f34419d;

    /* renamed from: e, reason: collision with root package name */
    private char[] f34420e;

    /* renamed from: f, reason: collision with root package name */
    private final int f34421f;

    /* loaded from: classes.dex */
    public static final class a {

        /* renamed from: a, reason: collision with root package name */
        private final int f34422a;

        /* renamed from: b, reason: collision with root package name */
        private final int f34423b;

        /* renamed from: c, reason: collision with root package name */
        private final boolean f34424c;

        public a(int i10, int i11, boolean z10) {
            this.f34422a = i10;
            this.f34423b = i11;
            this.f34424c = z10;
        }

        public final int a() {
            return this.f34423b;
        }

        public final int b() {
            return this.f34422a;
        }

        public final boolean c() {
            return this.f34424c;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof a)) {
                return false;
            }
            a aVar = (a) obj;
            return this.f34422a == aVar.f34422a && this.f34423b == aVar.f34423b && this.f34424c == aVar.f34424c;
        }

        public int hashCode() {
            return (((Integer.hashCode(this.f34422a) * 31) + Integer.hashCode(this.f34423b)) * 31) + Boolean.hashCode(this.f34424c);
        }

        public String toString() {
            return "BidiRun(start=" + this.f34422a + ", end=" + this.f34423b + ", isRtl=" + this.f34424c + ')';
        }
    }

    public y(Layout layout) {
        int g02;
        this.f34416a = layout;
        ArrayList arrayList = new ArrayList();
        int i10 = 0;
        do {
            g02 = kotlin.text.z.g0(this.f34416a.getText(), '\n', i10, false, 4, null);
            i10 = g02 < 0 ? this.f34416a.getText().length() : g02 + 1;
            arrayList.add(Integer.valueOf(i10));
        } while (i10 < this.f34416a.getText().length());
        this.f34417b = arrayList;
        int size = arrayList.size();
        ArrayList arrayList2 = new ArrayList(size);
        for (int i11 = 0; i11 < size; i11++) {
            arrayList2.add(null);
        }
        this.f34418c = arrayList2;
        this.f34419d = new boolean[this.f34417b.size()];
        this.f34421f = this.f34417b.size();
    }

    private final float b(int i10, boolean z10) {
        int g10;
        g10 = jn.o.g(i10, this.f34416a.getLineEnd(this.f34416a.getLineForOffset(i10)));
        return z10 ? this.f34416a.getPrimaryHorizontal(g10) : this.f34416a.getSecondaryHorizontal(g10);
    }

    public static /* synthetic */ int g(y yVar, int i10, boolean z10, int i11, Object obj) {
        if ((i11 & 2) != 0) {
            z10 = false;
        }
        return yVar.f(i10, z10);
    }

    private final int k(int i10, int i11) {
        while (i10 > i11 && i(this.f34416a.getText().charAt(i10 - 1))) {
            i10--;
        }
        return i10;
    }

    /* JADX WARN: Code restructure failed: missing block: B:16:0x005d, code lost:
    
        if (r0.getRunCount() == 1) goto L19;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bidi a(int i10) {
        Bidi bidi;
        if (this.f34419d[i10]) {
            return (Bidi) this.f34418c.get(i10);
        }
        int intValue = i10 == 0 ? 0 : ((Number) this.f34417b.get(i10 - 1)).intValue();
        int intValue2 = ((Number) this.f34417b.get(i10)).intValue();
        int i11 = intValue2 - intValue;
        char[] cArr = this.f34420e;
        if (cArr == null || cArr.length < i11) {
            cArr = new char[i11];
        }
        char[] cArr2 = cArr;
        TextUtils.getChars(this.f34416a.getText(), intValue, intValue2, cArr2, 0);
        if (Bidi.requiresBidi(cArr2, 0, i11)) {
            bidi = new Bidi(cArr2, 0, null, 0, i11, j(i10) ? 1 : 0);
        }
        bidi = null;
        this.f34418c.set(i10, bidi);
        this.f34419d[i10] = true;
        if (bidi != null) {
            char[] cArr3 = this.f34420e;
            cArr2 = cArr2 == cArr3 ? null : cArr3;
        }
        this.f34420e = cArr2;
        return bidi;
    }

    public final float c(int i10, boolean z10, boolean z11) {
        int Z;
        int Z2;
        int i11 = i10;
        if (!z11) {
            return b(i10, z10);
        }
        int a10 = x.a(this.f34416a, i11, z11);
        int lineStart = this.f34416a.getLineStart(a10);
        int lineEnd = this.f34416a.getLineEnd(a10);
        if (i11 != lineStart && i11 != lineEnd) {
            return b(i10, z10);
        }
        if (i11 == 0 || i11 == this.f34416a.getText().length()) {
            return b(i10, z10);
        }
        int f10 = f(i11, z11);
        boolean j10 = j(f10);
        int k10 = k(lineEnd, lineStart);
        int h10 = h(f10);
        int i12 = lineStart - h10;
        int i13 = k10 - h10;
        Bidi a11 = a(f10);
        Bidi createLineBidi = a11 != null ? a11.createLineBidi(i12, i13) : null;
        boolean z12 = false;
        if (createLineBidi == null || createLineBidi.getRunCount() == 1) {
            boolean isRtlCharAt = this.f34416a.isRtlCharAt(lineStart);
            if (z10 || j10 == isRtlCharAt) {
                j10 = !j10;
            }
            if (i11 == lineStart) {
                z12 = j10;
            } else if (!j10) {
                z12 = true;
            }
            Layout layout = this.f34416a;
            return z12 ? layout.getLineLeft(a10) : layout.getLineRight(a10);
        }
        int runCount = createLineBidi.getRunCount();
        a[] aVarArr = new a[runCount];
        for (int i14 = 0; i14 < runCount; i14++) {
            aVarArr[i14] = new a(createLineBidi.getRunStart(i14) + lineStart, createLineBidi.getRunLimit(i14) + lineStart, createLineBidi.getRunLevel(i14) % 2 == 1);
        }
        int runCount2 = createLineBidi.getRunCount();
        byte[] bArr = new byte[runCount2];
        for (int i15 = 0; i15 < runCount2; i15++) {
            bArr[i15] = (byte) createLineBidi.getRunLevel(i15);
        }
        Bidi.reorderVisually(bArr, 0, aVarArr, 0, runCount);
        int i16 = -1;
        if (i11 == lineStart) {
            int i17 = 0;
            while (true) {
                if (i17 >= runCount) {
                    break;
                }
                if (aVarArr[i17].b() == i11) {
                    i16 = i17;
                    break;
                }
                i17++;
            }
            a aVar = aVarArr[i16];
            if (z10 || j10 == aVar.c()) {
                j10 = !j10;
            }
            if (i16 == 0 && j10) {
                return this.f34416a.getLineLeft(a10);
            }
            Z2 = sm.p.Z(aVarArr);
            return (i16 != Z2 || j10) ? j10 ? this.f34416a.getPrimaryHorizontal(aVarArr[i16 - 1].b()) : this.f34416a.getPrimaryHorizontal(aVarArr[i16 + 1].b()) : this.f34416a.getLineRight(a10);
        }
        if (i11 > k10) {
            i11 = k(i11, lineStart);
        }
        int i18 = 0;
        while (true) {
            if (i18 >= runCount) {
                break;
            }
            if (aVarArr[i18].a() == i11) {
                i16 = i18;
                break;
            }
            i18++;
        }
        a aVar2 = aVarArr[i16];
        if (!z10 && j10 != aVar2.c()) {
            j10 = !j10;
        }
        if (i16 == 0 && j10) {
            return this.f34416a.getLineLeft(a10);
        }
        Z = sm.p.Z(aVarArr);
        return (i16 != Z || j10) ? j10 ? this.f34416a.getPrimaryHorizontal(aVarArr[i16 - 1].a()) : this.f34416a.getPrimaryHorizontal(aVarArr[i16 + 1].a()) : this.f34416a.getLineRight(a10);
    }

    public final a[] d(int i10) {
        Bidi createLineBidi;
        int lineStart = this.f34416a.getLineStart(i10);
        int lineEnd = this.f34416a.getLineEnd(i10);
        int g10 = g(this, lineStart, false, 2, null);
        int h10 = h(g10);
        int i11 = lineStart - h10;
        int i12 = lineEnd - h10;
        Bidi a10 = a(g10);
        if (a10 == null || (createLineBidi = a10.createLineBidi(i11, i12)) == null) {
            return new a[]{new a(lineStart, lineEnd, this.f34416a.isRtlCharAt(lineStart))};
        }
        int runCount = createLineBidi.getRunCount();
        a[] aVarArr = new a[runCount];
        for (int i13 = 0; i13 < runCount; i13++) {
            int runStart = createLineBidi.getRunStart(i13) + lineStart;
            int runLimit = createLineBidi.getRunLimit(i13) + lineStart;
            boolean z10 = true;
            if (createLineBidi.getRunLevel(i13) % 2 != 1) {
                z10 = false;
            }
            aVarArr[i13] = new a(runStart, runLimit, z10);
        }
        return aVarArr;
    }

    public final int e(int i10) {
        return k(this.f34416a.getLineEnd(i10), this.f34416a.getLineStart(i10));
    }

    public final int f(int i10, boolean z10) {
        int j10;
        j10 = sm.u.j(this.f34417b, Integer.valueOf(i10), 0, 0, 6, null);
        int i11 = j10 < 0 ? -(j10 + 1) : j10 + 1;
        if (z10 && i11 > 0) {
            int i12 = i11 - 1;
            if (i10 == ((Number) this.f34417b.get(i12)).intValue()) {
                return i12;
            }
        }
        return i11;
    }

    public final int h(int i10) {
        if (i10 == 0) {
            return 0;
        }
        return ((Number) this.f34417b.get(i10 - 1)).intValue();
    }

    public final boolean i(char c10) {
        return c10 == ' ' || c10 == '\n' || c10 == 5760 || (kotlin.jvm.internal.t.i(c10, UserMetadata.MAX_INTERNAL_KEY_SIZE) >= 0 && kotlin.jvm.internal.t.i(c10, 8202) <= 0 && c10 != 8199) || c10 == 8287 || c10 == 12288;
    }

    public final boolean j(int i10) {
        return this.f34416a.getParagraphDirection(this.f34416a.getLineForOffset(h(i10))) == -1;
    }
}
