package com.bytedance.sdk.component.wsy.ro;

import android.text.TextUtils;
import androidx.media3.common.PlaybackException;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.bytedance.sdk.component.ro.fm.ajl;
import com.bytedance.sdk.component.ro.fm.dsz;
import com.bytedance.sdk.component.ro.fm.duv;
import com.bytedance.sdk.component.ro.fm.ef;
import com.bytedance.sdk.component.ro.fm.fhx;
import com.bytedance.sdk.component.ro.fm.ku;
import com.bytedance.sdk.component.ro.fm.onz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.zip.GZIPOutputStream;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class yz extends lb {
    dsz fm;

    public yz(ku kuVar) {
        super(kuVar);
        this.fm = null;
    }

    /* JADX WARN: Can't wrap try/catch for region: R(7:7|(4:53|8|51|9)|(4:55|10|45|11)|12|49|13|33) */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private byte[] ajl(String str) throws Throwable {
        ByteArrayOutputStream byteArrayOutputStream;
        GZIPOutputStream gZIPOutputStream;
        GZIPOutputStream gZIPOutputStream2 = null;
        if (str == null || str.length() == 0) {
            return null;
        }
        byte[] byteArray = new byte[0];
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            } catch (IOException unused) {
            } catch (Throwable th) {
                th = th;
            }
        } catch (IOException unused2) {
            byteArrayOutputStream = null;
        } catch (Throwable th2) {
            th = th2;
            byteArrayOutputStream = null;
        }
        try {
            gZIPOutputStream.write(str.getBytes("utf-8"));
            try {
                gZIPOutputStream.close();
            } catch (IOException unused3) {
            }
        } catch (IOException unused4) {
            gZIPOutputStream2 = gZIPOutputStream;
            if (gZIPOutputStream2 != null) {
                try {
                    gZIPOutputStream2.close();
                } catch (IOException unused5) {
                }
            }
            if (byteArrayOutputStream != null) {
            }
            return byteArray;
        } catch (Throwable th3) {
            th = th3;
            gZIPOutputStream2 = gZIPOutputStream;
            if (gZIPOutputStream2 != null) {
                try {
                    gZIPOutputStream2.close();
                } catch (IOException unused6) {
                }
            }
            if (byteArrayOutputStream == null) {
                throw th;
            }
            byteArrayOutputStream.toByteArray();
            try {
                byteArrayOutputStream.close();
                throw th;
            } catch (IOException unused7) {
                throw th;
            }
        }
        byteArray = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return byteArray;
    }

    public void jnr(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "{}";
        }
        this.fm = dsz.fm(ef.fm("application/json; charset=utf-8"), str);
    }

    public void fm(JSONObject jSONObject) {
        this.fm = dsz.fm(ef.fm("application/json; charset=utf-8"), jSONObject != null ? jSONObject.toString() : "{}");
    }

    public void fm(String str, byte[] bArr) {
        this.fm = dsz.fm(ef.fm(str), bArr);
    }

    @Override // com.bytedance.sdk.component.wsy.ro.lb
    public void fm(final com.bytedance.sdk.component.wsy.fm.fm fmVar) {
        try {
            duv.fm fmVar2 = new duv.fm();
            if (TextUtils.isEmpty(this.wu)) {
                fmVar.fm(this, new IOException("Url is Empty"));
                return;
            }
            if (!TextUtils.isEmpty(this.jnr)) {
                fmVar2.fm(this.jnr);
            }
            int i10 = this.ajl;
            if (i10 > 0) {
                fmVar2.fm(i10);
            }
            fmVar2.ro(this.wu);
            if (this.fm == null) {
                if (fmVar != null) {
                    fmVar.fm(this, new IOException("RequestBody is null, content type is not support!!"));
                }
            } else {
                ro(fmVar2);
                fmVar2.fm((Object) yz());
                fm(fmVar2);
                this.f6701lb.fm(fmVar2.fm(this.fm).ro()).fm(new com.bytedance.sdk.component.ro.fm.lb() { // from class: com.bytedance.sdk.component.wsy.ro.yz.1
                    @Override // com.bytedance.sdk.component.ro.fm.lb
                    public void fm(com.bytedance.sdk.component.ro.fm.ro roVar, IOException iOException) {
                        com.bytedance.sdk.component.wsy.fm.fm fmVar3 = fmVar;
                        if (fmVar3 != null) {
                            fmVar3.fm(yz.this, iOException);
                        }
                    }

                    /* JADX WARN: Type inference fix 'apply assigned field type' failed
                    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
                    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
                    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
                    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
                    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
                    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
                    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
                    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
                     */
                    @Override // com.bytedance.sdk.component.ro.fm.lb
                    public void fm(com.bytedance.sdk.component.ro.fm.ro roVar, onz onzVar) throws IOException {
                        com.bytedance.sdk.component.wsy.ro roVar2;
                        com.bytedance.sdk.component.wsy.fm.fm fmVar3 = fmVar;
                        if (fmVar3 != null) {
                            if (onzVar == null) {
                                fmVar3.fm(yz.this, new IOException("No response"));
                                return;
                            }
                            yz.this.fm(onzVar.ef());
                            com.bytedance.sdk.component.wsy.ro roVar3 = null;
                            IOException iOException = null;
                            try {
                                HashMap map = new HashMap();
                                ajl ajlVarWsy = onzVar.wsy();
                                if (ajlVarWsy != null) {
                                    for (int i11 = 0; i11 < ajlVarWsy.fm(); i11++) {
                                        String strFm = ajlVarWsy.fm(i11);
                                        String strRo = ajlVarWsy.ro(i11);
                                        map.put(strFm, strRo);
                                        if (strFm != null && strFm.equalsIgnoreCase("content-type")) {
                                            map.put("content-type", strRo == null ? "" : strRo.toLowerCase());
                                        }
                                    }
                                }
                                fhx fhxVarAjl = onzVar.ajl();
                                if (com.bytedance.sdk.component.wsy.lb.fm.fm(map)) {
                                    byte[] bArrYz = fhxVarAjl.yz();
                                    roVar2 = new com.bytedance.sdk.component.wsy.ro(onzVar.yz(), onzVar.lb(), onzVar.jnr(), map, null, onzVar.ro(), onzVar.fm());
                                    try {
                                        roVar2.fm(bArrYz);
                                    } catch (Throwable th) {
                                        th = th;
                                        roVar3 = roVar2;
                                        roVar2 = roVar3;
                                        iOException = new IOException(th);
                                    }
                                } else if (yz.this.vt) {
                                    byte[] bArrYz2 = fhxVarAjl.yz();
                                    yz yzVar = yz.this;
                                    com.bytedance.sdk.component.wsy.ro roVar4 = new com.bytedance.sdk.component.wsy.ro(onzVar.yz(), onzVar.lb(), onzVar.jnr(), map, new String(bArrYz2, yzVar.fm(yzVar.fm(fhxVarAjl))), onzVar.ro(), onzVar.fm());
                                    try {
                                        roVar4.fm(bArrYz2);
                                        roVar2 = roVar4;
                                    } catch (Throwable th2) {
                                        th = th2;
                                        roVar3 = roVar4;
                                        roVar2 = roVar3;
                                        iOException = new IOException(th);
                                    }
                                } else {
                                    if (fhxVarAjl == null) {
                                        throw new IOException(onzVar.jnr());
                                    }
                                    roVar2 = new com.bytedance.sdk.component.wsy.ro(onzVar.yz(), onzVar.lb(), onzVar.jnr(), map, fhxVarAjl.ro(), onzVar.ro(), onzVar.fm());
                                }
                                yz.this.fm(roVar2, onzVar);
                            } catch (Throwable th3) {
                                th = th3;
                            }
                            if (roVar2 != null) {
                                fmVar.fm(yz.this, roVar2);
                                return;
                            }
                            com.bytedance.sdk.component.wsy.fm.fm fmVar4 = fmVar;
                            if (!(fmVar4 instanceof com.bytedance.sdk.component.wsy.fm.ro)) {
                                yz yzVar2 = yz.this;
                                if (iOException == null) {
                                    iOException = new IOException("Unexpected exception");
                                }
                                fmVar4.fm(yzVar2, iOException);
                                return;
                            }
                            com.bytedance.sdk.component.wsy.fm.ro roVar5 = (com.bytedance.sdk.component.wsy.fm.ro) fmVar4;
                            yz yzVar3 = yz.this;
                            if (iOException == null) {
                                iOException = new IOException("Unexpected exception");
                            }
                            roVar5.fm(yzVar3, iOException, new com.bytedance.sdk.component.wsy.ro(onzVar.yz(), onzVar.lb(), onzVar.jnr(), null, null, onzVar.ro(), onzVar.fm()));
                        }
                    }
                });
            }
        } catch (Throwable th) {
            fmVar.fm(this, new IOException(th.getMessage()));
        }
    }

    @Override // com.bytedance.sdk.component.wsy.ro.lb
    public com.bytedance.sdk.component.wsy.ro fm() {
        com.bytedance.sdk.component.wsy.ro roVar;
        try {
            duv.fm fmVar = new duv.fm();
            if (TextUtils.isEmpty(this.wu)) {
                return new com.bytedance.sdk.component.wsy.ro(false, 5000, "URL_NULL_MSG", null, "URL_NULL_BODY", 1L, 1L);
            }
            fmVar.ro(this.wu);
            if (this.fm == null) {
                return new com.bytedance.sdk.component.wsy.ro(false, 5000, "BODY_NULL_MSG", null, "BODY_NULL_BODY", 1L, 1L);
            }
            ro(fmVar);
            fmVar.fm((Object) yz());
            fm(fmVar);
            onz onzVarRo = this.f6701lb.fm(fmVar.fm(this.fm).ro()).ro();
            if (onzVarRo == null) {
                return null;
            }
            fm(onzVarRo.ef());
            HashMap map = new HashMap();
            ajl ajlVarWsy = onzVarRo.wsy();
            if (ajlVarWsy != null) {
                for (int i10 = 0; i10 < ajlVarWsy.fm(); i10++) {
                    String strFm = ajlVarWsy.fm(i10);
                    String strRo = ajlVarWsy.ro(i10);
                    map.put(strFm, strRo);
                    if (strFm != null && strFm.equalsIgnoreCase("content-type")) {
                        map.put("content-type", strRo == null ? "" : strRo.toLowerCase());
                    }
                }
            }
            fhx fhxVarAjl = onzVarRo.ajl();
            if (com.bytedance.sdk.component.wsy.lb.fm.fm(map)) {
                byte[] bArrYz = fhxVarAjl.yz();
                roVar = new com.bytedance.sdk.component.wsy.ro(onzVarRo.yz(), onzVarRo.lb(), onzVarRo.jnr(), map, null, onzVarRo.ro(), onzVarRo.fm());
                roVar.fm(bArrYz);
            } else if (this.vt) {
                byte[] bArrYz2 = fhxVarAjl.yz();
                com.bytedance.sdk.component.wsy.ro roVar2 = new com.bytedance.sdk.component.wsy.ro(onzVarRo.yz(), onzVarRo.lb(), onzVarRo.jnr(), map, new String(bArrYz2, fm(fm(fhxVarAjl))), onzVarRo.ro(), onzVarRo.fm());
                roVar2.fm(bArrYz2);
                roVar = roVar2;
            } else if (fhxVarAjl != null) {
                roVar = new com.bytedance.sdk.component.wsy.ro(onzVarRo.yz(), onzVarRo.lb(), onzVarRo.jnr(), map, fhxVarAjl.ro(), onzVarRo.ro(), onzVarRo.fm());
            } else {
                throw new IOException(onzVarRo.jnr());
            }
            fm(roVar, onzVarRo);
            return roVar;
        } catch (Throwable th) {
            return new com.bytedance.sdk.component.wsy.ro(false, PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED, th.getMessage(), null, "BODY_NULL_BODY", 1L, 1L);
        }
    }

    public void fm(String str, boolean z10) {
        if (z10) {
            fm("application/json; charset=utf-8", ajl(str));
            ro(RtspHeaders.CONTENT_ENCODING, "gzip");
        } else {
            jnr(str);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public Charset fm(ef efVar) {
        try {
            return efVar != null ? efVar.fm(com.bytedance.sdk.component.ro.fm.ro.ef.fm) : com.bytedance.sdk.component.ro.fm.ro.ef.fm;
        } catch (Exception unused) {
            return com.bytedance.sdk.component.ro.fm.ro.ef.fm;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public ef fm(fhx fhxVar) {
        try {
            return fhxVar.jnr();
        } catch (Exception unused) {
            return null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(com.bytedance.sdk.component.wsy.ro roVar, onz onzVar) {
        if (roVar == null || onzVar == null) {
            return;
        }
        roVar.fm(onzVar.wu());
    }
}
