// Generated by bynkc — do not edit by hand.
// Worker entry point for context `ops`.

import { Ok, Err, type Result, type JsonValue, type BoundaryError, boundaryError } from "../../runtime.js";
import { compose, type Env } from "./compose.js";
import * as handlers from "./handlers.js";

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);
    const path = url.pathname;
    const method = request.method;
    const surface = compose(env);
    try {
      if (path.startsWith("/_bynk/call/")) {
        const servicePath = path.slice("/_bynk/call/".length);
        switch (servicePath) {
          default:
            return new Response("Not found", { status: 404 });
        }
      }

      return new Response("Not Found", { status: 404 });
    } catch {
      return new Response("Internal Server Error", { status: 500 });
    }
  },
  async scheduled(event: { readonly cron: string; readonly scheduledTime: number }, env: Env): Promise<void> {
    const surface = compose(env);
    switch (event.cron) {
      case "*/5 * * * *": {
        const result = await surface.cron_sweeper_0();
        if (result.tag === "Err") console.error("cron */5 * * * * failed", result.error);
        return;
      }
      default:
        return;
    }
  },
  async queue(batch: { readonly queue: string; readonly messages: ReadonlyArray<{ readonly body: unknown; ack(): void; retry(): void }> }, env: Env): Promise<void> {
    const surface = compose(env);
    switch (batch.queue) {
      case "notifications": {
        for (const msg of batch.messages) {
          try {
            const __r = handlers.deserialise_Notification((msg.body as JsonValue), "$");
            if (__r.tag === "Err") { console.error("queue notifications deserialise failed", __r.error); msg.retry(); continue; }
            const result = await surface.queue_notifier_0(__r.value);
            if (result.tag === "Ack") msg.ack();
            else { console.error("queue notifications retry", result.reason); msg.retry(); }
          } catch (e) {
            console.error("queue notifications threw", e); msg.retry();
          }
        }
        return;
      }
      default:
        return;
    }
  },
};
