/**
 * 解析首字母转大写
 * @param str 字符串 ab
 * @returns 结果 Ab
 */
export function parseFirstUpper(str: string): string {
  if (!str) return str;
  str = str.replace(/[^_\w]+/g, '');
  return str.substring(0, 1).toUpperCase() + str.substring(1);
}

/**
 * 解析首字母转小写
 * @param str 字符串 Ab
 * @returns 结果 ab
 */
export function parseFirstLower(str: string): string {
  if (!str) return str;
  str = str.replace(/[^_\w]+/g, '');
  return str.substring(0, 1).toLowerCase() + str.substring(1);
}

/**
 * 解析下划线转驼峰
 * @param str 字符串 a_b
 * @returns  驼峰风格 aB
 */
export function parseStrLineToHump(str: string): string {
  if (!str) return str;
  return str.replace(/_(\w)/g, (_item, letter) => letter.toUpperCase());
}

/**
 * 解析驼峰转下划线
 * @param str 字符串 aB
 * @returns  下划线风格 a_b
 */
export function parseStrHumpToLine(str: string): string {
  if (!str) return str;
  return str
    .replace(/([A-Z])/g, '_$1')
    .toLowerCase()
    .replace(/^_/, '');
}

/**
 * 对象的key值驼峰转下划线
 * @param obj 对象属性 字符数组orJSON对象
 * @returns 驼峰转下划线
 */
export function parseObjHumpToLine(obj: any): any {
  if (obj === null || obj === undefined) {
    return obj;
  }
  if (Array.isArray(obj)) {
    return obj.map(v => parseObjHumpToLine(v));
  }
  if (typeof obj === 'object') {
    Object.keys(obj).forEach(key => {
      const new_key = parseStrHumpToLine(key);
      if (new_key !== key) {
        obj[new_key] = obj[key];
        delete obj[key];
      }
      obj[new_key] = parseObjHumpToLine(obj[new_key]);
    });
    return obj;
  }
  return obj;
}

/**
 * 对象的key值下划线转驼峰
 * @param obj 对象属性 字符数组orJSON对象
 * @returns 下划线转驼峰
 */
export function parseObjLineToHump(obj: any): any {
  if (obj === null || obj === undefined) {
    return obj;
  }
  if (Array.isArray(obj)) {
    return obj.map(v => parseObjLineToHump(v));
  }
  if (typeof obj === 'object') {
    Object.keys(obj).forEach(key => {
      const new_key = parseStrLineToHump(key);
      if (new_key !== key) {
        obj[new_key] = obj[key];
        delete obj[key];
      }
      obj[new_key] = parseObjLineToHump(obj[new_key]);
    });
    return obj;
  }
  return obj;
}

/**
 * 格式化文件大小
 * @param bytes 字节数
 * @param decimalPlaces 保留小数位,默认2位
 * @returns 单位 xB
 */
export function parseSizeFromFile(
  bytes: number,
  decimalPlaces: number = 2
): string {
  if (typeof bytes !== 'number' || isNaN(bytes) || bytes < 0) {
    return `${bytes}`;
  }
  if (bytes === 0) return '0 B';
  const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  let i = 0;
  while (bytes >= 1024 && i < units.length - 1) {
    bytes /= 1024;
    i++;
  }
  return `${bytes.toFixed(decimalPlaces || 1)} ${units[i]}`;
}

/**
 * 转换磁盘容量
 * @param size 数值大小
 * @returns
 */
export function parseSizeFromMB(size: number): string {
  const num = 1024.0;
  if (size < num) return size + ' MB';
  if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + ' GB';
  return (size / Math.pow(num, 3)).toFixed(2) + ' TB';
}

/**
 * 转换网络速率
 * @param size 数值大小
 * @returns
 */
export function parseSizeFromKBs(size: number): string {
  const num = 1024.0;
  if (size < num) return size + ' KB/s';
  if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + ' MB/s';
  if (size < Math.pow(num, 3)) {
    return (size / Math.pow(num, 2)).toFixed(2) + ' GB/s';
  }
  return (size / Math.pow(num, 3)).toFixed(2) + ' TB/s';
}

/**
 * 字节数转换速率
 * @param sizeByte 数值大小
 * @returns [值，单位]
 */
export function parseSizeFromKbs(sizeByte: number, timeInterval: number): any {
  sizeByte = Number(sizeByte) || 0;
  timeInterval = Number(timeInterval) || 1;
  //   let realBit:any=((sizeByte * 8) / timeInterval);
  //   if (realBit >= 0 && realBit < 1000) {
  //     return [realBit.toFixed(2),' bits/sec'];
  //  }

  //  if (realBit >= 1000 && realBit < 1000*1000) {
  //     return [(realBit/1000).toFixed(2) ,' Kbits/sec'];
  //  }

  //  if (realBit >= 1000*1000 && realBit < 1000*1000*1000) {
  //   return [((realBit/1000)/1000).toFixed(2) ,' Mbits/sec'];
  //  }

  //  if (realBit >= 1000*1000*1000) {
  //   return [((realBit/1000*1000)/1000).toFixed(2) ,' Gbits/sec'];
  //  }

  //  return [((realBit/1000*1000*1000)/1000).toFixed(2) ,' TB/sec'];

  let realBit: any = (sizeByte * 8) / timeInterval;

  return [(realBit / 1000 / 1000).toFixed(2), ' Mbits/sec'];
}

/**
 * 位数据转换单位
 * @param bits 位Bit大小 64009540 = 512.08 MB
 * @returns xx B / KB / MB / GB / TB / PB / EB / ZB / YB
 */
export function parseSizeFromBits(bits: number | string): string {
  bits = Number(bits) || 0;
  if (bits <= 0) return '0 B';
  bits = bits * 8;
  const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  const unitIndex = Math.floor(Math.log2(bits) / 10);
  const value = bits / Math.pow(1000, unitIndex);
  const unti = units[unitIndex];
  if (unitIndex > 0) {
    return `${value.toFixed(2)} ${unti}`;
  }
  return `${value} ${unti}`;
}

/**
 * 字节数转换单位
 * @param byte 字节Byte大小 64009540 = 512.08 MB
 * @param unit 指定单位 B / KB / MB / GB / TB / PB / EB / ZB / YB
 * @returns xx B / KB / MB / GB / TB / PB / EB / ZB / YB
 */
export function parseSizeFromByte(
  byte: number | string,
  unit?: string
): string {
  byte = Number(byte) || 0;
  if (byte <= 0) return '0 B';
  const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  let unitIndex = 0;
  let unitStr = 'B';
  if (unit) {
    const index = units.indexOf(unit);
    if (index > -1) {
      unitIndex = index;
      unitStr = unit;
    }
  } else {
    unitIndex = Math.floor(Math.log2(byte) / 10);
    unitStr = units[unitIndex];
  }
  const value = byte / Math.pow(1000, unitIndex);
  if (unitIndex > 0) {
    return `${value.toFixed(2)} ${unitStr}`;
  }
  return `${value} ${unitStr}`;
}
