package androidx.emoji2.text;

import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.MetaKeyKeyListener;
import android.view.KeyEvent;
import android.view.inputmethod.InputConnection;
import androidx.emoji2.text.C0229g;
import androidx.emoji2.text.C0255p;
import java.util.Arrays;

/* renamed from: androidx.emoji2.text.k */
/* loaded from: classes.dex */
public final class C0243k {

    /* renamed from: a */
    public final C0229g.C0239i f1105a;

    /* renamed from: b */
    public final C0255p f1106b;

    /* renamed from: c */
    public C0229g.InterfaceC0234d f1107c;

    /* renamed from: d */
    public final boolean f1108d;

    /* renamed from: e */
    public final int[] f1109e;

    /* renamed from: androidx.emoji2.text.k$a */
    /* loaded from: classes.dex */
    public static final class C0244a {
        /* renamed from: a */
        public static int m4352a(CharSequence charSequence, int i, int i2) {
            boolean isSurrogate;
            int length = charSequence.length();
            if (i < 0 || length < i || i2 < 0) {
                return -1;
            }
            while (true) {
                boolean z = false;
                while (i2 != 0) {
                    i--;
                    if (i < 0) {
                        return z ? -1 : 0;
                    }
                    char charAt = charSequence.charAt(i);
                    if (!z) {
                        isSurrogate = Character.isSurrogate(charAt);
                        if (!isSurrogate) {
                            i2--;
                        } else if (Character.isHighSurrogate(charAt)) {
                            return -1;
                        } else {
                            z = true;
                        }
                    } else if (!Character.isHighSurrogate(charAt)) {
                        return -1;
                    } else {
                        i2--;
                    }
                }
                return i;
            }
        }

        /* renamed from: b */
        public static int m4351b(CharSequence charSequence, int i, int i2) {
            boolean isSurrogate;
            int length = charSequence.length();
            if (i < 0 || length < i || i2 < 0) {
                return -1;
            }
            while (true) {
                boolean z = false;
                while (i2 != 0) {
                    if (i >= length) {
                        if (z) {
                            return -1;
                        }
                        return length;
                    }
                    char charAt = charSequence.charAt(i);
                    if (!z) {
                        isSurrogate = Character.isSurrogate(charAt);
                        if (!isSurrogate) {
                            i2--;
                            i++;
                        } else if (Character.isLowSurrogate(charAt)) {
                            return -1;
                        } else {
                            i++;
                            z = true;
                        }
                    } else if (!Character.isLowSurrogate(charAt)) {
                        return -1;
                    } else {
                        i2--;
                        i++;
                    }
                }
                return i;
            }
        }
    }

    /* renamed from: androidx.emoji2.text.k$b */
    /* loaded from: classes.dex */
    public static final class C0245b {

        /* renamed from: a */
        public int f1110a = 1;

        /* renamed from: b */
        public final C0255p.C0256a f1111b;

        /* renamed from: c */
        public C0255p.C0256a f1112c;

        /* renamed from: d */
        public C0255p.C0256a f1113d;

        /* renamed from: e */
        public int f1114e;

        /* renamed from: f */
        public int f1115f;

        /* renamed from: g */
        public final boolean f1116g;

        /* renamed from: h */
        public final int[] f1117h;

        public C0245b(C0255p.C0256a c0256a, boolean z, int[] iArr) {
            this.f1111b = c0256a;
            this.f1112c = c0256a;
            this.f1116g = z;
            this.f1117h = iArr;
        }

        /* renamed from: d */
        public static boolean m4347d(int i) {
            return i == 65039;
        }

        /* renamed from: f */
        public static boolean m4345f(int i) {
            return i == 65038;
        }

        /* renamed from: a */
        public int m4350a(int i) {
            C0255p.C0256a m4314a = this.f1112c.m4314a(i);
            int i2 = 2;
            if (this.f1110a != 2) {
                if (m4314a != null) {
                    this.f1110a = 2;
                    this.f1112c = m4314a;
                    this.f1115f = 1;
                }
                i2 = m4344g();
            } else if (m4314a != null) {
                this.f1112c = m4314a;
                this.f1115f++;
            } else {
                if (!m4345f(i)) {
                    if (!m4347d(i)) {
                        if (this.f1112c.m4313b() != null) {
                            i2 = 3;
                            if (this.f1115f != 1 || m4343h()) {
                                this.f1113d = this.f1112c;
                                m4344g();
                            }
                        }
                    }
                }
                i2 = m4344g();
            }
            this.f1114e = i;
            return i2;
        }

