// 创建一个 ArrayBuffer 并查看其值
export function transformArrayBuffer(buffer: ArrayBuffer): NFC.TransformId {
  if (!buffer) {
    return {
      byteArray: [],
      hexArray: [],
      hexString: '',
    };
  }

  // 将 ArrayBuffer 转为 Uint8Array（按字节读取）
  const uint8Array = new Uint8Array(buffer);

  // 方式1：转为普通数组
  const byteArray = Array.from(uint8Array);
  // console.log('ArrayBuffer 字节值（十进制）:', byteArray);

  // 方式2：转为十六进制（NFC 场景常用，因为 NFC 数据多以十六进制展示）
  const hexArray = byteArray.map((byte) =>
    byte.toString(16).padStart(2, '0').toUpperCase(),
  );

  const hexString = hexArray.join(':');

  return {
    byteArray,
    hexArray,

    hexString,
  };
}

// 可以添加解析函数来解析 ATQA 信息
export function parseAtqa(atqaBuffer: ArrayBuffer) {
  const uint8Array = new Uint8Array(atqaBuffer);
  if (uint8Array.length < 2) return null;

  const byte1 = uint8Array[0];
  const byte2 = uint8Array[1];

  return {
    uidType: (byte1 >> 7) & 0x01, // UID 类型
    storageSize: (byte1 >> 4) & 0x07, // 存储大小编码
    manufacturer: byte1 & 0x0f, // 制造商代码
    cardFeatures: (byte2 >> 4) & 0x0f, // 功能特性
    reserved: byte2 & 0x0f, // 保留位
  };
}

export function parseAtqaAndroid(atqa: number[]) {
  const byte1 = atqa[0];
  const byte2 = atqa[1];
  return {
    uidType: (byte1 >> 7) & 0x01, // UID 类型
    storageSize: (byte1 >> 4) & 0x07, // 存储大小编码
    manufacturer: byte1 & 0x0f, // 制造商代码
    cardFeatures: (byte2 >> 4) & 0x0f, // 功能特性
    reserved: byte2 & 0x0f, // 保留位
  };
}

/**
 * 获取 SAK 类型
 *
 * @export
 * @param {number} sak
 * @return {*}
 */
export function getSakType(sak: number) {
  switch (sak) {
    case 0x00:
      return '未指定类型';
    case 0x08:
      return 'Mifare Classic 1K';
    case 0x09:
      return 'Mifare Classic Mini';
    case 0x10:
      return 'Mifare Plus';
    case 0x11:
      return 'Mifare Classic 4K';
    case 0x18:
      return 'Mifare Ultralight / Ultralight C';
    case 0x20:
      return 'DESFire';
    default:
      return '未知';
  }
}

export function getSakHex(sak: number) {
  return '0x' + (sak < 16 ? '0' : '') + sak.toString(16).toUpperCase();
}

/** 获取读写标准 */
export function getReadWriteStandard(nfcTypes: NFC.NfcType[]) {
  return nfcTypes.map((nfcType) => {
    switch (nfcType) {
      case 'NFC-A':
      case 'MIFARE Classic':
      case 'MIFARE Ultralight':
        return 'ISO 14443-3A';
      case 'NFC-B':
        return 'ISO 14443-3B';
      case 'NFC-F':
        return 'ISO 14443-3F';
      case 'NFC-V':
        return 'ISO 15693';
      case 'Ndef':
        return 'NDEF';
      case 'ISO-DEP':
        return 'ISO 14443-4';
      case 'Ndef Formatable':
        return 'NDEF Formatable';
      default:
        return '未知';
    }
  });
}

export function getAndroidTechs(techs: string[]) {
  return techs.map((tech) => {
    switch (tech) {
      case 'android.nfc.tech.IsoDep':
        return 'ISO-DEP';
      case 'android.nfc.tech.MifareClassic':
        return 'MIFARE Classic';
      case 'android.nfc.tech.MifareUltralight':
        return 'MIFARE Ultralight';
      case 'android.nfc.tech.NfcA':
        return 'NFC-A';
      case 'android.nfc.tech.NfcB':
        return 'NFC-B';
      case 'android.nfc.tech.NfcF':
        return 'NFC-F';
      case 'android.nfc.tech.NfcV':
        return 'NFC-V';
      case 'android.nfc.tech.Ndef':
        return 'Ndef';
      case 'android.nfc.tech.NdefFormatable':
        return 'Ndef Formatable';
      default:
        return '';
    }
  });
}
