export default {
  methods: {
    mergeAnswerColumns(source, noKey, optionKey, answerKey, optionValsKey, optionDivider = "|") {
      let newColumns = [noKey, optionKey, optionValsKey];
      let options = source.columns.filter((c) => c.startsWith(optionKey));
      const optionIndexes = options.map((op) => source.columns.indexOf(op));
      const noIndex = source.columns.indexOf(noKey);
      const answerIndex = source.columns.indexOf(answerKey);
      const extra = this._removeArrayItems([...source.columns], [noKey, ...options, answerKey])
      const extraIndexes = extra.map(e => source.columns.indexOf(e))
      newColumns = [...newColumns, ...extra]
      options = options.map((op) => op.replace(optionKey, "").replace(optionDivider, ""));
      let newRows = [];
      for (let index = 0; index < source.rows.length; index++) {
        const rawRow = source.rows[index];
        const no = rawRow[noIndex];
        const answer = rawRow[answerIndex];
        const optionVals = optionIndexes.map((idx) => rawRow[idx]);
        const extraVals = extraIndexes.map(idx => rawRow[idx]);
        const noWithAnswer = answer ? no + "(" + answer + ")" : no;
        newRows.push([noWithAnswer, options, optionVals, ...extraVals]);
      }
      return {
        columns: newColumns,
        rows: newRows,
      };
    },
    mergePagingTabNameFormatter(rows, idx) {
      if (!rows || !rows.length) return "";
      if (rows.length == 1) return this._regexFilterQuestionNo(rows[0][0]) + "题";
      const start = this._regexFilterQuestionNo(rows[0][0]);
      const end = this._regexFilterQuestionNo(rows[rows.length - 1][0]);
      return `${start}-${end}题`;
    },
    _removeArrayItems(array, items) {
      items.forEach(item => {
        const idx = array.indexOf(item)
        if (idx > -1) array.splice(idx, 1)
      })
      return array
    },
    _regexFilterQuestionNo(str) {
      const bracketIdx = str.indexOf('(')
      if (bracketIdx == -1) return str
      return str.substring(0, bracketIdx)
    }
  }
}
