'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var axios = require('axios');
var city_city = require('../../city/city.js');
var location_locationInterface = require('../location-interface.js');
var lodashMini_get = require('../../lodash-mini/get.js');
require('../../city/data.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);

// 用腾讯地图api通过IP获取lbs地址
var TencentMapApiLocation = /*#__PURE__*/function () {
  function TencentMapApiLocation() {
    _classCallCheck__default["default"](this, TencentMapApiLocation);
  }
  return _createClass__default["default"](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: location_locationInterface.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__default["default"].get("https://apis.map.qq.com/ws/geocoder/v1/?location=".concat(lat, ",").concat(lng, "&key=").concat(location_locationInterface.tencentMapConfig.GEO_KEY_PLUS)).then(function (res) {
          if (res.data) {
            var data = res.data;
            data.city = lodashMini_get.get(data, 'result.ad_info.city');
            data.province = lodashMini_get.get(data, 'result.ad_info.province');
            var retList = city_city.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);
        });
      });
    }
  }]);
}();

exports["default"] = TencentMapApiLocation;
