import { serialNumberTypeList, displayList, serialNumberPositionList } from '@/views/book/courseware/data/common';
import { getRandomNumber } from '@/utils';

/**
 * 获取连线题属性
 * @param {number} column_num 列数 2 ~ 3
 */
export function getMatchingProperty(column_num = 2) {
  if (column_num !== 2 && column_num !== 3) {
    throw new Error('column_num must be 2 or 3');
  }

  return {
    serial_number: 1,
    sn_type: serialNumberTypeList[0].value,
    sn_position: serialNumberPositionList[0].value,
    sn_display_mode: displayList[0].value,
    serial_number_type_list: serialNumberTypeList.slice(0, column_num).map((item) => item.value),
    row_num: 2, // 行数
    column_num, // 列数 2 ~ 3
  };
}

export function getOptionItem() {
  return {
    content: '',
    mark: getRandomNumber(),
  };
}

/**
 * 获取连线题选项
 * @param {number} column_num 列数 2 ~ 3
 */
export function getOption(column_num = 2) {
  if (column_num !== 2 && column_num !== 3) {
    throw new Error('column_num must be 2 or 3');
  }

  let option = [];
  for (let i = 0; i < column_num; i++) {
    option.push(getOptionItem());
  }

  return option;
}

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

export function getMatchingData() {
  let option_list = [getOption(), getOption()];

  let answer_list = option_list.map((item) =>
    item.map(({ mark }, i) => {
      return i === 0 ? { mark } : { mark: '' };
    }),
  );

  return {
    type: 'matching',
    title: '连线',
    option_list,
    answer: {
      answer_list,
    },
    min_height: 120,
    min_width: 290,
    property: getMatchingProperty(),
  };
}
