interface Env {
    ADDR: string,
    ign: KVNamespace,
}

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext) {
        let ref = request.headers.get("Referer")
        console.log(ref)
        if (request.method == "GET" || !(ref?.startsWith(env.ADDR))) {
            // return new Response("", { status: 400 })
        }
        let coords: { lat: number, lng: number } = await request.json()
        let u = `https://re.jrc.ec.europa.eu/api/seriescalc?lat=${coords.lat}&lon=${coords.lng}&startyear=2016&pvcalculation=1&peakpower=1&loss=14&outputformat=json`
        let r = await fetch(u)
        let rr = await r.json()
        console.log(rr)
        return new Response(JSON.stringify(rr), { headers: [
            ["Access-Control-Allow-Origin", "*"]
        ]})
    }
}