import { Router } from "@fastly/expressly";
import { getGeolocationForIpAddress } from "fastly:geolocation";

const router = new Router();

router.get("(.*)", async (req, res) => {
  // setting date/time variables
  let dt = Date.UTC(2014, 10, 28, 12, 0, 9); //monthIndex 10 is Nov
  let fixedDT = dt + 9 * 3600 * 1000;

  // you can also take offset from timezone of connected user
  let geo;
  try {
    geo = getGeolocationForIpAddress(req.ip);
  } catch (error) {
    console.error(error);
  }

  let fastly_offset = geo.utc_offset;
  fixedDT =
    dt + (fastly_offset / 100 + (fastly_offset % 100) / 60) * 3600 * 1000;

  // setting time by time string parsing
  // method 1
  let tt = Date.parse("2014-11-28T12:00:09Z");
  if (tt != dt) {
    console.log("Date&time not equal");
  }
  tt = Date.parse("2014-11-28T21:00:09+09:00");
  if (tt != dt) {
    console.log("Date&time not equal");
  }

  // method 2
  tt = new Date("2014-11-28T12:00:09Z");
  if (tt[Symbol.toPrimitive]("number") != dt) {
    console.log("Date&time not equal");
  }

  // time formatting
  console.log(tt.toString());
  console.log(tt.toDateString());
  console.log(tt.toTimeString());
  console.log(tt.toISOString());
  console.log(tt.toUTCString());

  // send text message to client
  res.text("hello");
});

router.listen();