// Generated by bynkc — do not edit by hand.
// test target: commerce.orders

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "../runtime.js";
import * as commerce_orders from "./../commerce/orders.js";
import * as commerce_payment from "./../commerce/payment.js";

class AssertionError extends Error {
  constructor(public location: string, public start: number, public end: number) {
    super(`assertion failed at ${location}`);
  }
}
function __bynkAssertionFailure(location: string, start: number, end: number) {
  return new AssertionError(location, start, end);
}
function __bynkAssert(cond: boolean, location: string, start: number, end: number): void {
  if (!cond) { throw __bynkAssertionFailure(location, start, end); }
}

class MockPayment {
  async authorise(amount: number): Promise<Result<commerce_payment.AuthId, commerce_payment.PaymentError>> {
    const { AuthId, PaymentError } = commerce_payment as any;
    return (amount > 50000 ? Err(PaymentError.Declined) : Ok(AuthId.unsafe("MOCK-AUTH-12345678")));
  }
}

function makeTestDeps() {
  return { surface: { Payment: new MockPayment() } };
}

async function test_small_order_places_successfully() {
  try {
    const deps = makeTestDeps();
    const { OrderError, place } = commerce_orders as any;
    const { AuthId, PaymentError } = commerce_payment as any;
    const Payment = (deps as any).surface?.Payment;
    const result = await place.call(5000, deps);
    if (!(result.tag === "Ok")) { throw __bynkAssertionFailure("offset 346", 346, 361); }
    return { pass: true };
  } catch (e) {
    if (e instanceof AssertionError) {
      return { pass: false, error: { message: e.message, location: e.location } };
    }
    return { pass: false, error: { message: String(e), location: "unknown" } };
  }
}

async function test_large_order_is_declined() {
  try {
    const deps = makeTestDeps();
    const { OrderError, place } = commerce_orders as any;
    const { AuthId, PaymentError } = commerce_payment as any;
    const Payment = (deps as any).surface?.Payment;
    const result = await place.call(100000, deps);
    void (((__d) => {
        switch (__d.tag) {
          case "Err": {
            const PaymentDeclined = __d.error;
            if (!(true)) { throw __bynkAssertionFailure("offset 497", 497, 501); }
            return undefined;
          }
          default: {
            if (!(false)) { throw __bynkAssertionFailure("offset 543", 543, 548); }
            return undefined;
          }
        }
        throw new Error("non-exhaustive match");
      })(result));
    return { pass: true };
  } catch (e) {
    if (e instanceof AssertionError) {
      return { pass: false, error: { message: e.message, location: e.location } };
    }
    return { pass: false, error: { message: String(e), location: "unknown" } };
  }
}

export async function run() {
  const results = [];
  results.push({ name: "small order places successfully", ...(await test_small_order_places_successfully()) });
  results.push({ name: "large order is declined", ...(await test_large_order_is_declined()) });
  return results;
}
