package com.bytedance.adsdk.ro.yz;

import android.content.Context;
import android.util.Pair;
import com.bytedance.adsdk.ro.duv;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipInputStream;

/* JADX INFO: loaded from: classes.dex */
public class wu {
    private final wsy fm;
    private final ajl ro;

    public wu(wsy wsyVar, ajl ajlVar) {
        this.fm = wsyVar;
        this.ro = ajlVar;
    }

    private duv<com.bytedance.adsdk.ro.wsy> lb(Context context, String str, String str2) {
        Closeable closeable = null;
        try {
            try {
                yz yzVarFm = this.ro.fm(str);
                if (!yzVarFm.fm()) {
                    duv<com.bytedance.adsdk.ro.wsy> duvVar = new duv<>(new IllegalArgumentException(yzVarFm.yz()));
                    try {
                        yzVarFm.close();
                    } catch (IOException unused) {
                    }
                    return duvVar;
                }
                duv<com.bytedance.adsdk.ro.wsy> duvVarFm = fm(context, str, yzVarFm.ro(), yzVarFm.lb(), str2);
                duvVarFm.fm();
                try {
                    yzVarFm.close();
                } catch (IOException unused2) {
                }
                return duvVarFm;
            } catch (Throwable th) {
                if (0 != 0) {
                    try {
                        closeable.close();
                    } catch (IOException unused3) {
                    }
                }
                throw th;
            }
        } catch (Exception e10) {
            duv<com.bytedance.adsdk.ro.wsy> duvVar2 = new duv<>(e10);
            if (0 != 0) {
                try {
                    closeable.close();
                } catch (IOException unused4) {
                }
            }
            return duvVar2;
        }
    }

    private com.bytedance.adsdk.ro.wsy ro(Context context, String str, String str2) {
        wsy wsyVar;
        Pair<lb, InputStream> pairFm;
        if (str2 == null || (wsyVar = this.fm) == null || (pairFm = wsyVar.fm(str)) == null) {
            return null;
        }
        lb lbVar = (lb) pairFm.first;
        InputStream inputStream = (InputStream) pairFm.second;
        duv<com.bytedance.adsdk.ro.wsy> duvVarFm = lbVar == lb.ZIP ? com.bytedance.adsdk.ro.wu.fm(context, new ZipInputStream(inputStream), str2) : com.bytedance.adsdk.ro.wu.ro(inputStream, str2);
        if (duvVarFm.fm() != null) {
            return duvVarFm.fm();
        }
        return null;
    }

    public duv<com.bytedance.adsdk.ro.wsy> fm(Context context, String str, String str2) {
        com.bytedance.adsdk.ro.wsy wsyVarRo = ro(context, str, str2);
        return wsyVarRo != null ? new duv<>(wsyVarRo) : lb(context, str, str2);
    }

    private duv<com.bytedance.adsdk.ro.wsy> fm(Context context, String str, InputStream inputStream, String str2, String str3) throws IOException {
        duv<com.bytedance.adsdk.ro.wsy> duvVarFm;
        lb lbVar;
        wsy wsyVar;
        if (str2 == null) {
            str2 = "application/json";
        }
        if (!str2.contains("application/zip") && !str2.contains("application/x-zip") && !str2.contains("application/x-zip-compressed") && !str.split("\\?")[0].endsWith(".lottie")) {
            lbVar = lb.JSON;
            duvVarFm = fm(str, inputStream, str3);
        } else {
            lb lbVar2 = lb.ZIP;
            duvVarFm = fm(context, str, inputStream, str3);
            lbVar = lbVar2;
        }
        if (str3 != null && duvVarFm.fm() != null && (wsyVar = this.fm) != null) {
            wsyVar.fm(str, lbVar);
        }
        return duvVarFm;
    }

    private duv<com.bytedance.adsdk.ro.wsy> fm(Context context, String str, InputStream inputStream, String str2) throws IOException {
        wsy wsyVar;
        if (str2 != null && (wsyVar = this.fm) != null) {
            return com.bytedance.adsdk.ro.wu.fm(context, new ZipInputStream(new FileInputStream(wsyVar.fm(str, inputStream, lb.ZIP))), str);
        }
        return com.bytedance.adsdk.ro.wu.fm(context, new ZipInputStream(inputStream), (String) null);
    }

    private duv<com.bytedance.adsdk.ro.wsy> fm(String str, InputStream inputStream, String str2) throws IOException {
        wsy wsyVar;
        if (str2 != null && (wsyVar = this.fm) != null) {
            return com.bytedance.adsdk.ro.wu.ro(new FileInputStream(wsyVar.fm(str, inputStream, lb.JSON).getAbsolutePath()), str);
        }
        return com.bytedance.adsdk.ro.wu.ro(inputStream, (String) null);
    }
}
