const highland = require("highland");
const polyline = require("@mapbox/polyline");
const request = require("request-promise").defaults({
	gzip: true,
	headers: {
		'User-Agent': process.env.fmUserAgent
	}
});


const API_URL = "https://elevation.mapzen.com/height";
const API_KEY = "mapzen-LWPWRB1";
const LIMIT = 500;
const MIN_TIME_BETWEEN_REQUESTS = 600;

const throttle = highland();
throttle.ratelimit(1, MIN_TIME_BETWEEN_REQUESTS).each((func) => {
	func();
});

const elevation = module.exports = {

	_getThrottledSlot() {
		return new Promise((resolve) => {
			throttle.write(resolve);
		});
	},

	getElevationForPoint(point) {
		return elevation.getElevationForPoints([point]).then((points) => (points[0]));
	},

	getElevationForPoints(points) {
		if(points.length == 0)
			return Promise.resolve([ ]);

		let ret = Promise.resolve([ ]);
		for(let i=0; i<points.length; i+=LIMIT) {
			ret = ret.then((heights) => {
				return elevation._getThrottledSlot().then(() => (heights));
			}).then((heights) => {
				let json = {
					encoded_polyline: polyline.encode(points.slice(i, i+LIMIT).map((point) => ([point.lat, point.lon])), 6),
					range: false
				};

				return request.get({
					url: `${API_URL}?json=${encodeURI(JSON.stringify(json))}&api_key=${API_KEY}`,
					json: true
				}).then((res) => (heights.concat(res.height)));
			});
		}
		return ret;
	},

	getAscentDescent(elevations) {
		let ret = {
			ascent: 0,
			descent: 0
		};

		for(let i=1; i<elevations.length; i++) {
			if(elevations[i] > elevations[i-1])
				ret.ascent += elevations[i] - elevations[i-1];
			else
				ret.descent += elevations[i-1] - elevations[i];
		}

		return ret;
	}

};

@@ -750,16 +750,71 @@
 ts) %7B%0A%09%09
+return Promise.resolve(points.map(() =%3E (null)));%0A%0A%09%09/*
 if(point
@@ -1415,16 +1415,18 @@
 urn ret;
+*/
 %0A%09%7D,%0A%0A%09g
@@ -1455,16 +1455,124 @@
 ions) %7B%0A
+%09%09if(!elevations.some((ele) =%3E (ele != null))) %7B%0A%09%09%09return %7B%0A%09%09%09%09ascent: null,%0A%09%09%09%09descent: null%0A%09%09%09%7D;%0A%09%09%7D%0A%0A
 %09%09let re

Disable elevation, as the limit is reached