"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.regeoSearch = exports.poiSearch = exports.geoSearch = void 0;
var _axios = _interopRequireDefault(require("axios"));
var _mapTool = _interopRequireDefault(require("./mapTool"));
var _getSystemInfo = _interopRequireDefault(require("./getSystemInfo"));
var _excluded = ["location"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var GD_KEY = '2dd5476719ea3378dc06ef93b3d4aba7';
var CITY = (0, _getSystemInfo["default"])('rootDivisionName') || '中国';

// 根据经纬度查询
var regeoSearch = exports.regeoSearch = function regeoSearch(_ref) {
  var location = _ref.location,
    val = _objectWithoutProperties(_ref, _excluded);
  var lngLats = _mapTool["default"].wgs84togcj02(location[0], location[1]);
  var params = _objectSpread({
    key: GD_KEY,
    location: lngLats.join(','),
    type: 'geocode'
  }, val);
  return _axios["default"].get('https://restapi.amap.com/v3/geocode/regeo', {
    params: params
  }).then(function (res) {
    return res.data;
  }).then(function (data) {
    var info = data.info,
      status = data.status;
    var result = {};
    if (info === 'OK' && status === '1' && data.regeocode) {
      var _aois$;
      var _data$regeocode = data.regeocode,
        formatted_address = _data$regeocode.formatted_address,
        aois = _data$regeocode.aois;
      if (aois !== null && aois !== void 0 && aois.length) {
        aois.sort(function (a, b) {
          return Number(a.distance) - Number(b.distance);
        });
      }
      result = _objectSpread(_objectSpread({}, data.result), {}, {
        name: (aois === null || aois === void 0 || (_aois$ = aois[0]) === null || _aois$ === void 0 ? void 0 : _aois$.name) || formatted_address,
        address: formatted_address,
        geometryInfo: {
          type: 'point',
          lngLats: location.join(','),
          coordType: 'wgs84'
        }
      });
    }
    return result;
  });
};

// 根据地址查询
var geoSearch = exports.geoSearch = function geoSearch(address) {
  var params = {
    key: GD_KEY,
    address: address,
    city: CITY
  };
  return _axios["default"].get('https://restapi.amap.com/v3/geocode/geo', {
    params: params
  }).then(function (res) {
    return res.data;
  }).then(function (data) {
    var result = {};
    if (data.status === '0' && data.result) {
      result = data.result;
    }
    return result;
  });
};

// 根据poi 兴趣点查询
var poiSearch = exports.poiSearch = function poiSearch(val) {
  var params = _objectSpread({
    key: GD_KEY,
    region: CITY,
    keywords: '',
    page_size: 25
  }, val);
  return _axios["default"].get('https://restapi.amap.com/v5/place/text', {
    params: params
  }).then(function (res) {
    return res.data;
  }).then(function (res) {
    var list = [];
    var pois = res.pois,
      info = res.info,
      status = res.status;
    if (info === 'OK' && status === '1' && pois !== null && pois !== void 0 && pois.length) {
      pois.map(function (item) {
        var location = item.location,
          name = item.name,
          id = item.id;
        if (location) {
          location = location.split(',');
          var lngLats = _mapTool["default"].gcj02towgs84(location[0] * 1, location[1] * 1);
          list.push(_objectSpread(_objectSpread({}, item), {}, {
            label: name,
            value: id,
            index: list.length + 1,
            geometryInfo: {
              type: 'point',
              lngLats: lngLats.join(','),
              coordType: 'wgs84'
            }
          }));
        }
      });
    }
    return list;
  })["catch"](function () {
    return [];
  });
};
//# sourceMappingURL=search.js.map
