import "core-js/modules/es.promise.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import qs from 'query-string';
import { jsonp } from '@wii/jsonp';
export const getCenterByIP = key => {
  const params = {
    key,
    output: 'jsonp'
  };
  return new Promise((resolve, reject) => {
    jsonp(`https://apis.map.qq.com/ws/location/v1/ip?${qs.stringify(params)}`, (err, data) => {
      if (err) {
        reject(err);
      } else if (data) {
        resolve(data);
      }
    });
  });
};
export function appendScript(key, callback) {
  const params = {
    v: '1.exp',
    key,
    callback
  };
  const src = `https://map.qq.com/api/gljs?${qs.stringify(params)}`;
  const ele = document.querySelector(`script[src='${src}']`);
  if (ele) {
    ele.remove();
  }
  const script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = src;
  document.body.appendChild(script);
  return script;
}
export const getPosition = () => {
  return new Promise((resolve, reject) => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(position => {
        const latitude = position.coords.latitude;
        const longitude = position.coords.longitude;
        const data = {
          lat: latitude,
          lng: longitude
        };
        resolve(data);
      }, e => {
        reject(e.message);
      }, {
        timeout: 5000
      });
    } else {
      // eslint-disable-next-line prefer-promise-reject-errors
      reject('你的浏览器不支持当前地理位置信息获取');
    }
  });
};