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

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

export type AuthId = string & { readonly __brand: "commerce.payment.AuthId" };

export const AuthId = {
  of(value: string): Result<AuthId, ValidationError> {
    return Ok(value as AuthId);
  },
  unsafe(value: string): AuthId {
    return value as AuthId;
  },
};

export type PaymentError =
    { readonly tag: "Declined" }
  | { readonly tag: "InsufficientFunds" };

export const PaymentError = {
  Declined: { tag: "Declined" } as PaymentError,
  InsufficientFunds: { tag: "InsufficientFunds" } as PaymentError,
};

export interface Logger {
  log(msg: string): Promise<void>;
}

export const LoggerToken: unique symbol = Symbol("Logger");

export class ConsoleLogger implements Logger {
  async log(msg: string): Promise<void> {
    return undefined;
  }
}

export const ConsoleLoggerProvider = { token: LoggerToken, factory: () => new ConsoleLogger() };

export const authorise = {
  async call(amount: number, deps: { Logger: Logger }): Promise<Result<AuthId, PaymentError>> {
    const __r0 = await deps.Logger.log("authorise");
    return (amount === 0 ? Err(PaymentError.Declined) : (amount > 1000000 ? Err(PaymentError.InsufficientFunds) : Ok(AuthId.unsafe("AUTH-DEFAULT"))));
  },
};

export interface CommercePaymentDeps {
  readonly Logger: Logger;
}

export function makeSurface(deps: CommercePaymentDeps) {
  return {
    async authorise(amount: number): Promise<Result<AuthId, PaymentError>> {
      return authorise.call(amount, deps);
    },
  };
}

