package com.strmr.ps.data.remote;

import android.util.Log;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.strmr.ps.data.model.ContentItem;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.ik2;
import defpackage.n41;
import defpackage.n89;
import defpackage.r69;
import defpackage.s38;
import defpackage.s69;
import defpackage.vx8;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0003\u001a\b\u0012\u0004\u0012\u00020\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0003\u0010\u0004"}, d2 = {"Lak1;", "", "Lcom/strmr/ps/data/model/ContentItem;", "<anonymous>", "(Lak1;)Ljava/util/List;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.remote.AddonSearchClient$search$2", f = "AddonSearchClient.kt", i = {}, l = {}, m = "invokeSuspend", n = {}, s = {})
@vx8
final class c extends SuspendLambda implements gr3<ak1, Continuation<? super List<? extends ContentItem>>, Object> {
    public final /* synthetic */ String a;
    public final /* synthetic */ String b;
    public final /* synthetic */ String c;
    public final /* synthetic */ String d;
    public final /* synthetic */ AddonSearchClient e;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public c(String str, String str2, String str3, String str4, AddonSearchClient addonSearchClient, Continuation continuation) {
        super(2, continuation);
        this.a = str;
        this.b = str2;
        this.c = str3;
        this.d = str4;
        this.e = addonSearchClient;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation create(Object obj, Continuation continuation) {
        return new c(this.a, this.b, this.c, this.d, this.e, continuation);
    }

    @Override // defpackage.gr3
    public final Object invoke(Object obj, Object obj2) {
        return ((c) create((ak1) obj, (Continuation) obj2)).invokeSuspend(e6a.a);
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Object invokeSuspend(Object obj) throws UnsupportedEncodingException {
        List metas;
        AddonSearchClient addonSearchClient = this.e;
        ik2 ik2Var = ik2.a;
        ck1 ck1Var = ck1.a;
        s38.b(obj);
        String strEncode = URLEncoder.encode(this.a, "UTF-8");
        StringBuilder sb = new StringBuilder();
        String str = this.b;
        sb.append(str);
        sb.append("/catalog/");
        sb.append(this.c);
        sb.append(DomExceptionUtils.SEPARATOR);
        sb.append(this.d);
        String strT = n41.t(sb, "/search=", strEncode, ".json");
        try {
            OkHttpClient.Builder builderNewBuilder = addonSearchClient.a.newBuilder();
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            Response responseExecute = builderNewBuilder.callTimeout(15000L, timeUnit).connectTimeout(15000L, timeUnit).readTimeout(15000L, timeUnit).build().newCall(new Request.Builder().url(strT).header(RtspHeaders.USER_AGENT, "Strmr/1.0").build()).execute();
            try {
                if (!responseExecute.isSuccessful()) {
                    Log.e("AddonSearchClient", "HTTP " + responseExecute.code() + " from " + strT);
                    responseExecute.close();
                    return ik2Var;
                }
                ResponseBody responseBodyBody = responseExecute.body();
                String strString = responseBodyBody != null ? responseBodyBody.string() : null;
                if (strString != null && !n89.ab(strString)) {
                    s69 s69Var = (s69) addonSearchClient.b.fromJson(strString, s69.class);
                    if (s69Var == null || (metas = s69Var.getMetas()) == null) {
                        metas = ik2Var;
                    }
                    metas.size();
                    ArrayList arrayList = new ArrayList();
                    Iterator it = metas.iterator();
                    while (it.hasNext()) {
                        ContentItem contentItemA = AddonSearchClient.a(addonSearchClient, (r69) it.next(), str);
                        if (contentItemA != null) {
                            arrayList.add(contentItemA);
                        }
                    }
                    responseExecute.close();
                    return arrayList;
                }
                Log.e("AddonSearchClient", "Empty response from " + strT);
                responseExecute.close();
                return ik2Var;
            } finally {
            }
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            Log.e("AddonSearchClient", "Error searching addon: " + e2.getMessage(), e2);
            return ik2Var;
        }
    }
}
