package P2;

import J4.f;
import M2.C0556x0;
import N2.v;
import S4.m;
import S4.n;
import S4.p;
import V4.E;
import androidx.media3.common.PlaybackException;
import com.revenuecat.purchases.common.networking.RCHTTPStatusCodes;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import kotlin.jvm.internal.o;
import q.AbstractC2761L;
import w4.k;
import x4.AbstractC3253s;

/* JADX INFO: loaded from: classes.dex */
public abstract class c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final m f5650a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final m f5651b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final m f5652c;

    static {
        n[] nVarArr = n.f6290a;
        f5650a = new m("\\.(mkv|mp4)$", 0);
        f5651b = new m("(^|[^a-z0-9])sample([^a-z0-9]|$)", 0);
        f5652c = new m("\\.(mkv|mp4|avi)$", 0);
    }

    public static String a(String str) {
        String lowerCase = str.toLowerCase(Locale.ROOT);
        o.g(lowerCase, "toLowerCase(...)");
        Pattern patternCompile = Pattern.compile("[^a-z0-9]+");
        o.g(patternCompile, "compile(...)");
        String strReplaceAll = patternCompile.matcher(lowerCase).replaceAll("");
        o.g(strReplaceAll, "replaceAll(...)");
        return strReplaceAll;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0023  */
    /* JADX WARN: Removed duplicated region for block: B:33:0x0070  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0072  */
    /* JADX WARN: Removed duplicated region for block: B:38:0x0087  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00b8  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Object b(List list, int i6, int i7, String str, J4.c cVar) {
        String string;
        String strA;
        Integer num;
        if (str == null || (string = p.o1(str).toString()) == null || string.length() <= 0) {
            string = null;
        }
        if (string != null) {
            strA = a(string);
            if (strA.length() < 6) {
                strA = null;
            }
        }
        Object obj = null;
        int i8 = -1;
        for (Object obj2 : list) {
            String str2 = (String) cVar.invoke(obj2);
            if (f(str2)) {
                if (!(str2 != null ? f5651b.a(str2) : false)) {
                    k kVarC = e.c(str2);
                    int iMax = PlaybackException.CUSTOM_ERROR_CODE_BASE;
                    if (kVarC != null) {
                        int iIntValue = ((Number) kVarC.f25469a).intValue();
                        int iIntValue2 = ((Number) kVarC.f25470b).intValue();
                        if (iIntValue != i6 || iIntValue2 != i7) {
                            if (strA == null) {
                                iMax = -1;
                            } else if (strA != null) {
                                String strA2 = a(str2);
                                int iMax2 = strA2.length() > 0 ? Math.max(-1, d(strA, strA2)) : -1;
                                if (iMax2 > 0) {
                                    iMax = iMax2;
                                } else if (kVarC == null) {
                                    try {
                                        d dVarB = e.b(str2);
                                        Integer num2 = dVarB.f5654b;
                                        boolean z6 = num2 != null && num2.intValue() == i6 && (num = dVarB.f5655c) != null && num.intValue() == i7;
                                        if (strA != null) {
                                            String strA3 = a(str2);
                                            int iD = strA3.length() == 0 ? 0 : d(strA, strA3);
                                            iMax = Math.max(z6 ? 1000000 : 0, iD);
                                        }
                                    } catch (Throwable unused) {
                                        iMax = -1;
                                    }
                                }
                            }
                        }
                        if (iMax > i8) {
                            obj = obj2;
                            i8 = iMax;
                        }
                    }
                }
            }
        }
        if (i8 > 0) {
            return obj;
        }
        return null;
    }

    public static String c(String... strArr) {
        String str;
        int length = strArr.length;
        int i6 = 0;
        while (true) {
            if (i6 >= length) {
                str = null;
                break;
            }
            str = strArr[i6];
            if (e(str)) {
                break;
            }
            i6++;
        }
        if (str != null) {
            return p.o1(str).toString();
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x0092 A[RETURN] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static int d(String str, String str2) {
        int length;
        if (str.length() >= 6 && str2.length() != 0) {
            if (p.D0(str2, str, false)) {
                return str.length() + 1000;
            }
            if (p.D0(str, str2, false) && str2.length() >= 6) {
                return str2.length() + 800;
            }
            if (str.length() < 6 || str2.length() == 0) {
                length = 0;
                if (length < 400) {
                    return length;
                }
            } else {
                if (p.D0(str2, str, false)) {
                    length = str.length() + 1000;
                } else if (!p.D0(str, str2, false) || str2.length() < 6) {
                    int length2 = str.length();
                    if (length2 > 96) {
                        length2 = 96;
                    }
                    loop0: while (5 < length2) {
                        int length3 = str.length() - length2;
                        if (length3 >= 0) {
                            int i6 = 0;
                            while (true) {
                                String strSubstring = str.substring(i6, i6 + length2);
                                o.g(strSubstring, "substring(...)");
                                if (p.D0(str2, strSubstring, false)) {
                                    length = length2 + RCHTTPStatusCodes.BAD_REQUEST;
                                    break loop0;
                                }
                                if (i6 != length3) {
                                    i6++;
                                }
                            }
                        }
                        length2--;
                    }
                    length = 0;
                } else {
                    length = str2.length() + 800;
                }
                if (length < 400) {
                }
            }
        }
        return 0;
    }

    public static boolean e(String str) {
        String string;
        if (str != null && (string = p.o1(str).toString()) != null && string.length() != 0 && !string.equalsIgnoreCase("null") && !string.equals(".") && !string.equals("..") && !string.equalsIgnoreCase("MagnetName")) {
            for (int i6 = 0; i6 < string.length(); i6++) {
                if (Character.isLetterOrDigit(string.charAt(i6))) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean f(String str) {
        if (str != null) {
            return f5650a.a(str);
        }
        return false;
    }

    public static String g(String hash) {
        o.h(hash, "hash");
        return AbstractC2761L.g("magnet:?xt=urn:btih:", hash, "&dn=", URLEncoder.encode("MagnetName", S4.a.f6257a.name()));
    }

    public static Object h(List list, int i6, f fVar, C4.c cVar) {
        return E.k(new b(list, i6, fVar, null), cVar);
    }

    public static ArrayList i(List list, J4.c cVar, J4.c cVar2, J4.c cVar3) {
        ArrayList arrayList = new ArrayList();
        for (Object obj : list) {
            if (f((String) cVar.invoke(obj))) {
                arrayList.add(obj);
            }
        }
        ArrayList arrayList2 = new ArrayList(AbstractC3253s.G(arrayList, 10));
        for (Object obj2 : arrayList) {
            String str = (String) cVar.invoke(obj2);
            e.b(str);
            arrayList2.add(new v(str, ((Number) cVar2.invoke(obj2)).longValue(), (String) cVar3.invoke(obj2)));
        }
        return arrayList2;
    }

    public static Object j(List list, String str, Integer num, Integer num2, J4.c cVar, J4.c cVar2, C0556x0 c0556x0, String str2) {
        String string;
        Integer num3;
        if (c0556x0 != null) {
            ArrayList arrayList = new ArrayList();
            for (Object obj : list) {
                if (((Boolean) c0556x0.invoke(obj)).booleanValue()) {
                    arrayList.add(obj);
                }
            }
            list = arrayList;
        }
        if (!list.isEmpty()) {
            if (o.c(str, "tv") && num != null && num2 != null) {
                if (str2 == null || (string = p.o1(str2).toString()) == null || string.length() <= 0) {
                    string = null;
                }
                if (string != null) {
                    return b(list, num.intValue(), num2.intValue(), string, cVar);
                }
                int iIntValue = num.intValue();
                int iIntValue2 = num2.intValue();
                for (Object obj2 : list) {
                    String str3 = (String) cVar.invoke(obj2);
                    if (f(str3)) {
                        k kVarC = e.c(str3);
                        if (kVarC == null) {
                            try {
                                d dVarB = e.b(str3);
                                Integer num4 = dVarB.f5654b;
                                if (num4 != null && num4.intValue() == iIntValue && (num3 = dVarB.f5655c) != null && num3.intValue() == iIntValue2) {
                                    return obj2;
                                }
                            } catch (Throwable unused) {
                                continue;
                            }
                        } else if (((Number) kVarC.f25469a).intValue() == iIntValue && ((Number) kVarC.f25470b).intValue() == iIntValue2) {
                            return obj2;
                        }
                    }
                }
                return null;
            }
            if (o.c(str, "movie")) {
                long j4 = 0;
                Object obj3 = null;
                for (Object obj4 : list) {
                    long jLongValue = ((Number) cVar2.invoke(obj4)).longValue();
                    if (jLongValue > j4) {
                        obj3 = obj4;
                        j4 = jLongValue;
                    }
                }
                if (obj3 == null) {
                    return null;
                }
                if (f5652c.a((CharSequence) cVar.invoke(obj3))) {
                    return obj3;
                }
                return null;
            }
        }
        return null;
    }
}
