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

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) {
          case "allKeys": {
            const args = await request.json() as JsonValue;
            const __r_unused = (typeof args === "number" && Number.isInteger(args) ? Ok(args) : Err({ kind: "StructuralMismatch", path: "$", expected: "integer", actual: String(args) }) as Result<any, BoundaryError>);
            if (__r_unused.tag === "Err") return new Response(JSON.stringify(__r_unused.error), { status: 400, headers: { "content-type": "application/json" } });
            const unused = __r_unused.value;
            const result = await surface.allKeys(unused);
            const body = handlers.serialise_Result_List_String_Unit(result);
            return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
          }
          case "cache": {
            const args = await request.json() as JsonValue;
            if (typeof args !== "object" || args === null || Array.isArray(args)) return new Response(JSON.stringify({ kind: "StructuralMismatch", path: "$", expected: "object", actual: typeof args }), { status: 400, headers: { "content-type": "application/json" } });
            const argsObj = args as { [k: string]: JsonValue };
            const __r_key = (typeof argsObj["key"] === "string" ? Ok(argsObj["key"]) : Err({ kind: "StructuralMismatch", path: "$.key", expected: "string", actual: typeof argsObj["key"] }) as Result<any, BoundaryError>);
            if (__r_key.tag === "Err") return new Response(JSON.stringify(__r_key.error), { status: 400, headers: { "content-type": "application/json" } });
            const key = __r_key.value;
            const __r_e = handlers.deserialise_Entry(argsObj["e"], "$.e");
            if (__r_e.tag === "Err") return new Response(JSON.stringify(__r_e.error), { status: 400, headers: { "content-type": "application/json" } });
            const e = __r_e.value;
            const result = await surface.cache(key, e);
            const body = handlers.serialise_Option_Entry(result);
            return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
          }
          case "scan": {
            const args = await request.json() as JsonValue;
            const __r_prefix = (typeof args === "string" ? Ok(args) : Err({ kind: "StructuralMismatch", path: "$", expected: "string", actual: typeof args }) as Result<any, BoundaryError>);
            if (__r_prefix.tag === "Err") return new Response(JSON.stringify(__r_prefix.error), { status: 400, headers: { "content-type": "application/json" } });
            const prefix = __r_prefix.value;
            const result = await surface.scan(prefix);
            const body = handlers.serialise_Result_List_String_Unit(result);
            return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
          }
          default:
            return new Response("Not found", { status: 404 });
        }
      }

      return new Response("Not Found", { status: 404 });
    } catch {
      return new Response("Internal Server Error", { status: 500 });
    }
  },
};
