const cacheManager = require("../eirtubeMods/cache-manager")

module.exports = [
	{
		route: "/getDeArrow", methods: ["GET"], code: async ({req, url, res}) => {
			let videoID = url.searchParams.get("v")
			if (videoID) {
				const segs = videoID.split("/")
				videoID = segs[segs.length - 1]
			}

			// Check exists
			let data = cacheManager.read("dearrow")[videoID]
			if (!data)
				return {
					statusCode: 404,
					contentType: "text/html; charset=UTF-8",
					content: "Not found"
				}
			else if (data.loading) {
				await new Promise(resolve => {
					const int = setInterval(() => {
						data = cacheManager.read("dearrow")[videoID]
						if (!data.loading) {
							clearInterval(int)
							resolve()
						}
					}, 1000)
				})
			}
			return {
				statusCode: 200,
				contentType: "application/json",
				content: data,
			}
		}
	}
]
