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

import { Ok, Err, type Result, type JsonValue, type BoundaryError, boundaryError, matchPath, httpResultToResponse } 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 });
        }
      }

      {
        if (method === "POST" && path === "/items") {
          let __body_json: JsonValue;
          try {
            __body_json = (await request.json()) as JsonValue;
          } catch {
            return new Response(JSON.stringify({ kind: "MalformedJson", details: "Invalid request body" }), { status: 400, headers: { "content-type": "application/json" } });
          }
          const __r_body = handlers.deserialise_CreateItem(__body_json, "$");
          if (__r_body.tag === "Err") return new Response(JSON.stringify(__r_body.error), { status: 400, headers: { "content-type": "application/json" } });
          const body = __r_body.value;
          const result = await surface.http_POST_items(body);
          return httpResultToResponse(result, handlers.serialise_ItemView);
        }
      }
      return new Response("Not Found", { status: 404 });
    } catch {
      return new Response("Internal Server Error", { status: 500 });
    }
  },
};
