// 将后端返回的字段转换成树组件的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,');
}
