import { fetch } from "https://esm.town/v/std/fetch";
export const nearestOpenStation = async ({ lon, lat }) => {
lon = +lon;
lat = +lat;
function dist(station) {
return Math.sqrt(
Math.pow(station.lat - lat, 2) + Math.pow(station.lon - lon, 2)
);
}
const {
data: { stations: stationInfo },
} = await fetch(
"https://gbfs.citibikenyc.com/gbfs/en/station_status.json"
).then((r) => r.json());
const stationInfoMap = new Map(
stationInfo.map((info) => [info.station_id, info])
);
const {
data: { stations },
} = await fetch(
"https://gbfs.citibikenyc.com/gbfs/en/station_information.json"
).then((r) => r.json());
const nearest = stations
.map((station) => {
const info = stationInfoMap.get(station.station_id);
return {
...station,
info,
};
})
.filter((station) => {
return station.info.num_docks_available > 0;
})
.slice()
.sort((a, b) => {
return dist(a) - dist(b);
});
const nearestStation = nearest[0];
if (nearestStation) {
return nearestStation.name;
}
return "";
};