// 选项类型
export const optionTypeList = [
  { value: 'letter', label: '字母' },
  { value: 'number', label: '数字' },
  { value: 'capital', label: '大写字母' },
  { value: 'bracket_number', label: '括号数字' },
];

// 计算选项方法
export const computeOptionMethods = {
  [optionTypeList[0].value]: (i) => `${String.fromCharCode(97 + i)}.`,
  [optionTypeList[1].value]: (i) => `${i + 1}.`,
  [optionTypeList[2].value]: (i) => `${String.fromCharCode(65 + i)}.`,
  [optionTypeList[3].value]: (i) => `(${i + 1})`,
};

/**
 * 改变选项类型
 * @param {object} data 数据
 */
export function changeOptionType(data) {
  let index = optionTypeList.findIndex(({ value }) => value === data.option_number_show_mode);
  data.option_number_show_mode = optionTypeList[index + 1]?.value || optionTypeList[0].value;
}

/**
 * 计算选项题号
 * @param {Number} i 序号
 * @param {String} option_number_show_mode 选项类型
 * @returns String 题号
 */
export function computedQuestionNumber(i, option_number_show_mode) {
  const computationMethod = computeOptionMethods[option_number_show_mode];
  if (computationMethod) {
    return computationMethod(i);
  }
  return '';
}

// 题干类型
export const stemTypeList = [
  { value: 'text', label: '纯文本' },
  { value: 'rich', label: '富文本' },
];

// 分值类型
export const scoreTypeList = [
  { value: 'aggregate', label: '总分' },
  { value: 'subdivision', label: '细分' },
];

// 选择类型
export const selectTypeList = [
  { value: 'single', label: '单选' },
  { value: 'multiple', label: '多选' },
];

// 开关选项
export const switchOption = [
  { value: 'true', label: '开启' },
  { value: 'false', label: '关闭' },
];

// 字体大小列表
export const fontSizeList = [
  '8pt',
  '10pt',
  '11pt',
  '12pt',
  '14pt',
  '16pt',
  '18pt',
  '20pt',
  '22pt',
  '24pt',
  '26pt',
  '28pt',
  '30pt',
  '32pt',
  '34pt',
  '36pt',
];

/**
 * 是否开启
 * @param {String} value 值
 * @returns Boolean
 */
export function isEnable(value) {
  return value === switchOption[0].value;
}

// 题号类型
export const questionNumberTypeList = [
  { value: 'recalculate', label: '重新计算' },
  { value: 'follow', label: '跟随上题' },
];

export const svgNS = 'http://www.w3.org/2000/svg'; // SVG命名空间

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;
}

/**
 * 输入框输入小于0的返回0 且为整数
 * @param {number|string} number
 */
export function handleInputNumber(number) {
  const number_int = Number(number);
  return number_int > 0 ? Math.floor(number_int) : 1;
}
