import { email } from "https://esm.town/v/std/email?v=9";
import { fetch } from "https://esm.town/v/std/fetch";
export async function reminders(interval: Interval) {
function* walk(node, path = []) {
yield node;
if (node && Array.isArray(node.value)) {
const arr = node.value;
for (let i = 0; i < arr.length; i++) {
yield* walk(arr[i], [...path, i]);
}
}
}
const comingUp = [];
const response = await fetch("https://markwhen.com/rob.json");
const mw = await response.json();
const events = mw.timelines[0].events;
const now = Date.now();
for (const node of walk(events)) {
if (!node.value.dateRangeIso) {
continue;
}
const from = Date.parse(node.value.dateRangeIso.fromDateTimeIso);
const diff = from - now;
if (diff > 0 && diff < 60 * 60 * 1000) {
comingUp.push(node.value);
}
}
if (comingUp.length) {
await email({
text: comingUp.map((e) => e.eventString).join("\n\n"),
subject: `${comingUp.length} event${comingUp.length > 1 ? "s" : ""} coming up`,
});
}
}