// Generated by bynkc — do not edit by hand.
// context commerce.orders

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "../runtime.js";

import { AuthId } from "./payment.js";

import * as commerce_payment from "./payment.js";

export type OrderError =
    { readonly tag: "NotPlaced" };

export const OrderError = {
  NotPlaced: { tag: "NotPlaced" } as OrderError,
};

export const place = {
  async call(amount: number, deps: { surface: { Payment: ReturnType<typeof commerce_payment.makeSurface> } }): Promise<Result<AuthId, OrderError>> {
    const result = await deps.surface.Payment.authorise(amount);
    switch (result.tag) {
      case "Ok": {
        const id = result.value;
        return Ok(id);
      }
      case "Err": {
        return Err(OrderError.NotPlaced);
      }
    }
    throw new Error("non-exhaustive match");
  },
};

export interface CommerceOrdersDeps {
  readonly surface: { Payment: ReturnType<typeof commerce_payment.makeSurface> };
}

export function makeSurface(deps: CommerceOrdersDeps) {
  return {
    async place(amount: number): Promise<Result<AuthId, OrderError>> {
      return place.call(amount, deps);
    },
  };
}

