const radius = 6378245;
const ee = 0.006693421622965943;
const eSquared = ee * ee;
function wgs84ToGcj02(lng, lat) {
  let dLat = transformLat(lng - 105, lat - 35);
  let dLng = transformLng(lng - 105, lat - 35);
  let radLat = lat / 180 * Math.PI;
  let magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
  let sqrtMagic = Math.sqrt(magic);
  dLat = dLat * 180 / (radius * (1 - eSquared) / (magic * sqrtMagic) * Math.PI);
  dLng = dLng * 180 / (radius / sqrtMagic * Math.cos(radLat) * Math.PI);
  return {
    lng: lng + dLng,
    lat: lat + dLat
  };
}
function gcj02ToBd09(lng, lat) {
  const z = Math.sqrt(lng * lng + lat * lat) + 2e-5 * Math.sin(lat * Math.PI);
  const theta = Math.atan2(lat, lng) + 3e-6 * Math.cos(lng * Math.PI);
  const bdLng = z * Math.cos(theta) + 65e-4;
  const bdLat = z * Math.sin(theta) + 6e-3;
  return {
    lng: bdLng,
    lat: bdLat
  };
}
function wgs84ToBd09(_lng, _lat) {
  const { lng, lat } = wgs84ToGcj02(_lng, _lat);
  return gcj02ToBd09(lng, lat);
}
const transformLat = (x, y) => {
  let ret = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  ret += (20 * Math.sin(6 * x * Math.PI) + 20 * Math.sin(2 * x * Math.PI)) * 2 / 3;
  ret += (20 * Math.sin(y * Math.PI) + 40 * Math.sin(y / 3 * Math.PI)) * 2 / 3;
  ret += (160 * Math.sin(y / 12 * Math.PI) + 320 * Math.sin(y * Math.PI / 30)) * 2 / 3;
  return ret;
};
const transformLng = (x, y) => {
  let ret = 300 + x + 2 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  ret += (20 * Math.sin(6 * x * Math.PI) + 20 * Math.sin(2 * x * Math.PI)) * 2 / 3;
  ret += (20 * Math.sin(x * Math.PI) + 40 * Math.sin(x / 3 * Math.PI)) * 2 / 3;
  ret += (150 * Math.sin(x / 12 * Math.PI) + 300 * Math.sin(x / 30 * Math.PI)) * 2 / 3;
  return ret;
};

export { gcj02ToBd09, wgs84ToBd09, wgs84ToGcj02 };
