"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.appendScript = appendScript;
exports.getPosition = exports.getCenterByIP = void 0;
require("core-js/modules/es.promise.js");
var _queryString = _interopRequireDefault(require("query-string"));
var _jsonp = require("@wii/jsonp");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

const getCenterByIP = key => {
  const params = {
    key,
    output: 'jsonp'
  };
  return new Promise((resolve, reject) => {
    (0, _jsonp.jsonp)(`https://apis.map.qq.com/ws/location/v1/ip?${_queryString.default.stringify(params)}`, (err, data) => {
      if (err) {
        reject(err);
      } else if (data) {
        resolve(data);
      }
    });
  });
};
exports.getCenterByIP = getCenterByIP;
function appendScript(key, callback) {
  const params = {
    v: '1.exp',
    key,
    callback
  };
  const src = `https://map.qq.com/api/gljs?${_queryString.default.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;
}
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('你的浏览器不支持当前地理位置信息获取');
    }
  });
};
exports.getPosition = getPosition;