package k4;

import android.text.InputFilter;
import android.text.Selection;
import android.text.Spannable;
import android.text.Spanned;
import android.widget.TextView;
import androidx.emoji2.text.e;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/* loaded from: classes.dex */
final class d implements InputFilter {

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

    /* renamed from: b, reason: collision with root package name */
    private e.f f25366b;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class a extends e.f {

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

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

        a(TextView textView, d dVar) {
            this.f25367a = new WeakReference(textView);
            this.f25368b = new WeakReference(dVar);
        }

        private boolean c(TextView textView, InputFilter inputFilter) {
            InputFilter[] filters;
            if (inputFilter == null || textView == null || (filters = textView.getFilters()) == null) {
                return false;
            }
            for (InputFilter inputFilter2 : filters) {
                if (inputFilter2 == inputFilter) {
                    return true;
                }
            }
            return false;
        }

        @Override // androidx.emoji2.text.e.f
        public void b() {
            CharSequence text;
            CharSequence r10;
            super.b();
            TextView textView = (TextView) this.f25367a.get();
            if (c(textView, (InputFilter) this.f25368b.get()) && textView.isAttachedToWindow() && text != (r10 = androidx.emoji2.text.e.c().r((text = textView.getText())))) {
                int selectionStart = Selection.getSelectionStart(r10);
                int selectionEnd = Selection.getSelectionEnd(r10);
                textView.setText(r10);
                if (r10 instanceof Spannable) {
                    d.b((Spannable) r10, selectionStart, selectionEnd);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public d(TextView textView) {
        this.f25365a = textView;
    }

    private e.f a() {
        if (this.f25366b == null) {
            this.f25366b = new a(this.f25365a, this);
        }
        return this.f25366b;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static void b(Spannable spannable, int i10, int i11) {
        if (i10 >= 0 && i11 >= 0) {
            Selection.setSelection(spannable, i10, i11);
        } else if (i10 >= 0) {
            Selection.setSelection(spannable, i10);
        } else if (i11 >= 0) {
            Selection.setSelection(spannable, i11);
        }
    }

    @Override // android.text.InputFilter
    public CharSequence filter(CharSequence charSequence, int i10, int i11, Spanned spanned, int i12, int i13) {
        if (this.f25365a.isInEditMode()) {
            return charSequence;
        }
        int g10 = androidx.emoji2.text.e.c().g();
        if (g10 != 0) {
            boolean z10 = true;
            if (g10 == 1) {
                if (i13 == 0 && i12 == 0 && spanned.length() == 0 && charSequence == this.f25365a.getText()) {
                    z10 = false;
                }
                if (!z10 || charSequence == null) {
                    return charSequence;
                }
                if (i10 != 0 || i11 != charSequence.length()) {
                    charSequence = charSequence.subSequence(i10, i11);
                }
                return androidx.emoji2.text.e.c().s(charSequence, 0, charSequence.length());
            }
            if (g10 != 3) {
                return charSequence;
            }
        }
        androidx.emoji2.text.e.c().v(a());
        return charSequence;
    }
}
