
import { logger } from '@zenview/micro-utils';
const indexArr = getMakerIndex();

function getLineWidth(count) {
  if (count <= 5) {
    return 2;
  }
  if (count <= 10) {
    return 4;
  }
  if (count <= 50) {
    return 7;
  }
  if (count <= 100 || count > 100) {
    return 10;
  }
}

function getMakerIndex() {
  let index = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(
    ','
  );
  let arr = cloneDeep(index);
  let a = cloneDeep(index);
  let b = cloneDeep(index);
  a.forEach(v1 => {
    b.forEach(v2 => {
      arr.push(`${v1}${v2}`);
    });
  });
  return arr;
}

function getLineDefaultStyle(lw) {
  return {
    normal: {
      width: lw,
      color: 'red',
      curveness: 0.2,
    },
  };
}
export const computedChartData = (data, key = 'cid', nameKey = 'name') => {
  let index = -1;
  const indexMap = {};
  const result = { data: [], links: [], pointsMap: {} };
  if (!Array.isArray(data) || data.length === 0) {
    logger.debug('人员规律数据为空！');
    return result;
  }

  /**
   * @解析目标点数据
   */
  for (let i = 0, l = data.length; i < l; i++) {
    const item = data[i];
    if (!item.position || !item.position[0] || !item.position[1]) {
      continue;
    }
    if (indexMap[item[nameKey]]) {
      item.index = indexMap[item[nameKey]];
    } else {
      index += 1;
      item.index = indexMap[item[nameKey]] = indexArr[index];
    }
    item.value = item.position;
    item.name = item[nameKey];
    item.lon = item.position[0];
    item.lat = item.position[1];
    if (!result.pointsMap[item[key]]) {
      result.pointsMap[item[key]] = item;
    }
    if (!result.data.find(v => v.name === item.name)) { //这一步是为了避免出现重复点位导致echart报错
      result.data.push(item);
    }
    if (!Array.isArray(item.list) || item.list.length === 0) {
      continue;
    }
    /**
     * @解析到达目标点数据
     */
    for (let i = 0, l = item.list.length; i < l; i++) {
      const link = item.list[i];
      if (!link.position || !link.position[0] || !link.position[1]) {
        continue;
      }
      const lw = getLineWidth(link.count);
      if (indexMap[link[nameKey]]) {
        link.index = indexMap[link[nameKey]];
      } else {
        index++;
        link.index = indexMap[link[nameKey]] = indexArr[index];
      }
      link.source = link[nameKey];
      link.target = item.name;
      link.symbol = ['none', 'arrow'];
      link.symbolSize = Math.round(lw * 1.2) + 12;
      link.lineStyle = getLineDefaultStyle(lw);
      if (!result.pointsMap[link[key]]) {
        result.pointsMap[link[key]] = link;
      }
      result.links.push(link);
    }
  }

  return result;
}