/**
 * 文字链接
 */
export function cancleAnim (ev) {
  ev.target.style.color = '#57acfb';
  ev.target.style.cursor = 'pointer';
};
export function startAnim (ev) {
  ev.target.style.color = '#6789AC';
};

/**
 * 生成uuid
 * @returns
 */
export function generateUUID () {
  var d = new Date().getTime(); //Timestamp
  var d2 = (performance && performance.now && (performance.now() * 1000)) || 0; //Time in microseconds since page-load or 0 if unsupported
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16; //random number between 0 and 16
    if (d > 0) { //Use timestamp until depleted
      r = (d + r) % 16 | 0;
      d = Math.floor(d / 16);
    } else { //Use microseconds since page-load if supported
      r = (d2 + r) % 16 | 0;
      d2 = Math.floor(d2 / 16);
    }
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
}

/**
 * 判断是几维数组
 * @param {*} arr
 * @returns
 */
export function getArrayDimensions (arr) {
  if (Array.isArray(arr)) {
    let maxDimension = 0;
    for (let i = 0; i < arr.length; i++) {
      const dimensions = getArrayDimensions(arr[i]);
      maxDimension = Math.max(maxDimension, dimensions);
    }
    return maxDimension + 1;
  }
  return 1;
}
/**
 * 数组转化为对象
 */
export function arrayToMap (list, key) {
  const obj = {};
  list.forEach(item => obj[item[key]] = item);
  return obj;
};
/**
 * 计算百分比
 */
export function getPercent (mun, max) {
  var newNum = (mun / max) * 100;
  return newNum.toFixed(2);
};
/**
 * 求和
 */
export function getMax (list, key) {
  var max = 0;
  if (key) {
    list.map(item => {
      max += item[key];
    });
  } else {
    list.map(item => {
      max += item;
    });
  }
  return max;
};
/**
 * 最大值
 */
export function getTotal (list, key) {
  let valueList = list;
  if (key) {
    valueList = list.map(item => item[key]);
  }
  return valueList.reduce((prev, now) => {
    return prev + now;
  });
};
/**
 * 获取客户端标识
 */
export function getClientId () {
  return '5cf4828d94e9a37422d323a3';
};
/**
 * 根据范围获取中心点
 */
export function getCenterByExtent (extent) {
  const x = (extent[0] + extent[2]) / 2;
  const y = (extent[1] + extent[3]) / 2;
  return [x, y];
};
// 随机获取一个范围整数
export function getRandom (m, n) {
  return Math.floor(Math.random() * (m - n + 1) + n);
};

export function random (min, max) {

  return Math.floor(Math.random() * (max - min + 1)) + min;

}

/**
 * 获取时间
 */
export function getDay (_date, fmt) {
  const weekList = ['日', '一', '二', '三', '四', '五', '六'];
  const date = _date || new Date();
  const opt = {
    'y+': date.getFullYear().toString(), // 年
    'M+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'w+': weekList[date.getDay()], // 周
    'h+': date.getHours().toString(), // 时
    'm+': date.getMinutes().toString(), // 分
    's+': date.getSeconds().toString() // 秒
  };
  for (const k in opt) {
    const ret = new RegExp('(' + k + ')').exec(fmt);
    if (ret) {
      if (/(y+)/.test(k)) {
        fmt = fmt.replace(ret[1], opt[k].substring(4 - ret[1].length));
      } else {
        fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
      }
    }
  }
  return fmt;
}

// 回显数据字典
export function selectDictLabel(datas, value) {

  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == ('' + value)) {
      actions.push(datas[key].dictLabel);
      return true;
    }
  })
  return actions.join('');
}

export function array_toMap(arr, byFunc, overriding)
{
  let res = {};
  arr.forEach((x) => {
    let a = byFunc(x);
    if(!res.hasOwnProperty(a) || overriding)
      res[a] = x;
  });
  return res;
}
