package io.flutter.plugin.editing;

import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import dd.a0;
import dd.i0;
import dd.j0;
import ed.t;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* JADX INFO: loaded from: classes.dex */
public final class i implements i0, SpellCheckerSession.SpellCheckerSessionListener {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public SpellCheckerSession f28153c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public t f28154d;

    public i(TextServicesManager textServicesManager, j0 j0Var) {
        this.f28152b = textServicesManager;
        this.f28151a = j0Var;
        j0Var.setSpellCheckMethodHandler(this);
    }

    public final void a(a0 a0Var, String str, String str2) {
        if (this.f28154d != null) {
            a0Var.error(IjkMediaPlayer.OnNativeInvokeListener.ARG_ERROR, "Previous spell check request still pending.", null);
            return;
        }
        this.f28154d = a0Var;
        Locale localeA = fd.b.a(str);
        if (this.f28153c == null) {
            this.f28153c = this.f28152b.newSpellCheckerSession(null, localeA, this, true);
        }
        this.f28153c.getSentenceSuggestions(new TextInfo[]{new TextInfo(str2)}, 5);
    }

    @Override // android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener
    public final void onGetSentenceSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfoArr) {
        if (sentenceSuggestionsInfoArr.length == 0) {
            this.f28154d.success(new ArrayList());
            this.f28154d = null;
            return;
        }
        ArrayList arrayList = new ArrayList();
        SentenceSuggestionsInfo sentenceSuggestionsInfo = sentenceSuggestionsInfoArr[0];
        if (sentenceSuggestionsInfo == null) {
            this.f28154d.success(new ArrayList());
            this.f28154d = null;
            return;
        }
        for (int i10 = 0; i10 < sentenceSuggestionsInfo.getSuggestionsCount(); i10++) {
            SuggestionsInfo suggestionsInfoAt = sentenceSuggestionsInfo.getSuggestionsInfoAt(i10);
            int suggestionsCount = suggestionsInfoAt.getSuggestionsCount();
            if (suggestionsCount > 0) {
                HashMap map = new HashMap();
                int offsetAt = sentenceSuggestionsInfo.getOffsetAt(i10);
                int lengthAt = sentenceSuggestionsInfo.getLengthAt(i10) + offsetAt;
                map.put("startIndex", Integer.valueOf(offsetAt));
                map.put("endIndex", Integer.valueOf(lengthAt));
                ArrayList arrayList2 = new ArrayList();
                boolean z10 = false;
                for (int i11 = 0; i11 < suggestionsCount; i11++) {
                    String suggestionAt = suggestionsInfoAt.getSuggestionAt(i11);
                    if (!suggestionAt.equals("")) {
                        arrayList2.add(suggestionAt);
                        z10 = true;
                    }
                }
                if (z10) {
                    map.put("suggestions", arrayList2);
                    arrayList.add(map);
                }
            }
        }
        this.f28154d.success(arrayList);
        this.f28154d = null;
    }

    @Override // android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener
    public final void onGetSuggestions(SuggestionsInfo[] suggestionsInfoArr) {
    }
}
