// 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: "PaymentDeclined" }
  | { readonly tag: "PaymentInsufficient" };

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

export const place = {
  async call(amount: number, deps: { surface: { Payment: ReturnType<typeof commerce_payment.makeSurface> } }): Promise<Result<AuthId, OrderError>> {
    const auth = await deps.surface.Payment.authorise(amount);
    switch (auth.tag) {
      case "Ok": {
        const id = auth.value;
        return Ok(id);
      }
      case "Err": {
        const error = auth.error;
        return ((__d) => {
    switch (__d.tag) {
      case "Declined": {
        return Err(OrderError.PaymentDeclined);
      }
      case "InsufficientFunds": {
        return Err(OrderError.PaymentInsufficient);
      }
    }
    throw new Error("non-exhaustive match");
  })(error);
      }
    }
    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);
    },
  };
}