        /* renamed from: b */
        public C0241i m4349b() {
            return this.f1112c.m4313b();
        }

        /* renamed from: c */
        public C0241i m4348c() {
            return this.f1113d.m4313b();
        }

        /* renamed from: e */
        public boolean m4346e() {
            return this.f1110a == 2 && this.f1112c.m4313b() != null && (this.f1115f > 1 || m4343h());
        }

        /* renamed from: g */
        public final int m4344g() {
            this.f1110a = 1;
            this.f1112c = this.f1111b;
            this.f1115f = 0;
            return 1;
        }

        /* renamed from: h */
        public final boolean m4343h() {
            if (this.f1112c.m4313b().m4363j() || m4347d(this.f1114e)) {
                return true;
            }
            if (this.f1116g) {
                if (this.f1117h == null) {
                    return true;
                }
                if (Arrays.binarySearch(this.f1117h, this.f1112c.m4313b().m4371b(0)) < 0) {
                    return true;
                }
            }
            return false;
        }
    }

    public C0243k(C0255p c0255p, C0229g.C0239i c0239i, C0229g.InterfaceC0234d interfaceC0234d, boolean z, int[] iArr) {
        this.f1105a = c0239i;
        this.f1106b = c0255p;
        this.f1107c = interfaceC0234d;
        this.f1108d = z;
        this.f1109e = iArr;
    }

    /* renamed from: b */
    public static boolean m4359b(Editable editable, KeyEvent keyEvent, boolean z) {
        AbstractC0246l[] abstractC0246lArr;
        if (m4354g(keyEvent)) {
            return false;
        }
        int selectionStart = Selection.getSelectionStart(editable);
        int selectionEnd = Selection.getSelectionEnd(editable);
        if (!m4355f(selectionStart, selectionEnd) && (abstractC0246lArr = (AbstractC0246l[]) editable.getSpans(selectionStart, selectionEnd, AbstractC0246l.class)) != null && abstractC0246lArr.length > 0) {
            for (AbstractC0246l abstractC0246l : abstractC0246lArr) {
                int spanStart = editable.getSpanStart(abstractC0246l);
                int spanEnd = editable.getSpanEnd(abstractC0246l);
                if ((z && spanStart == selectionStart) || ((!z && spanEnd == selectionStart) || (selectionStart > spanStart && selectionStart < spanEnd))) {
                    editable.delete(spanStart, spanEnd);
                    return true;
                }
            }
        }
        return false;
    }

    /* renamed from: c */
    public static boolean m4358c(InputConnection inputConnection, Editable editable, int i, int i2, boolean z) {
        int max;
        int min;
        if (editable != null && inputConnection != null && i >= 0 && i2 >= 0) {
            int selectionStart = Selection.getSelectionStart(editable);
            int selectionEnd = Selection.getSelectionEnd(editable);
            if (m4355f(selectionStart, selectionEnd)) {
                return false;
            }
            if (z) {
                max = C0244a.m4352a(editable, selectionStart, Math.max(i, 0));
                min = C0244a.m4351b(editable, selectionEnd, Math.max(i2, 0));
                if (max == -1 || min == -1) {
                    return false;
                }
            } else {
                max = Math.max(selectionStart - i, 0);
                min = Math.min(selectionEnd + i2, editable.length());
            }
            AbstractC0246l[] abstractC0246lArr = (AbstractC0246l[]) editable.getSpans(max, min, AbstractC0246l.class);
            if (abstractC0246lArr != null && abstractC0246lArr.length > 0) {
                for (AbstractC0246l abstractC0246l : abstractC0246lArr) {
                    int spanStart = editable.getSpanStart(abstractC0246l);
                    int spanEnd = editable.getSpanEnd(abstractC0246l);
                    max = Math.min(spanStart, max);
                    min = Math.max(spanEnd, min);
                }
                int max2 = Math.max(max, 0);
                int min2 = Math.min(min, editable.length());
                inputConnection.beginBatchEdit();
                editable.delete(max2, min2);
                inputConnection.endBatchEdit();
                return true;
            }
        }
        return false;
    }

