package com.sistemasolton.itranvias;

import android.nfc.tech.IsoDep;
import io.ktor.http.ContentDisposition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import kotlin.Metadata;
import kotlin.UByte;
import kotlin.collections.ArraysKt;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.ByteCompanionObject;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.CharsKt;
import kotlin.text.StringsKt;

/* JADX INFO: compiled from: CardRepositoryImpl.kt */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000,\n\u0000\n\u0002\u0010\b\n\u0002\u0010\u0012\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\f\n\u0002\b\u0005\u001a\u001a\u0010\u0000\u001a\u00020\u0001*\u00020\u00022\u0006\u0010\u0003\u001a\u00020\u00012\u0006\u0010\u0004\u001a\u00020\u0001\u001a\u0014\u0010\u0005\u001a\u00020\u0006*\u00020\u00022\b\b\u0002\u0010\u0007\u001a\u00020\b\u001a\u0012\u0010\t\u001a\u00020\u0006*\u00020\u00022\u0006\u0010\n\u001a\u00020\u0001\u001a\n\u0010\u000b\u001a\u00020\b*\u00020\u0002\u001a\u0012\u0010\f\u001a\u00020\b*\u00020\u00022\u0006\u0010\r\u001a\u00020\b\u001a\u0018\u0010\f\u001a\u00020\b2\u0006\u0010\u000e\u001a\u00020\b2\u0006\u0010\r\u001a\u00020\bH\u0002\u001a\n\u0010\u000f\u001a\u00020\b*\u00020\u0001\u001a\u001a\u0010\u0010\u001a\u00020\u0001*\u00020\u00012\u0006\u0010\u0011\u001a\u00020\u00012\u0006\u0010\u0004\u001a\u00020\u0001\u001a\n\u0010\u0012\u001a\u00020\u0002*\u00020\u0013\u001a\n\u0010\u0014\u001a\u00020\u0002*\u00020\u0013\u001a\n\u0010\u0015\u001a\u00020\u0002*\u00020\u0013\u001a$\u0010\u0016\u001a\u00020\u0002*\u00020\u00132\u0006\u0010\u0017\u001a\u00020\u00022\u0006\u0010\u0018\u001a\u00020\u00012\b\b\u0002\u0010\u0007\u001a\u00020\b\u001a\n\u0010\u0019\u001a\u00020\u001a*\u00020\b\u001a\u0012\u0010\u001b\u001a\u00020\b*\u00020\b2\u0006\u0010\u001c\u001a\u00020\u0001\u001a\u001c\u0010\u001d\u001a\u00020\b*\u00020\b2\u0006\u0010\n\u001a\u00020\u00012\b\b\u0002\u0010\u001e\u001a\u00020\b¨\u0006\u001f"}, d2 = {"toIntBigEndian", "", "", "offset", "length", "requireStatusWord", "", "statusWord", "", "requireSize", ContentDisposition.Parameters.Size, "bcdToDecString", "panToIep", "apli", "pan16", "toBalanceString", "getBits", "start", "selectTransportNetwork", "Landroid/nfc/tech/IsoDep;", "readEnvironment", "readContract", "sendApdu", "command", "responseSize", "luhnCheckDigit", "", "maskFirstNChars", "n", "groupEvery", "sep", "composeApp_release"}, k = 2, mv = {2, 1, 0}, xi = 48)
public final class CardRepositoryImplKt {
    public static final int toIntBigEndian(byte[] bArr, int i, int i2) {
        Intrinsics.checkNotNullParameter(bArr, "<this>");
        if (i < 0 || i + i2 > bArr.length) {
            throw new IllegalArgumentException(("Array too small: need " + i2 + " bytes at offset " + i + " but size is " + bArr.length).toString());
        }
        int i3 = 0;
        for (int i4 = 0; i4 < i2; i4++) {
            i3 = (i3 << 8) | (bArr[i + i4] & UByte.MAX_VALUE);
        }
        return i3;
    }

