package o0;

import android.graphics.PointF;
import android.graphics.RectF;
import android.os.CancellationSignal;
import android.view.inputmethod.DeleteGesture;
import android.view.inputmethod.DeleteRangeGesture;
import android.view.inputmethod.HandwritingGesture;
import android.view.inputmethod.InsertGesture;
import android.view.inputmethod.JoinOrSplitGesture;
import android.view.inputmethod.PreviewableHandwritingGesture;
import android.view.inputmethod.RemoveSpaceGesture;
import android.view.inputmethod.SelectGesture;
import android.view.inputmethod.SelectRangeGesture;
import androidx.compose.ui.platform.y2;

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

    /* renamed from: a, reason: collision with root package name */
    public static final h1 f30454a = new h1();

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class a extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ kotlin.jvm.internal.m0 f30455p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ kotlin.jvm.internal.m0 f30456q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        a(kotlin.jvm.internal.m0 m0Var, kotlin.jvm.internal.m0 m0Var2) {
            super(1);
            this.f30455p = m0Var;
            this.f30456q = m0Var2;
        }

        @Override // dn.l
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final CharSequence invoke(kotlin.text.j jVar) {
            kotlin.jvm.internal.m0 m0Var = this.f30455p;
            if (m0Var.f26197p == -1) {
                m0Var.f26197p = jVar.d().g();
            }
            this.f30456q.f26197p = jVar.d().m() + 1;
            return "";
        }
    }

    private h1() {
    }

    private final void A(w1 w1Var, DeleteGesture deleteGesture, v1 v1Var) {
        RectF deletionArea;
        int granularity;
        long w10;
        deletionArea = deleteGesture.getDeletionArea();
        r1.i f10 = s1.k1.f(deletionArea);
        granularity = deleteGesture.getGranularity();
        w10 = i1.w(v1Var, f10, L(granularity), q2.h0.f33298a.h());
        e(w1Var, w10, n0.d.f29721a.a());
    }

    private final void B(l0.z zVar, DeleteRangeGesture deleteRangeGesture, r0.f0 f0Var) {
        RectF deletionStartArea;
        RectF deletionEndArea;
        int granularity;
        long x10;
        if (f0Var != null) {
            deletionStartArea = deleteRangeGesture.getDeletionStartArea();
            r1.i f10 = s1.k1.f(deletionStartArea);
            deletionEndArea = deleteRangeGesture.getDeletionEndArea();
            r1.i f11 = s1.k1.f(deletionEndArea);
            granularity = deleteRangeGesture.getGranularity();
            x10 = i1.x(zVar, f10, f11, L(granularity), q2.h0.f33298a.h());
            f0Var.X(x10);
        }
    }

    private final void C(w1 w1Var, DeleteRangeGesture deleteRangeGesture, v1 v1Var) {
        RectF deletionStartArea;
        RectF deletionEndArea;
        int granularity;
        long y10;
        deletionStartArea = deleteRangeGesture.getDeletionStartArea();
        r1.i f10 = s1.k1.f(deletionStartArea);
        deletionEndArea = deleteRangeGesture.getDeletionEndArea();
        r1.i f11 = s1.k1.f(deletionEndArea);
        granularity = deleteRangeGesture.getGranularity();
        y10 = i1.y(v1Var, f10, f11, L(granularity), q2.h0.f33298a.h());
        e(w1Var, y10, n0.d.f29721a.a());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void F(w1 w1Var) {
        w1.b(w1Var);
        w1.a(w1Var);
        p0.a aVar = p0.a.MergeIfPossible;
        throw null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void G(r0.f0 f0Var) {
        if (f0Var != null) {
            f0Var.n();
        }
    }

    private final void H(l0.z zVar, SelectGesture selectGesture, r0.f0 f0Var) {
        RectF selectionArea;
        int granularity;
        long v10;
        if (f0Var != null) {
            selectionArea = selectGesture.getSelectionArea();
            r1.i f10 = s1.k1.f(selectionArea);
            granularity = selectGesture.getGranularity();
            v10 = i1.v(zVar, f10, L(granularity), q2.h0.f33298a.h());
            f0Var.g0(v10);
        }
    }

    private final void I(w1 w1Var, SelectGesture selectGesture, v1 v1Var) {
        RectF selectionArea;
        int granularity;
        long w10;
        selectionArea = selectGesture.getSelectionArea();
        r1.i f10 = s1.k1.f(selectionArea);
        granularity = selectGesture.getGranularity();
        w10 = i1.w(v1Var, f10, L(granularity), q2.h0.f33298a.h());
        e(w1Var, w10, n0.d.f29721a.b());
    }

    private final void J(l0.z zVar, SelectRangeGesture selectRangeGesture, r0.f0 f0Var) {
        RectF selectionStartArea;
        RectF selectionEndArea;
        int granularity;
        long x10;
        if (f0Var != null) {
            selectionStartArea = selectRangeGesture.getSelectionStartArea();
            r1.i f10 = s1.k1.f(selectionStartArea);
            selectionEndArea = selectRangeGesture.getSelectionEndArea();
            r1.i f11 = s1.k1.f(selectionEndArea);
            granularity = selectRangeGesture.getGranularity();
            x10 = i1.x(zVar, f10, f11, L(granularity), q2.h0.f33298a.h());
            f0Var.g0(x10);
        }
    }

    private final void K(w1 w1Var, SelectRangeGesture selectRangeGesture, v1 v1Var) {
        RectF selectionStartArea;
        RectF selectionEndArea;
        int granularity;
        long y10;
        selectionStartArea = selectRangeGesture.getSelectionStartArea();
        r1.i f10 = s1.k1.f(selectionStartArea);
        selectionEndArea = selectRangeGesture.getSelectionEndArea();
        r1.i f11 = s1.k1.f(selectionEndArea);
        granularity = selectRangeGesture.getGranularity();
        y10 = i1.y(v1Var, f10, f11, L(granularity), q2.h0.f33298a.h());
        e(w1Var, y10, n0.d.f29721a.b());
    }

    private final int L(int i10) {
        return i10 != 1 ? i10 != 2 ? q2.d0.f33285a.a() : q2.d0.f33285a.a() : q2.d0.f33285a.b();
    }

    private final int c(w1 w1Var, HandwritingGesture handwritingGesture) {
        w1.b(w1Var);
        w1.a(w1Var);
        p0.a aVar = p0.a.MergeIfPossible;
        throw null;
    }

    private final int d(HandwritingGesture handwritingGesture, dn.l lVar) {
        String fallbackText;
        fallbackText = handwritingGesture.getFallbackText();
        if (fallbackText == null) {
            return 3;
        }
        lVar.invoke(new w2.a(fallbackText, 1));
        return 5;
    }

    private final void e(w1 w1Var, long j10, int i10) {
        if (!q2.m0.h(j10)) {
            throw null;
        }
        w1.b(w1Var);
        w1.a(w1Var);
        p0.a aVar = p0.a.MergeIfPossible;
        throw null;
    }

    private final int f(l0.z zVar, DeleteGesture deleteGesture, q2.d dVar, dn.l lVar) {
        int granularity;
        RectF deletionArea;
        long v10;
        granularity = deleteGesture.getGranularity();
        int L = L(granularity);
        deletionArea = deleteGesture.getDeletionArea();
        v10 = i1.v(zVar, s1.k1.f(deletionArea), L, q2.h0.f33298a.h());
        if (q2.m0.h(v10)) {
            return f30454a.d(l0.a(deleteGesture), lVar);
        }
        k(v10, dVar, q2.d0.d(L, q2.d0.f33285a.b()), lVar);
        return 1;
    }

    private final int g(w1 w1Var, DeleteGesture deleteGesture, v1 v1Var) {
        int granularity;
        RectF deletionArea;
        long w10;
        granularity = deleteGesture.getGranularity();
        int L = L(granularity);
        deletionArea = deleteGesture.getDeletionArea();
        w10 = i1.w(v1Var, s1.k1.f(deletionArea), L, q2.h0.f33298a.h());
        if (q2.m0.h(w10)) {
            return f30454a.c(w1Var, l0.a(deleteGesture));
        }
        j(w1Var, w10, q2.d0.d(L, q2.d0.f33285a.b()));
        return 1;
    }

    private final int h(l0.z zVar, DeleteRangeGesture deleteRangeGesture, q2.d dVar, dn.l lVar) {
        int granularity;
        RectF deletionStartArea;
        RectF deletionEndArea;
        long x10;
        granularity = deleteRangeGesture.getGranularity();
        int L = L(granularity);
        deletionStartArea = deleteRangeGesture.getDeletionStartArea();
        r1.i f10 = s1.k1.f(deletionStartArea);
        deletionEndArea = deleteRangeGesture.getDeletionEndArea();
        x10 = i1.x(zVar, f10, s1.k1.f(deletionEndArea), L, q2.h0.f33298a.h());
        if (q2.m0.h(x10)) {
            return f30454a.d(l0.a(deleteRangeGesture), lVar);
        }
        k(x10, dVar, q2.d0.d(L, q2.d0.f33285a.b()), lVar);
        return 1;
    }

    private final int i(w1 w1Var, DeleteRangeGesture deleteRangeGesture, v1 v1Var) {
        int granularity;
        RectF deletionStartArea;
        RectF deletionEndArea;
        long y10;
        granularity = deleteRangeGesture.getGranularity();
        int L = L(granularity);
        deletionStartArea = deleteRangeGesture.getDeletionStartArea();
        r1.i f10 = s1.k1.f(deletionStartArea);
        deletionEndArea = deleteRangeGesture.getDeletionEndArea();
        y10 = i1.y(v1Var, f10, s1.k1.f(deletionEndArea), L, q2.h0.f33298a.h());
        if (q2.m0.h(y10)) {
            return f30454a.c(w1Var, l0.a(deleteRangeGesture));
        }
        j(w1Var, y10, q2.d0.d(L, q2.d0.f33285a.b()));
        return 1;
    }

    private final void j(w1 w1Var, long j10, boolean z10) {
        if (z10) {
            throw null;
        }
        w1.c(w1Var, "", j10, null, false, 12, null);
    }

    private final void k(long j10, q2.d dVar, boolean z10, dn.l lVar) {
        w2.i n10;
        if (z10) {
            j10 = i1.m(j10, dVar);
        }
        n10 = i1.n(new w2.p0(q2.m0.i(j10), q2.m0.i(j10)), new w2.g(q2.m0.j(j10), 0));
        lVar.invoke(n10);
    }

    private final int n(l0.z zVar, InsertGesture insertGesture, y2 y2Var, dn.l lVar) {
        PointF insertionPoint;
        long F;
        int q10;
        String textToInsert;
        q2.j0 f10;
        boolean z10;
        if (y2Var == null) {
            return d(l0.a(insertGesture), lVar);
        }
        insertionPoint = insertGesture.getInsertionPoint();
        F = i1.F(insertionPoint);
        q10 = i1.q(zVar, F, y2Var);
        if (q10 != -1) {
            l0.a1 j10 = zVar.j();
            boolean z11 = false;
            if (j10 != null && (f10 = j10.f()) != null) {
                z10 = i1.z(f10, q10);
                if (z10) {
                    z11 = true;
                }
            }
            if (!z11) {
                textToInsert = insertGesture.getTextToInsert();
                p(q10, textToInsert, lVar);
                return 1;
            }
        }
        return d(l0.a(insertGesture), lVar);
    }

    private final int o(w1 w1Var, InsertGesture insertGesture, v1 v1Var, y2 y2Var) {
        PointF insertionPoint;
        long F;
        int r10;
        String textToInsert;
        insertionPoint = insertGesture.getInsertionPoint();
        F = i1.F(insertionPoint);
        r10 = i1.r(v1Var, F, y2Var);
        if (r10 == -1) {
            return c(w1Var, l0.a(insertGesture));
        }
        textToInsert = insertGesture.getTextToInsert();
        w1.c(w1Var, textToInsert, q2.n0.a(r10), null, false, 12, null);
        return 1;
    }

    private final void p(int i10, String str, dn.l lVar) {
        w2.i n10;
        n10 = i1.n(new w2.p0(i10, i10), new w2.a(str, 1));
        lVar.invoke(n10);
    }

    private final int q(l0.z zVar, JoinOrSplitGesture joinOrSplitGesture, q2.d dVar, y2 y2Var, dn.l lVar) {
        PointF joinOrSplitPoint;
        long F;
        int q10;
        long E;
        q2.j0 f10;
        boolean z10;
        if (y2Var == null) {
            return d(l0.a(joinOrSplitGesture), lVar);
        }
        joinOrSplitPoint = joinOrSplitGesture.getJoinOrSplitPoint();
        F = i1.F(joinOrSplitPoint);
        q10 = i1.q(zVar, F, y2Var);
        if (q10 != -1) {
            l0.a1 j10 = zVar.j();
            boolean z11 = false;
            if (j10 != null && (f10 = j10.f()) != null) {
                z10 = i1.z(f10, q10);
                if (z10) {
                    z11 = true;
                }
            }
            if (!z11) {
                E = i1.E(dVar, q10);
                if (q2.m0.h(E)) {
                    p(q2.m0.n(E), " ", lVar);
                } else {
                    k(E, dVar, false, lVar);
                }
                return 1;
            }
        }
        return d(l0.a(joinOrSplitGesture), lVar);
    }

    private final int r(w1 w1Var, JoinOrSplitGesture joinOrSplitGesture, v1 v1Var, y2 y2Var) {
        throw null;
    }

    private final int s(l0.z zVar, RemoveSpaceGesture removeSpaceGesture, q2.d dVar, y2 y2Var, dn.l lVar) {
        PointF startPoint;
        long F;
        PointF endPoint;
        long F2;
        long t10;
        w2.i n10;
        l0.a1 j10 = zVar.j();
        q2.j0 f10 = j10 != null ? j10.f() : null;
        startPoint = removeSpaceGesture.getStartPoint();
        F = i1.F(startPoint);
        endPoint = removeSpaceGesture.getEndPoint();
        F2 = i1.F(endPoint);
        t10 = i1.t(f10, F, F2, zVar.i(), y2Var);
        if (q2.m0.h(t10)) {
            return f30454a.d(l0.a(removeSpaceGesture), lVar);
        }
        kotlin.jvm.internal.m0 m0Var = new kotlin.jvm.internal.m0();
        m0Var.f26197p = -1;
        kotlin.jvm.internal.m0 m0Var2 = new kotlin.jvm.internal.m0();
        m0Var2.f26197p = -1;
        String f11 = new kotlin.text.l("\\s+").f(q2.n0.e(dVar, t10), new a(m0Var, m0Var2));
        if (m0Var.f26197p == -1 || m0Var2.f26197p == -1) {
            return d(l0.a(removeSpaceGesture), lVar);
        }
        int n11 = q2.m0.n(t10) + m0Var.f26197p;
        int n12 = q2.m0.n(t10) + m0Var2.f26197p;
        String substring = f11.substring(m0Var.f26197p, f11.length() - (q2.m0.j(t10) - m0Var2.f26197p));
        kotlin.jvm.internal.t.f(substring, "this as java.lang.String…ing(startIndex, endIndex)");
        n10 = i1.n(new w2.p0(n11, n12), new w2.a(substring, 1));
        lVar.invoke(n10);
        return 1;
    }

    private final int t(w1 w1Var, RemoveSpaceGesture removeSpaceGesture, v1 v1Var, y2 y2Var) {
        throw null;
    }

    private final int u(l0.z zVar, SelectGesture selectGesture, r0.f0 f0Var, dn.l lVar) {
        RectF selectionArea;
        int granularity;
        long v10;
        selectionArea = selectGesture.getSelectionArea();
        r1.i f10 = s1.k1.f(selectionArea);
        granularity = selectGesture.getGranularity();
        v10 = i1.v(zVar, f10, L(granularity), q2.h0.f33298a.h());
        if (q2.m0.h(v10)) {
            return f30454a.d(l0.a(selectGesture), lVar);
        }
        y(v10, f0Var, lVar);
        return 1;
    }

    private final int v(w1 w1Var, SelectGesture selectGesture, v1 v1Var) {
        RectF selectionArea;
        int granularity;
        long w10;
        selectionArea = selectGesture.getSelectionArea();
        r1.i f10 = s1.k1.f(selectionArea);
        granularity = selectGesture.getGranularity();
        w10 = i1.w(v1Var, f10, L(granularity), q2.h0.f33298a.h());
        if (q2.m0.h(w10)) {
            return f30454a.c(w1Var, l0.a(selectGesture));
        }
        throw null;
    }

    private final int w(l0.z zVar, SelectRangeGesture selectRangeGesture, r0.f0 f0Var, dn.l lVar) {
        RectF selectionStartArea;
        RectF selectionEndArea;
        int granularity;
        long x10;
        selectionStartArea = selectRangeGesture.getSelectionStartArea();
        r1.i f10 = s1.k1.f(selectionStartArea);
        selectionEndArea = selectRangeGesture.getSelectionEndArea();
        r1.i f11 = s1.k1.f(selectionEndArea);
        granularity = selectRangeGesture.getGranularity();
        x10 = i1.x(zVar, f10, f11, L(granularity), q2.h0.f33298a.h());
        if (q2.m0.h(x10)) {
            return f30454a.d(l0.a(selectRangeGesture), lVar);
        }
        y(x10, f0Var, lVar);
        return 1;
    }

    private final int x(w1 w1Var, SelectRangeGesture selectRangeGesture, v1 v1Var) {
        RectF selectionStartArea;
        RectF selectionEndArea;
        int granularity;
        long y10;
        selectionStartArea = selectRangeGesture.getSelectionStartArea();
        r1.i f10 = s1.k1.f(selectionStartArea);
        selectionEndArea = selectRangeGesture.getSelectionEndArea();
        r1.i f11 = s1.k1.f(selectionEndArea);
        granularity = selectRangeGesture.getGranularity();
        y10 = i1.y(v1Var, f10, f11, L(granularity), q2.h0.f33298a.h());
        if (q2.m0.h(y10)) {
            return f30454a.c(w1Var, l0.a(selectRangeGesture));
        }
        throw null;
    }

    private final void y(long j10, r0.f0 f0Var, dn.l lVar) {
        lVar.invoke(new w2.p0(q2.m0.n(j10), q2.m0.i(j10)));
        if (f0Var != null) {
            f0Var.v(true);
        }
    }

    private final void z(l0.z zVar, DeleteGesture deleteGesture, r0.f0 f0Var) {
        RectF deletionArea;
        int granularity;
        long v10;
        if (f0Var != null) {
            deletionArea = deleteGesture.getDeletionArea();
            r1.i f10 = s1.k1.f(deletionArea);
            granularity = deleteGesture.getGranularity();
            v10 = i1.v(zVar, f10, L(granularity), q2.h0.f33298a.h());
            f0Var.X(v10);
        }
    }

    public final boolean D(l0.z zVar, PreviewableHandwritingGesture previewableHandwritingGesture, final r0.f0 f0Var, CancellationSignal cancellationSignal) {
        q2.j0 f10;
        q2.i0 l10;
        q2.d w10 = zVar.w();
        if (w10 == null) {
            return false;
        }
        l0.a1 j10 = zVar.j();
        if (!kotlin.jvm.internal.t.b(w10, (j10 == null || (f10 = j10.f()) == null || (l10 = f10.l()) == null) ? null : l10.j())) {
            return false;
        }
        if (e1.a(previewableHandwritingGesture)) {
            H(zVar, b0.a(previewableHandwritingGesture), f0Var);
        } else if (c0.a(previewableHandwritingGesture)) {
            z(zVar, d0.a(previewableHandwritingGesture), f0Var);
        } else if (e0.a(previewableHandwritingGesture)) {
            J(zVar, f0.a(previewableHandwritingGesture), f0Var);
        } else {
            if (!g0.a(previewableHandwritingGesture)) {
                return false;
            }
            B(zVar, h0.a(previewableHandwritingGesture), f0Var);
        }
        if (cancellationSignal == null) {
            return true;
        }
        cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() { // from class: o0.g1
            @Override // android.os.CancellationSignal.OnCancelListener
            public final void onCancel() {
                h1.G(r0.f0.this);
            }
        });
        return true;
    }

    public final boolean E(final w1 w1Var, PreviewableHandwritingGesture previewableHandwritingGesture, v1 v1Var, CancellationSignal cancellationSignal) {
        if (e1.a(previewableHandwritingGesture)) {
            I(w1Var, b0.a(previewableHandwritingGesture), v1Var);
        } else if (c0.a(previewableHandwritingGesture)) {
            A(w1Var, d0.a(previewableHandwritingGesture), v1Var);
        } else if (e0.a(previewableHandwritingGesture)) {
            K(w1Var, f0.a(previewableHandwritingGesture), v1Var);
        } else {
            if (!g0.a(previewableHandwritingGesture)) {
                return false;
            }
            C(w1Var, h0.a(previewableHandwritingGesture), v1Var);
        }
        if (cancellationSignal == null) {
            return true;
        }
        cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener(w1Var) { // from class: o0.f1
            @Override // android.os.CancellationSignal.OnCancelListener
            public final void onCancel() {
                h1.F(null);
            }
        });
        return true;
    }

    public final int l(l0.z zVar, HandwritingGesture handwritingGesture, r0.f0 f0Var, y2 y2Var, dn.l lVar) {
        q2.j0 f10;
        q2.i0 l10;
        q2.d w10 = zVar.w();
        if (w10 == null) {
            return 3;
        }
        l0.a1 j10 = zVar.j();
        if (!kotlin.jvm.internal.t.b(w10, (j10 == null || (f10 = j10.f()) == null || (l10 = f10.l()) == null) ? null : l10.j())) {
            return 3;
        }
        if (e1.a(handwritingGesture)) {
            return u(zVar, b0.a(handwritingGesture), f0Var, lVar);
        }
        if (c0.a(handwritingGesture)) {
            return f(zVar, d0.a(handwritingGesture), w10, lVar);
        }
        if (e0.a(handwritingGesture)) {
            return w(zVar, f0.a(handwritingGesture), f0Var, lVar);
        }
        if (g0.a(handwritingGesture)) {
            return h(zVar, h0.a(handwritingGesture), w10, lVar);
        }
        if (n0.a(handwritingGesture)) {
            return q(zVar, o0.a(handwritingGesture), w10, y2Var, lVar);
        }
        if (i0.a(handwritingGesture)) {
            return n(zVar, j0.a(handwritingGesture), y2Var, lVar);
        }
        if (k0.a(handwritingGesture)) {
            return s(zVar, m0.a(handwritingGesture), w10, y2Var, lVar);
        }
        return 2;
    }

    public final int m(w1 w1Var, HandwritingGesture handwritingGesture, v1 v1Var, y2 y2Var) {
        if (e1.a(handwritingGesture)) {
            return v(w1Var, b0.a(handwritingGesture), v1Var);
        }
        if (c0.a(handwritingGesture)) {
            return g(w1Var, d0.a(handwritingGesture), v1Var);
        }
        if (e0.a(handwritingGesture)) {
            return x(w1Var, f0.a(handwritingGesture), v1Var);
        }
        if (g0.a(handwritingGesture)) {
            return i(w1Var, h0.a(handwritingGesture), v1Var);
        }
        if (n0.a(handwritingGesture)) {
            return r(w1Var, o0.a(handwritingGesture), v1Var, y2Var);
        }
        if (i0.a(handwritingGesture)) {
            return o(w1Var, j0.a(handwritingGesture), v1Var, y2Var);
        }
        if (k0.a(handwritingGesture)) {
            return t(w1Var, m0.a(handwritingGesture), v1Var, y2Var);
        }
        return 2;
    }
}
