import coordsToFit from "./../coordsToFit";

// 内库使用-start
import LocaleUtil from "./../../../../utils/LocaleUtil";
// 内库使用-end

/* 测试使用-start
import { LocaleUtil } from 'seedsui-react'
测试使用-start */

// 搜索附近, keyword:搜索关键词
function bmapQueryNearby(_ref) {
  var map = _ref.map,
    keyword = _ref.keyword,
    longitude = _ref.longitude,
    latitude = _ref.latitude,
    type = _ref.type,
    radius = _ref.radius;
  if (!(map !== null && map !== void 0 && map.currentMap) || !longitude || !latitude || !type) {
    return null;
  }
  return new Promise(function (resolve) {
    // 中国转bd09再搜索
    var centerCoord = coordsToFit({
      longitude: longitude,
      latitude: latitude,
      type: type,
      inChinaTo: 'bd09',
      outChinaTo: 'wgs84'
    });
    var centerPoint = new window.BMap.Point(centerCoord.longitude, centerCoord.latitude);

    // 定位到当前位置
    map.currentMap.panTo(centerPoint);

    // 创建本地搜索对象
    var local = new window.BMap.LocalSearch(map.currentMap, {
      pageCapacity: 100,
      onSearchComplete: function onSearchComplete(results) {
        // 判断状态是否正确
        if (local.getStatus() === window.BMAP_STATUS_SUCCESS) {
          var list = [];
          for (var i = 0; i < results.getCurrentNumPois(); i++) {
            var item = results.getPoi(i);
            if (!item.title && !item.address) continue;
            var _longitude = item.point.lng;
            var _latitude = item.point.lat;
            var _type = centerCoord.isInChina ? 'bd09' : 'wgs84';
            list.push({
              name: item.title,
              address: item.address,
              longitude: _longitude,
              latitude: _latitude,
              type: _type
            });
          }
          resolve(list);
        } else {
          resolve(LocaleUtil.locale('查询失败', 'SeedsUI_query_failed'));
        }
      }
    });

    // 搜索附近
    if (centerPoint && radius) {
      local.searchNearby(keyword, centerPoint, radius);
    }
    // 搜索全部
    else {
      local.search(keyword);
    }
  });
}
export default bmapQueryNearby;