type profile = 'driving-traffic' |'driving'|'walking' |'cycling'
const token = 'pk.eyJ1IjoiYWxleDkwMTIiLCJhIjoiY2xtOGw4NHdkMGFndTNjcnFkeWZncGc2dyJ9.lVrAdPHE0Dg5zoWFidfj4Q'

class MapService {
    static queryPlaceInfoAsync = async (keyword: string): Promise<{ label: string, value: string, center: number[] }[]> => {
        let url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${keyword}.json?types=place&access_token=${token}`

        const response = await fetch(url)
        return new Promise(async (resolve) => {
            if (response.ok) {
                let data = await response.json()
                if (data.features && data.features.length > 0) {
                    let result: { label: string, value: string, center: number[] }[] = []
                    data.features.forEach((x: any) => {
                        result.push({
                            label: x.matching_text?`${x.matching_text}(${x.text})`:x.text,
                            value: x.id,
                            center: x.center
                        })
                    })
                    resolve(result)
                }
            }
            resolve([])
        })
    }
    
    static queryDirectionsInfoAsync = async (profileInfo:profile,start:number[],end:number[])=>{
        let url = `https://api.mapbox.com/directions/v5/mapbox/${profileInfo}/${start.join(',')};${end.join(',')}?steps=true&language=zh-CN&access_token=${token}`
        console.log(url)
        const response = await fetch(url)

        if(response.ok){
            const data = await response.json()
            return data
        }

        return null
    }
}

export default MapService