/**
 * 换算数据大小
 * @param {number} size
 * @returns {string} 换算后的数据大小，两位小数带单位
 */
export function conversionSize(size) {
  let _size = size;
  const units = ['B', 'KB', 'MB', 'GB'];
  let factor = 0;
  while (_size > 1024 && factor < units.length - 1) {
    _size /= 1024;
    factor += 1;
  }
  return `${_size.toFixed(2)}${units[factor]}`;
}

export const tone_data = [
  ['ā', 'á', 'ǎ', 'à', 'a'],
  ['ō', 'ó', 'ǒ', 'ò', 'o'],
  ['ē', 'é', 'ě', 'è', 'e'],
  ['ī', 'í', 'ǐ', 'ì', 'i'],
  ['ū', 'ú', 'ǔ', 'ù', 'u'],
  ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
  ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
  ['Ā', 'Á', 'Â', 'À', 'A'],
  ['Ō', 'Ó', 'Ô', 'Ò', 'O'],
  ['Ē', 'É', 'Ê', 'È', 'E'],
  ['Ī', 'Í', 'Î', 'Ì', 'I'],
  ['Ū', 'Ú', 'Û', 'Ù', 'U'],
];

/**
 * 添加声调
 * @param {Number} number
 * @param {String} con
 * @returns String
 */
export function addTone(number, con) {
  const zmList = ['a', 'o', 'e', 'i', 'u', 'v', 'ü', 'A', 'O', 'E', 'I', 'U'];
  let cons = con;
  if (number) {
    for (let i = 0; i < zmList.length; i++) {
      let zm = zmList[i];
      if (con.includes(zm)) {
        let zm2 = tone_data[i][number - 1];
        if (con.includes('iu')) {
          zm2 = tone_data[4][number - 1];
          cons = con.replace('u', zm2);
        } else if (con.includes('ui')) {
          zm2 = tone_data[3][number - 1];
          cons = con.replace('i', zm2);
        } else if (/yv|jv|qv|xv/.test(con)) {
          zm2 = tone_data[4][number - 1];
          cons = con.replace('v', zm2);
        } else if (/yü|jü|qü|xü/.test(con)) {
          zm2 = tone_data[4][number - 1];
          cons = con.replace('ü', zm2);
        } else {
          cons = con.replace(zm, zm2);
        }

        break;
      }
    }
  }
  return cons;
}

export function handleToneValue(valItem) {
  let numList = [];
  if (/[A-Za-zü]+\d/g.test(valItem)) {
    valItem.split('').forEach((item, i) => {
      if (/\d/.test(item)) {
        let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
        let con = valItem.substring(numIndex, i).replace(/\d/g, '');
        numList.push({
          number: item,
          con,
        });
      }
    });
  } else {
    numList = [];
  }

  return numList.length === 0 ? [{ con: valItem }] : numList;
}
