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

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 { Ledger } 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 "place": {
            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_id = (typeof argsObj["id"] === "string" ? Ok(argsObj["id"]) : Err({ kind: "StructuralMismatch", path: "$.id", expected: "string", actual: typeof argsObj["id"] }) as Result<any, BoundaryError>);
            if (__r_id.tag === "Err") return new Response(JSON.stringify(__r_id.error), { status: 400, headers: { "content-type": "application/json" } });
            const id = __r_id.value;
            const __r_cents = (typeof argsObj["cents"] === "number" && Number.isInteger(argsObj["cents"]) ? Ok(argsObj["cents"]) : Err({ kind: "StructuralMismatch", path: "$.cents", expected: "integer", actual: String(argsObj["cents"]) }) as Result<any, BoundaryError>);
            if (__r_cents.tag === "Err") return new Response(JSON.stringify(__r_cents.error), { status: 400, headers: { "content-type": "application/json" } });
            const cents = __r_cents.value;
            const result = await surface.place(id, cents);
            const body = handlers.serialise_Result_Int_OrderError(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 });
    }
  },
};
