/// <reference types="@fastly/js-compute" />

import { env } from "fastly:env";

function handler(event) {

  var now = new Date();

  //FASTLY_POP is not available in fiddle
  //Health Check is not available in fiddle
  let respBody = JSON.stringify({
    fastly_pop: env("FASTLY_POP") ? env("FASTLY_POP") : "TYO",
    origin0: (Backend.health('origin_0') == 'healthy') ? true : false,
    origin1: (Backend.health('origin_1') == 'healthy') ? true : false,
    originUnhealthy: (Backend.health('F_origin_unhealthy') == 'healthy') ? true : false,
    timestamp: now,
  })

  return new Response(respBody, {
    statsus: 200,
    headers: {
      "Content-Type": "application/json",
    },
  })
}

// eslint-disable-next-line no-restricted-globals
addEventListener("fetch", (event) => event.respondWith(handler(event)))