/**
 *
 * @param {Array} arrayList 要分段的数组
 * @param {Number} sectionLength 每段的长度
 */
export function sectionArr(arrayList, sectionLength) {
  var splitArr = [];
  for (var i = 0, len = arrayList.length; i < len; i += sectionLength) {
    splitArr.push(arrayList.slice(i, i + sectionLength));
  }
  return splitArr;
}

export function getGroupNum(num, min) {
  const max = Math.ceil(min * 1.5);
  if (num <= max) return num;
  let rate = [];
  for (let i = min; i <= max; i++) {
    const r = (num % i) / i;
    rate.push(r || 1);
  }
  const maxRate = getArrayMaxNum(rate);
  const pos = rate.indexOf(maxRate);
  return min + pos;
}

/**
 *
 * @param {Array} arrayList 要分段的数组
 * @param {Number} sectionLength 每段的长度
 * @param {Number} firstPageNum 第一段的长度
 */
export function sectionArrFirstPage(arrayList, sectionLength, firstPageNum) {
  var splitArr = [];
  var currentSectionLength = 0;
  for (var i = 0, len = arrayList.length; i < len; i += currentSectionLength) {
    if (i === 0) {
      currentSectionLength = firstPageNum;
    } else {
      currentSectionLength = sectionLength;
    }
    splitArr.push(arrayList.slice(i, i + currentSectionLength));
  }
  return splitArr;
}

/**
 * 组元素个数少于10个时，此法更方便
 * @param {Array} arrayList 要分段的数组
 * @param {Number} sectionMaxItemLength 最大分段元素个数
 */
export function sectionArrDynamic(
  arrayList,
  sectionMaxItemLength,
  childrenName
) {
  var splitArr = [];
  var splitSectionItemLen = 0;
  var splitSectionArr = [];
  for (var i = 0, len = arrayList.length; i < len; i++) {
    if (
      splitSectionItemLen + arrayList[i][childrenName].length >
      sectionMaxItemLength
    ) {
      splitArr.push(splitSectionArr);
      splitSectionItemLen = 0;
      splitSectionArr = [];
    }
    splitSectionItemLen += arrayList[i][childrenName].length;
    splitSectionArr.push(arrayList[i]);
  }
  splitArr.push(splitSectionArr);
  return splitArr;
}
/**
 * 组元素过多，且无规律时，使用此方法更合理
 * @param {*} arrayList
 * @param {*} sectionMaxItemLength
 * @param {*} childrenName
 */
export function groupSectionArrDynamic(
  arrayList,
  sectionMaxItemLength,
  childrenName
) {
  var splitArr = [];
  var splitSectionItemLen = 0;
  var splitSectionArr = [];

  arrayList.map(function (item) {
    var groupsList = [];
    item[childrenName].map(function (elem) {
      splitSectionItemLen++;
      groupsList.push(elem);

      if (splitSectionItemLen >= sectionMaxItemLength) {
        splitSectionArr.push({
          name: item.name,
          [childrenName]: groupsList,
        });
        splitArr.push(splitSectionArr);
        splitSectionItemLen = 0;
        splitSectionArr = [];
        groupsList = [];
      }
    });

    if (groupsList.length)
      splitSectionArr.push({
        name: item.name,
        [childrenName]: groupsList,
      });
  });
  if (splitSectionArr.length) splitArr.push(splitSectionArr);

  return splitArr;
}

export function getArrayMaxNum(arr) {
  return Math.max.apply(null, arr);
}
export function getAxisTopMax(dataList, chartType) {
  // barPointTopic
  // barPointGrade
  var arrList = [];
  if (chartType === "barPointTopic") {
    dataList.map(function (item) {
      arrList.push(item.avgScore);
      arrList.push(item.stdevScore);
    });
    return Math.ceil(getArrayMaxNum(arrList));
  } else if (chartType === "barPointGrade") {
    dataList.map(function (item) {
      arrList.push(item.avgScore);
      arrList.push(item.maxScore);
      arrList.push(item.minScore);
    });
    return Math.ceil(getArrayMaxNum(arrList) / 10) * 10;
  }
}
