import request from '../../../../../utils/request';
import apis from '../../../../../services/apis';

const KEY = '14f05ce59c26364fd0674014dc0d8b1b'
// const KEY = 'c9fc664a9030aed2ec2183508c2ca476'

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

// key 值会被拿到 building 表中 组合为字段名
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: [],
    },
  ]

/**
 * 获取 POI 类型
 * @param {*} keyType 
 */
const getPOIType = keyType => {
    return POI_TYPES.filter(item => item.key === keyType)
}

/**
 * 获取这个 keyType 类型的所有 types
 * @param {类型的key值} keyType 
 */
const getTypes = keyType => {
  const typesArr = getPOIType(keyType)
  return typesArr[0].types.join('|')
}

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

        request({ ...apis.amap.webService, params: { ...params, key: KEY, types: getTypes(types), location } }).then(data => {
            // console.log(data)
            resolve(data)
        }).catch(err => {
            reject(err)
        })
    })
}

export { POI_TYPES_KETY, getAroundData, getPOIType, POI_TYPES }
