import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import axios from 'axios';
import { getAreaCode } from '../../city/city.mjs';
import { get } from '../../lodash-mini/get.mjs';
import '@babel/runtime/helpers/typeof';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/toArray';
import { LocationFlag, tencentMapConfig } from '../location-interface.mjs';
import '../../city/data.mjs';

// 用腾讯地图api通过IP获取lbs地址
var TencentMapApiLocation = /*#__PURE__*/function () {
  function TencentMapApiLocation() {
    _classCallCheck(this, TencentMapApiLocation);
  }
  return _createClass(TencentMapApiLocation, [{
    key: "getLocation",
    value: function getLocation(options) {
      return new Promise(function (resolve, reject) {
        var _a;
        if (!options.fetchLBSRequest) {
          reject();
          return;
        }
        (_a = options.fetchLBSRequest) === null || _a === void 0 ? void 0 : _a.call(options).then(function (res) {
          // })
          // post({ url: '/pmdtrpc.commcgi.tipcgi.tipcgi/GetLBS', showMsgToast: false }).then((res) => {
          var result = (res === null || res === void 0 ? void 0 : res.ip_result) || {};
          var location = (result === null || result === void 0 ? void 0 : result.location) || {};
          if ((location === null || location === void 0 ? void 0 : location.lat) && location.lng) {
            var position = {
              lat: location.lat,
              lng: location.lng,
              ip: result.ip,
              type: 'ip',
              adInfo: result.ad_info
            };
            resolve({
              location: position,
              flag: LocationFlag.LocationIpSuccess
            });
          } else {
            reject();
          }
        })["catch"](function (error) {
          console.log('TencentMapApiLocation', error);
          reject();
        });
      });
    }
  }], [{
    key: "getAreaInfoByLatAndLng",
    value:
    // 用经纬度逆解析出省市信息
    function getAreaInfoByLatAndLng(lat, lng) {
      return new Promise(function (resolve, reject) {
        axios.get("https://apis.map.qq.com/ws/geocoder/v1/?location=".concat(lat, ",").concat(lng, "&key=").concat(tencentMapConfig.GEO_KEY_PLUS)).then(function (res) {
          if (res.data) {
            var data = res.data;
            data.city = get(data, 'result.ad_info.city');
            data.province = get(data, 'result.ad_info.province');
            var retList = getAreaCode(data.province, data.city);
            if (retList.length > 1) {
              data.provinceId = retList[0] || 0;
              data.cityId = retList[1] || 0;
            }
            resolve(data);
          } else {
            reject();
          }
        })["catch"](function (error) {
          reject(error);
        });
      });
    }
  }]);
}();

export { TencentMapApiLocation as default };
