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

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 "quotes": {
            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_sku = (typeof argsObj["sku"] === "string" ? Ok(argsObj["sku"]) : Err({ kind: "StructuralMismatch", path: "$.sku", expected: "string", actual: typeof argsObj["sku"] }) as Result<any, BoundaryError>);
            if (__r_sku.tag === "Err") return new Response(JSON.stringify(__r_sku.error), { status: 400, headers: { "content-type": "application/json" } });
            const sku = __r_sku.value;
            const __r_price = (typeof argsObj["price"] === "number" && Number.isFinite(argsObj["price"]) ? Ok(argsObj["price"]) : Err({ kind: "StructuralMismatch", path: "$.price", expected: "finite number", actual: String(argsObj["price"]) }) as Result<any, BoundaryError>);
            if (__r_price.tag === "Err") return new Response(JSON.stringify(__r_price.error), { status: 400, headers: { "content-type": "application/json" } });
            const price = __r_price.value;
            const result = await surface.quotes(sku, price);
            const body = handlers.serialise_Result_Quote_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 });
    }
  },
};
