package T1;

import C0.J;
import I2.C0318b;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Spanned;
import android.view.inputmethod.EditorInfo;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import n.C2579f;

/* JADX INFO: loaded from: classes.dex */
public final class h {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Object f6583j = new Object();

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static volatile h f6584k;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ReentrantReadWriteLock f6585a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C2579f f6586b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile int f6587c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Handler f6588d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final C0318b f6589e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g f6590f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final u4.c f6591g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f6592h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final d f6593i;

    public h(p pVar) {
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        this.f6585a = reentrantReadWriteLock;
        this.f6587c = 3;
        g gVar = (g) pVar.f6581b;
        this.f6590f = gVar;
        int i6 = pVar.f6580a;
        this.f6592h = i6;
        this.f6593i = (d) pVar.f6582c;
        this.f6588d = new Handler(Looper.getMainLooper());
        this.f6586b = new C2579f(0);
        this.f6591g = new u4.c(12);
        C0318b c0318b = new C0318b(this);
        this.f6589e = c0318b;
        reentrantReadWriteLock.writeLock().lock();
        if (i6 == 0) {
            try {
                this.f6587c = 0;
            } catch (Throwable th) {
                this.f6585a.writeLock().unlock();
                throw th;
            }
        }
        reentrantReadWriteLock.writeLock().unlock();
        if (c() == 0) {
            try {
                gVar.a(new e(c0318b));
            } catch (Throwable th2) {
                f(th2);
            }
        }
    }

    public static h a() {
        h hVar;
        synchronized (f6583j) {
            try {
                hVar = f6584k;
                if (!(hVar != null)) {
                    throw new IllegalStateException("EmojiCompat is not initialized.\n\nYou must initialize EmojiCompat prior to referencing the EmojiCompat instance.\n\nThe most likely cause of this error is disabling the EmojiCompatInitializer\neither explicitly in AndroidManifest.xml, or by including\nandroidx.emoji2:emoji2-bundled.\n\nAutomatic initialization is typically performed by EmojiCompatInitializer. If\nyou are not expecting to initialize EmojiCompat manually in your application,\nplease check to ensure it has not been removed from your APK's manifest. You can\ndo this in Android Studio using Build > Analyze APK.\n\nIn the APK Analyzer, ensure that the startup entry for\nEmojiCompatInitializer and InitializationProvider is present in\n AndroidManifest.xml. If it is missing or contains tools:node=\"remove\", and you\nintend to use automatic configuration, verify:\n\n  1. Your application does not include emoji2-bundled\n  2. All modules do not contain an exclusion manifest rule for\n     EmojiCompatInitializer or InitializationProvider. For more information\n     about manifest exclusions see the documentation for the androidx startup\n     library.\n\nIf you intend to use emoji2-bundled, please call EmojiCompat.init. You can\nlearn more in the documentation for BundledEmojiCompatConfig.\n\nIf you intended to perform manual configuration, it is recommended that you call\nEmojiCompat.init immediately on application startup.\n\nIf you still cannot resolve this issue, please open a bug with your specific\nconfiguration to help improve error message.");
                }
            } finally {
            }
        }
        return hVar;
    }

    public static boolean d() {
        return f6584k != null;
    }

    public final int b(CharSequence charSequence, int i6) {
        if (!(c() == 1)) {
            throw new IllegalStateException("Not initialized yet");
        }
        A4.g.n(charSequence, "charSequence cannot be null");
        C4.f fVar = (C4.f) this.f6589e.f2582b;
        fVar.getClass();
        if (i6 < 0 || i6 >= charSequence.length()) {
            return -1;
        }
        if (charSequence instanceof Spanned) {
            Spanned spanned = (Spanned) charSequence;
            s[] sVarArr = (s[]) spanned.getSpans(i6, i6 + 1, s.class);
            if (sVarArr.length > 0) {
                return spanned.getSpanStart(sVarArr[0]);
            }
        }
        return ((m) fVar.K(charSequence, Math.max(0, i6 - 16), Math.min(charSequence.length(), i6 + 16), Integer.MAX_VALUE, true, new m(i6))).f6598b;
    }

    public final int c() {
        this.f6585a.readLock().lock();
        try {
            return this.f6587c;
        } finally {
            this.f6585a.readLock().unlock();
        }
    }

    public final void e() {
        if (!(this.f6592h == 1)) {
            throw new IllegalStateException("Set metadataLoadStrategy to LOAD_STRATEGY_MANUAL to execute manual loading");
        }
        if (c() == 1) {
            return;
        }
        this.f6585a.writeLock().lock();
        try {
            if (this.f6587c == 0) {
                return;
            }
            this.f6587c = 0;
            this.f6585a.writeLock().unlock();
            C0318b c0318b = this.f6589e;
            h hVar = (h) c0318b.f2581a;
            try {
                hVar.f6590f.a(new e(c0318b));
            } catch (Throwable th) {
                hVar.f(th);
            }
        } finally {
            this.f6585a.writeLock().unlock();
        }
    }

    public final void f(Throwable th) {
        ArrayList arrayList = new ArrayList();
        this.f6585a.writeLock().lock();
        try {
            this.f6587c = 2;
            arrayList.addAll(this.f6586b);
            this.f6586b.clear();
            this.f6585a.writeLock().unlock();
            this.f6588d.post(new C1.b(arrayList, this.f6587c, th));
        } catch (Throwable th2) {
            this.f6585a.writeLock().unlock();
            throw th2;
        }
    }

    public final void g(EditorInfo editorInfo) {
        if (c() != 1 || editorInfo == null) {
            return;
        }
        if (editorInfo.extras == null) {
            editorInfo.extras = new Bundle();
        }
        C0318b c0318b = this.f6589e;
        c0318b.getClass();
        Bundle bundle = editorInfo.extras;
        U1.b bVar = (U1.b) ((J) c0318b.f2583c).f533b;
        int iA = bVar.a(4);
        bundle.putInt("android.support.text.emoji.emojiCompat_metadataVersion", iA != 0 ? ((ByteBuffer) bVar.f6864d).getInt(iA + bVar.f6861a) : 0);
        Bundle bundle2 = editorInfo.extras;
        ((h) c0318b.f2581a).getClass();
        bundle2.putBoolean("android.support.text.emoji.emojiCompat_replaceAll", false);
    }
}
