import amapSDK from './sdk';

export const AMAP_KEY = {
  service: '3be0a9567a794d2690d378476f057a6f',
  web: '29259f677b42900507074fd92ecb628a',
}

export const sdk = amapSDK(AMAP_KEY)

export const POI_TYPES_KETY = {
  Transport: 'Transport',
  Mall: 'Mall',
  Edu: 'Edu',
  Hospital: 'Hospital',
  Bank: 'Bank',
  Restaurant: 'Restaurant',
}

// key 值会被拿到 building 表中 组合为字段名
export const POI_TYPES = [
  {
    key: POI_TYPES_KETY.Transport,
    label: '交通',
    types: ['150100', '150200', '150300', '150400', '150500', '150600'],
    data: [],
  },
  {
    key: POI_TYPES_KETY.Mall,
    label: '商业',
    types: ['060100', '060200', '060300', '060400', '060500', '060600',
            '060700', '060800', '060900', '061000', '061100', '061200',
            '061300', '061400', '080100', '080200', '080300', '080400',
            '080500', '080600'],
    data: [],
  },
  {
    key: POI_TYPES_KETY.Edu,
    label: '学校',
    types: ['141200'],
    data: [],
  },
  {
    key: POI_TYPES_KETY.Hospital,
    label: '医院',
    types: ['090100', '090200', '090300', '090400', '090500', '090600', '090700'],
    data: [],
  },
  {
    key: POI_TYPES_KETY.Bank,
    label: '银行',
    types: ['160100'],
    data: [],
  },
  {
    key: POI_TYPES_KETY.Restaurant,
    label: '餐饮',
    types: ['050100', '050200', '050300', '050400', '050500', '050600',
            '050700', '050800', '050900'],
    data: [],
  },
]

/**
 * 获取周边数据
 * @param {查询参数} params 
 */
export function getPoiData (params) {
  return new Promise((resolve, reject) => {
      const { type, location } = params
      if (!type) {
          // console.err('未传输类型types值')
          reject(new Error('未传输类型 type 值'))
          return
      }
      if (!location) {
          // console.err('未传输传输中心坐标location值')
          reject(new Error('未传输传输中心坐标 location 值'))
          return
      }

      const poiType = POI_TYPES.filter((it) => it.key === type)[0];
      if (!poiType) {
        reject(new Error('周边 type 类型不正确'))
        return
      }

      sdk.getPlaceAround({
        ...params,
        types: poiType.types.join('|'),
      }).then(resolve)
      .catch(reject);
  })
}