    /* renamed from: d */
    public static boolean m4357d(Editable editable, int i, KeyEvent keyEvent) {
        if (i != 67 ? i != 112 ? false : m4359b(editable, keyEvent, true) : m4359b(editable, keyEvent, false)) {
            MetaKeyKeyListener.adjustMetaAfterKeypress(editable);
            return true;
        }
        return false;
    }

    /* renamed from: f */
    public static boolean m4355f(int i, int i2) {
        return i == -1 || i2 == -1 || i != i2;
    }

    /* renamed from: g */
    public static boolean m4354g(KeyEvent keyEvent) {
        return !KeyEvent.metaStateHasNoModifiers(keyEvent.getMetaState());
    }

    /* renamed from: a */
    public final void m4360a(Spannable spannable, C0241i c0241i, int i, int i2) {
        spannable.setSpan(this.f1105a.m4373a(c0241i), i, i2, 33);
    }

    /* renamed from: e */
    public final boolean m4356e(CharSequence charSequence, int i, int i2, C0241i c0241i) {
        if (c0241i.m4369d() == 0) {
            c0241i.m4362k(this.f1107c.mo4377a(charSequence, i, i2, c0241i.m4365h()));
        }
        return c0241i.m4369d() == 2;
    }

    /* JADX WARN: Code restructure failed: missing block: B:79:0x0122, code lost:
        ((androidx.emoji2.text.C0257q) r11).m4308d();
     */
    /* JADX WARN: Removed duplicated region for block: B:101:0x00d4 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:104:0x009f A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0046 A[Catch: all -> 0x0129, TryCatch #0 {all -> 0x0129, blocks: (B:7:0x000e, B:10:0x0013, B:12:0x0017, B:14:0x0024, B:19:0x0037, B:21:0x003f, B:23:0x0042, B:25:0x0046, B:27:0x0052, B:28:0x0055, B:30:0x0062, B:36:0x0071, B:37:0x007d, B:41:0x0098, B:49:0x00a8, B:52:0x00b4, B:53:0x00be, B:54:0x00c8, B:56:0x00cf, B:57:0x00d4, B:59:0x00df, B:61:0x00e6, B:65:0x00f0, B:68:0x00fc, B:69:0x0102, B:71:0x010b, B:16:0x002c), top: B:87:0x000e }] */
    /* JADX WARN: Removed duplicated region for block: B:68:0x00fc A[Catch: all -> 0x0129, TryCatch #0 {all -> 0x0129, blocks: (B:7:0x000e, B:10:0x0013, B:12:0x0017, B:14:0x0024, B:19:0x0037, B:21:0x003f, B:23:0x0042, B:25:0x0046, B:27:0x0052, B:28:0x0055, B:30:0x0062, B:36:0x0071, B:37:0x007d, B:41:0x0098, B:49:0x00a8, B:52:0x00b4, B:53:0x00be, B:54:0x00c8, B:56:0x00cf, B:57:0x00d4, B:59:0x00df, B:61:0x00e6, B:65:0x00f0, B:68:0x00fc, B:69:0x0102, B:71:0x010b, B:16:0x002c), top: B:87:0x000e }] */
    /* JADX WARN: Removed duplicated region for block: B:71:0x010b A[Catch: all -> 0x0129, TRY_LEAVE, TryCatch #0 {all -> 0x0129, blocks: (B:7:0x000e, B:10:0x0013, B:12:0x0017, B:14:0x0024, B:19:0x0037, B:21:0x003f, B:23:0x0042, B:25:0x0046, B:27:0x0052, B:28:0x0055, B:30:0x0062, B:36:0x0071, B:37:0x007d, B:41:0x0098, B:49:0x00a8, B:52:0x00b4, B:53:0x00be, B:54:0x00c8, B:56:0x00cf, B:57:0x00d4, B:59:0x00df, B:61:0x00e6, B:65:0x00f0, B:68:0x00fc, B:69:0x0102, B:71:0x010b, B:16:0x002c), top: B:87:0x000e }] */
    /* JADX WARN: Removed duplicated region for block: B:75:0x0117  */
    /* renamed from: h */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public CharSequence m4353h(CharSequence charSequence, int i, int i2, int i3, boolean z) {
        C0262u c0262u;
        C0245b c0245b;
        C0262u c0262u2;
        int codePointAt;
        int i4;
        int m4350a;
        AbstractC0246l[] abstractC0246lArr;
        boolean z2 = charSequence instanceof C0257q;
        if (z2) {
            ((C0257q) charSequence).m4311a();
        }
        if (!z2) {
            try {
                if (!(charSequence instanceof Spannable)) {
                    c0262u = (!(charSequence instanceof Spanned) || ((Spanned) charSequence).nextSpanTransition(i + (-1), i2 + 1, AbstractC0246l.class) > i2) ? null : new C0262u(charSequence);
                    if (c0262u != null && (abstractC0246lArr = (AbstractC0246l[]) c0262u.getSpans(i, i2, AbstractC0246l.class)) != null && abstractC0246lArr.length > 0) {
                        for (AbstractC0246l abstractC0246l : abstractC0246lArr) {
                            int spanStart = c0262u.getSpanStart(abstractC0246l);
                            int spanEnd = c0262u.getSpanEnd(abstractC0246l);
                            if (spanStart != i2) {
                                c0262u.removeSpan(abstractC0246l);
                            }
                            i = Math.min(spanStart, i);
                            i2 = Math.max(spanEnd, i2);
                        }
                    }
                    if (i != i2 && i < charSequence.length()) {
                        if (i3 != Integer.MAX_VALUE && c0262u != null) {
                            i3 -= ((AbstractC0246l[]) c0262u.getSpans(0, c0262u.length(), AbstractC0246l.class)).length;
                        }
                        c0245b = new C0245b(this.f1106b.m4317f(), this.f1108d, this.f1109e);
                        c0262u2 = c0262u;
                        codePointAt = Character.codePointAt(charSequence, i);
                        int i5 = 0;
                        loop1: while (true) {
                            i4 = i;
                            while (i < i2 && i5 < i3) {
                                m4350a = c0245b.m4350a(codePointAt);
                                if (m4350a != 1) {
                                    i4 += Character.charCount(Character.codePointAt(charSequence, i4));
                                    if (i4 < i2) {
                                        codePointAt = Character.codePointAt(charSequence, i4);
                                    }
                                    i = i4;
                                } else if (m4350a == 2) {
                                    i += Character.charCount(codePointAt);
                                    if (i < i2) {
                                        codePointAt = Character.codePointAt(charSequence, i);
                                    }
                                } else if (m4350a == 3) {
                                    if (z || !m4356e(charSequence, i4, i, c0245b.m4348c())) {
                                        if (c0262u2 == null) {
                                            c0262u2 = new C0262u((Spannable) new SpannableString(charSequence));
                                        }
                                        m4360a(c0262u2, c0245b.m4348c(), i4, i);
                                        i5++;
                                    }
                                }
                            }
                        }
                        if (c0245b.m4346e() && i5 < i3 && (z || !m4356e(charSequence, i4, i, c0245b.m4349b()))) {
                            if (c0262u2 == null) {
                                c0262u2 = new C0262u(charSequence);
                            }
                            m4360a(c0262u2, c0245b.m4349b(), i4, i);
                        }
                        if (c0262u2 == null) {
                            return c0262u2.m4295b();
                        }
                        if (z2) {
                            ((C0257q) charSequence).m4308d();
                        }
                        return charSequence;
                    }
                    return charSequence;
                }
            } finally {
                if (z2) {
                    ((C0257q) charSequence).m4308d();
                }
            }
        }
        c0262u = new C0262u((Spannable) charSequence);
        if (c0262u != null) {
            while (r6 < r5) {
            }
        }
        if (i != i2) {
            if (i3 != Integer.MAX_VALUE) {
                i3 -= ((AbstractC0246l[]) c0262u.getSpans(0, c0262u.length(), AbstractC0246l.class)).length;
            }
            c0245b = new C0245b(this.f1106b.m4317f(), this.f1108d, this.f1109e);
            c0262u2 = c0262u;
            codePointAt = Character.codePointAt(charSequence, i);
            int i52 = 0;
            loop1: while (true) {
                i4 = i;
                while (i < i2) {
                    m4350a = c0245b.m4350a(codePointAt);
                    if (m4350a != 1) {
                    }
                }
            }
            if (c0245b.m4346e()) {
                if (c0262u2 == null) {
                }
                m4360a(c0262u2, c0245b.m4349b(), i4, i);
            }
            if (c0262u2 == null) {
            }
        }
        return charSequence;
    }
}