// 将后端返回的字段转换成树组件的ApiSelectTree的对应字段
export const copyTransFun = (arr: any[]) => {
  arr.forEach((item) => {
    if (item.name) {
      item.label = item.name;
      delete item.name;
    }
    if (item.id) {
      item.value = item.id;
      delete item.id;
    }
    if (item.children) {
      if (item.children.length) {
        copyTransFun(item.children);
      }
    }
  });
};

// 将后端返回的字段转换成树组件的Tree的对应字段
export const copyTransTreeFun = (arr: any[]) => {
  arr.forEach((item) => {
    if (item.name) {
      item.title = item.name;
      delete item.name;
    }
    if (item.id) {
      item.key = item.id;
      delete item.id;
    }
    if (item.children) {
      if (item.children.length) {
        copyTransTreeFun(item.children);
      }
    }
  });
};

// 百度地图url
const ak = '7uOPPyAHn2Y2ZryeQqHtcRqtIY374vKa';

const register_BAI_DU_MAP_URL = (ak: string) => {
  return `https://api.map.baidu.com/getscript?v=3.0&ak=${ak}`;
};

const registerBaiDuMapGlLib = (ak: string) => {
  return `//api.map.baidu.com/getscript?type=webgl&v=1.0&ak=${ak}&services=&t=${Date.now()}`;
};

export const BAI_DU_MAP_TRACK_ANIMATION = '//mapopen.bj.bcebos.com/github/BMapGLLib/TrackAnimation/src/TrackAnimation.min.js';

export const BAI_DU_MAP_GL_LIB = registerBaiDuMapGlLib(ak);

export const BAI_DU_MAP_URL = register_BAI_DU_MAP_URL(ak);

// 数字加上每三位加上逗号
export function toThousands(num) {
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

function type(obj) {
  const toString = Object.prototype.toString;
  const map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
  };
  return map[toString.call(obj)];
}

export const deepClone = (data) => {
  let t = type(data),
    o,
    i,
    ni;

  if (t === 'array') {
    o = [];
  } else if (t === 'object') {
    o = {};
  } else {
    return data;
  }

  if (t === 'array') {
    for (i = 0, ni = data.length; i < ni; i++) {
      o.push(deepClone(data[i]));
    }
    return o;
  } else if (t === 'object') {
    for (i in data) {
      o[i] = deepClone(data[i]);
    }
    return o;
  }
};

//给日期加0
function addDate0(time) {
  if (time.toString().length == 1) {
    time = '0' + time.toString();
  }
  return time;
}

export const getNearly7Day = () => {
  const days = [];
  const date = new Date();
  for (let i = 0; i <= 24 * 6; i += 24) {
    //今天加上前6天
    const dateItem = new Date(date.getTime() - i * 60 * 60 * 1000); //使用当天时间戳减去以前的时间毫秒（小时*分*秒*毫秒）
    const y = dateItem.getFullYear(); //获取年份
    let m = dateItem.getMonth() + 1; //获取月份js月份从0开始，需要+1
    let d = dateItem.getDate(); //获取日期
    m = addDate0(m); //给为单数的月份补零
    d = addDate0(d); //给为单数的日期补零
    const valueItem = y + '-' + m + '-' + d + ' 00:00:00'; //组合
    days.push(valueItem); //添加至数组
  }
  return days;
};

export const formatDateTime = (date) => {
  const y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  let d = date.getDate();
  d = d < 10 ? '0' + d : d;
  let h = date.getHours();
  h = h < 10 ? '0' + h : h;
  let minute = date.getMinutes();
  minute = minute < 10 ? '0' + minute : minute;
  let second = date.getSeconds();
  second = second < 10 ? '0' + second : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};

export const getStrUnicode = (str) => {
  if (str) {
    let code = '';
    for (let i = 0; i < str.length; i++) {
      const r = str[i].charCodeAt();
      code += r;
    }
    return '&' + code;
  }
};

// 获取选中的测站信息
export const obtainingStations = (arr: Array, stcdId) => {
  for (let index = 0; index < arr.length; index++) {
    const element = arr[index];
    if (stcdId == element.stcd) {
      return element;
    }
  }
};
// 获取当前时间和24小时之前
export const getNowTime = (type) => {
  const yy = new Date().getFullYear();
  const MM = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1;
  let dd = '';
  if (type == 1) {
    dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
  }
  if (type == 2) {
    dd =
      new Date(new Date().getTime() - 86400000).getDate() < 10
        ? '0' + new Date(new Date().getTime() - 86400000).getDate()
        : new Date(new Date().getTime() - 86400000).getDate();
  }
  const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours();
  const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
  const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
  return yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss;
};
// 获取某一个传感器数据
export const getSensor = (obj, name) => {
  let activeObj = {};
  obj.sensor.forEach((element) => {
    if (element.sensor_name.indexOf(name) != -1) {
      activeObj = element;
    }
  });
  return activeObj;
};
// 数字处理超出多少个字显示...

export const getNewName = (name, num) => {
  let len = 0;
  let reallen = 0;
  for (let j = 0; j < name.length; j++) {
    if (name.charCodeAt(j) > 127 || name.charCodeAt(j) == 94) {
      len += 2;
    } else {
      len++;
    }
    if (len < num + 1) {
      reallen++;
    }
  }
  if (len > num) {
    name = name.substr(0, reallen) + '···';
  }
  return name;
};
