package m4;

import java.text.ParseException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/* JADX INFO: renamed from: m4.x0, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public abstract class AbstractC2557x0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final long f21212a = TimeUnit.SECONDS.toNanos(1);

    public static void a(List list) {
        for (int i6 = 0; i6 < list.size(); i6++) {
            if (!(list.get(i6) instanceof Map)) {
                throw new ClassCastException(String.format(Locale.US, "value %s for idx %d in %s is not object", list.get(i6), Integer.valueOf(i6), list));
            }
        }
    }

    public static Boolean b(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }
        throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not Boolean", obj, str, map));
    }

    public static List c(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (obj instanceof List) {
            return (List) obj;
        }
        throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not List", obj, str, map));
    }

    public static List d(String str, Map map) {
        List listC = c(str, map);
        if (listC == null) {
            return null;
        }
        for (int i6 = 0; i6 < listC.size(); i6++) {
            if (!(listC.get(i6) instanceof String)) {
                throw new ClassCastException(String.format(Locale.US, "value '%s' for idx %d in '%s' is not string", listC.get(i6), Integer.valueOf(i6), listC));
            }
        }
        return listC;
    }

    public static Double e(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (obj instanceof Double) {
            return (Double) obj;
        }
        if (!(obj instanceof String)) {
            throw new IllegalArgumentException(String.format("value '%s' for key '%s' in '%s' is not a number", obj, str, map));
        }
        try {
            return Double.valueOf(Double.parseDouble((String) obj));
        } catch (NumberFormatException unused) {
            throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not a double", obj, str));
        }
    }

    public static Integer f(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (!(obj instanceof Double)) {
            if (!(obj instanceof String)) {
                throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not an integer", obj, str));
            }
            try {
                return Integer.valueOf(Integer.parseInt((String) obj));
            } catch (NumberFormatException unused) {
                throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not an integer", obj, str));
            }
        }
        Double d6 = (Double) obj;
        int iIntValue = d6.intValue();
        if (iIntValue == d6.doubleValue()) {
            return Integer.valueOf(iIntValue);
        }
        throw new ClassCastException("Number expected to be integer: " + d6);
    }

    public static Map g(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (obj instanceof Map) {
            return (Map) obj;
        }
        throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not object", obj, str, map));
    }

    public static String h(String str, Map map) {
        if (!map.containsKey(str)) {
            return null;
        }
        Object obj = map.get(str);
        if (obj instanceof String) {
            return (String) obj;
        }
        throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not String", obj, str, map));
    }

    public static Long i(String str, Map map) {
        String strH = h(str, map);
        if (strH == null) {
            return null;
        }
        try {
            return Long.valueOf(k(strH));
        } catch (ParseException e6) {
            throw new RuntimeException(e6);
        }
    }

    public static long j(int i6, long j4) {
        long j6 = i6;
        long j7 = f21212a;
        if (j6 <= (-j7) || j6 >= j7) {
            j4 = A4.g.u(j4, j6 / j7);
            i6 = (int) (j6 % j7);
        }
        if (j4 > 0 && i6 < 0) {
            i6 = (int) (((long) i6) + j7);
            j4--;
        }
        if (j4 < 0 && i6 > 0) {
            i6 = (int) (((long) i6) - j7);
            j4++;
        }
        if (j4 >= -315576000000L && j4 <= 315576000000L) {
            long j8 = i6;
            if (j8 >= -999999999 && j8 < j7 && ((j4 >= 0 && i6 >= 0) || (j4 <= 0 && i6 <= 0))) {
                long nanos = TimeUnit.SECONDS.toNanos(j4);
                long j9 = i6;
                long j10 = nanos + j9;
                return (((j9 ^ nanos) > 0L ? 1 : ((j9 ^ nanos) == 0L ? 0 : -1)) < 0) | ((nanos ^ j10) >= 0) ? j10 : ((j10 >>> 63) ^ 1) + Long.MAX_VALUE;
            }
        }
        throw new IllegalArgumentException("Duration is not valid. See proto definition for valid values. Seconds (" + j4 + ") must be in range [-315,576,000,000, +315,576,000,000]. Nanos (" + i6 + ") must be in range [-999,999,999, +999,999,999]. Nanos must have the same sign as seconds");
    }

    public static long k(String str) throws ParseException {
        boolean z6;
        String strSubstring;
        int iCharAt;
        if (str.isEmpty() || str.charAt(str.length() - 1) != 's') {
            throw new ParseException("Invalid duration string: ".concat(str), 0);
        }
        if (str.charAt(0) == '-') {
            str = str.substring(1);
            z6 = true;
        } else {
            z6 = false;
        }
        String strSubstring2 = str.substring(0, str.length() - 1);
        int iIndexOf = strSubstring2.indexOf(46);
        if (iIndexOf != -1) {
            strSubstring = strSubstring2.substring(iIndexOf + 1);
            strSubstring2 = strSubstring2.substring(0, iIndexOf);
        } else {
            strSubstring = "";
        }
        long j4 = Long.parseLong(strSubstring2);
        if (strSubstring.isEmpty()) {
            iCharAt = 0;
        } else {
            iCharAt = 0;
            for (int i6 = 0; i6 < 9; i6++) {
                iCharAt *= 10;
                if (i6 < strSubstring.length()) {
                    if (strSubstring.charAt(i6) < '0' || strSubstring.charAt(i6) > '9') {
                        throw new ParseException("Invalid nanoseconds.", 0);
                    }
                    iCharAt = (strSubstring.charAt(i6) - '0') + iCharAt;
                }
            }
        }
        if (j4 < 0) {
            throw new ParseException("Invalid duration string: ".concat(str), 0);
        }
        if (z6) {
            j4 = -j4;
            iCharAt = -iCharAt;
        }
        try {
            return j(iCharAt, j4);
        } catch (IllegalArgumentException unused) {
            throw new ParseException("Duration value is out of range.", 0);
        }
    }
}
