
import jsonp from 'jsonp'
const BASEURL = 'https://apis.map.qq.com/ws/'

function request (url, data={}) {
  data.key = 'P47BZ-KX7W6-3ULSW-EJYUH-MC2GF-AOFC3&output=jsonp'
  url = BASEURL + url + '?'
  Object.keys(data).forEach(item => url += `${item}=${data[item]}&`)
  return new Promise((resolve, reject) => {
    jsonp(url, null, (err, data) => {
      if (err) {
        reject(err)
        return
      }
      resolve(data)
    })
  })
}
export function getDistrictList () {
  return request(`district/v1/list`, {})
}

export function searchPlaceByKeyword ({keyword, boundary=`region(珠海,0)`}) {
  return request('place/v1/search', {keyword, boundary})
}

export function getLocationByIp () {
  return request('location/v1/ip')
}

export function getLocationByGeocoder (location) {
  return request('geocoder/v1', { location })
}

export function getDistrict (keyword) {
  return request('district/v1/search', { keyword })
}