    public static /* synthetic */ void requireStatusWord$default(byte[] bArr, String str, int i, Object obj) {
        if ((i & 1) != 0) {
            str = "9000";
        }
        requireStatusWord(bArr, str);
    }

    public static final void requireStatusWord(byte[] bArr, String statusWord) {
        Intrinsics.checkNotNullParameter(bArr, "<this>");
        Intrinsics.checkNotNullParameter(statusWord, "statusWord");
        String str = String.format("%02X%02X", Arrays.copyOf(new Object[]{Byte.valueOf(bArr[bArr.length - 2]), Byte.valueOf(bArr[bArr.length - 1])}, 2));
        Intrinsics.checkNotNullExpressionValue(str, "format(...)");
        if (!Intrinsics.areEqual(statusWord, str)) {
            throw new IllegalArgumentException(("APDU error: expected status word " + statusWord + " but was " + str).toString());
        }
    }

    public static final void requireSize(byte[] bArr, int i) {
        Intrinsics.checkNotNullParameter(bArr, "<this>");
        if (bArr.length == i) {
            return;
        }
        throw new IllegalArgumentException(("APDU response size error: expected " + i + " bytes but was " + bArr.length).toString());
    }

    public static final String bcdToDecString(byte[] bArr) {
        Intrinsics.checkNotNullParameter(bArr, "<this>");
        if (bArr.length != 8) {
            throw new IllegalArgumentException(("8 BCD bytes were expected from PAN, but they were " + bArr.length).toString());
        }
        return ArraysKt.joinToString$default(bArr, (CharSequence) "", (CharSequence) null, (CharSequence) null, 0, (CharSequence) null, new Function1() { // from class: com.sistemasolton.itranvias.CardRepositoryImplKt$$ExternalSyntheticLambda0
            @Override // kotlin.jvm.functions.Function1
            public final Object invoke(Object obj) {
                return CardRepositoryImplKt.bcdToDecString$lambda$5(((Byte) obj).byteValue());
            }
        }, 30, (Object) null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final CharSequence bcdToDecString$lambda$5(byte b) {
        int i = b & UByte.MAX_VALUE;
        int i2 = i >>> 4;
        int i3 = b & 15;
        if (i2 >= 10 || i3 >= 10) {
            String str = String.format("Byte is not valid BCD: %02X", Arrays.copyOf(new Object[]{Integer.valueOf(i)}, 1));
            Intrinsics.checkNotNullExpressionValue(str, "format(...)");
            throw new IllegalArgumentException(str.toString());
        }
        return new StringBuilder().append(i2).append(i3).toString();
    }

    public static final String panToIep(byte[] bArr, String apli) {
        Intrinsics.checkNotNullParameter(bArr, "<this>");
        Intrinsics.checkNotNullParameter(apli, "apli");
        return panToIep(bcdToDecString(bArr), apli);
    }

    private static final String panToIep(String str, String str2) {
        if (str2.length() != 2) {
            throw new IllegalArgumentException("Apli must have exactly 2 characters".toString());
        }
        String strPadEnd = StringsKt.padEnd(str, 16, '0');
        StringBuilder sb = new StringBuilder();
        String strSubstring = strPadEnd.substring(0, 6);
        Intrinsics.checkNotNullExpressionValue(strSubstring, "substring(...)");
        sb.append(strSubstring);
        sb.append(str2);
        String strSubstring2 = strPadEnd.substring(8, 15);
        Intrinsics.checkNotNullExpressionValue(strSubstring2, "substring(...)");
        sb.append(strSubstring2);
        sb.append('0');
        String string = sb.toString();
        String strSubstring3 = string.substring(0, 15);
        Intrinsics.checkNotNullExpressionValue(strSubstring3, "substring(...)");
        return strSubstring3 + luhnCheckDigit(string);
    }

    public static final String toBalanceString(int i) {
        String str = String.format("%,.2f", Arrays.copyOf(new Object[]{Double.valueOf(((double) i) / 100.0d)}, 1));
        Intrinsics.checkNotNullExpressionValue(str, "format(...)");
        return str;
    }

    public static final int getBits(int i, int i2, int i3) {
        if (i2 < 0 || i2 >= 16 || 1 > i3 || i3 >= 17 || i2 + i3 > 16) {
            throw new IllegalArgumentException("Invalid bit range".toString());
        }
        return (i >> i2) & ((1 << i3) - 1);
    }

    public static final byte[] selectTransportNetwork(IsoDep isoDep) {
        Intrinsics.checkNotNullParameter(isoDep, "<this>");
        return sendApdu$default(isoDep, new byte[]{ByteCompanionObject.MIN_VALUE, 38, 79, 17, 10}, 12, null, 4, null);
    }

    public static final byte[] readEnvironment(IsoDep isoDep) {
        Intrinsics.checkNotNullParameter(isoDep, "<this>");
        return sendApdu$default(isoDep, new byte[]{ByteCompanionObject.MIN_VALUE, 50, 0, 0, 24}, 26, null, 4, null);
    }

    public static final byte[] readContract(IsoDep isoDep) {
        Intrinsics.checkNotNullParameter(isoDep, "<this>");
        return sendApdu$default(isoDep, new byte[]{ByteCompanionObject.MIN_VALUE, 46, 1, 0, 32}, 34, null, 4, null);
    }

    public static /* synthetic */ byte[] sendApdu$default(IsoDep isoDep, byte[] bArr, int i, String str, int i2, Object obj) {
        if ((i2 & 4) != 0) {
            str = "9000";
        }
        return sendApdu(isoDep, bArr, i, str);
    }

    public static final byte[] sendApdu(IsoDep isoDep, byte[] command, int i, String statusWord) throws IOException {
        Intrinsics.checkNotNullParameter(isoDep, "<this>");
        Intrinsics.checkNotNullParameter(command, "command");
        Intrinsics.checkNotNullParameter(statusWord, "statusWord");
        byte[] bArrTransceive = isoDep.transceive(command);
        Intrinsics.checkNotNull(bArrTransceive);
        requireSize(bArrTransceive, i);
        requireStatusWord(bArrTransceive, statusWord);
        return bArrTransceive;
    }

    public static final char luhnCheckDigit(String str) {
        Intrinsics.checkNotNullParameter(str, "<this>");
        String str2 = str;
        ArrayList arrayList = new ArrayList(str2.length());
        int i = 0;
        for (int i2 = 0; i2 < str2.length(); i2++) {
            i++;
            int iDigitToInt = CharsKt.digitToInt(str2.charAt(i2));
            if (i % 2 == (str.length() + 1) % 2) {
                iDigitToInt *= 2;
            }
            if (iDigitToInt > 9) {
                iDigitToInt -= 9;
            }
            arrayList.add(Integer.valueOf(iDigitToInt));
        }
        return CharsKt.digitToChar((10 - (CollectionsKt.sumOfInt(arrayList) % 10)) % 10);
    }

    public static final String maskFirstNChars(String str, int i) {
        Intrinsics.checkNotNullParameter(str, "<this>");
        if (str.length() <= i) {
            return StringsKt.repeat(str, str.length());
        }
        String strRepeat = StringsKt.repeat(str, i);
        String strSubstring = str.substring(i);
        Intrinsics.checkNotNullExpressionValue(strSubstring, "substring(...)");
        return strRepeat + strSubstring;
    }

    public static final String groupEvery(String str, int i, String sep) {
        Intrinsics.checkNotNullParameter(str, "<this>");
        Intrinsics.checkNotNullParameter(sep, "sep");
        return CollectionsKt.joinToString$default(StringsKt.chunked(str, i), sep, null, null, 0, null, null, 62, null);
    }

    public static /* synthetic */ String groupEvery$default(String str, int i, String str2, int i2, Object obj) {
        if ((i2 & 2) != 0) {
            str2 = " ";
        }
        return groupEvery(str, i, str2);
    }
}