// 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";

import { CustomerId as __CommonsCustomerId } from "./identifiers.js";
import { Money as __CommonsMoney } from "./money.js";

export type CustomerId = __CommonsCustomerId & { readonly __ctxBrand: "commerce.payment" };
export const CustomerId = {
  of(value: string): Result<CustomerId, ValidationError> { return __CommonsCustomerId.of(value) as unknown as Result<CustomerId, ValidationError>; },
  unsafe(value: string): CustomerId { return __CommonsCustomerId.unsafe(value) as unknown as CustomerId; },
};
export type Money = __CommonsMoney & { readonly __ctxBrand: "commerce.payment" };

/**
 * Payment context. Authorises monetary transactions and records auth IDs.
 */

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

export const AuthId = {
  of(value: string): Result<AuthId, ValidationError> {
    if (!new RegExp("^" + "AUTH-[0-9]{8}" + "$").test(value)) {
      return Err({ field: "AuthId", message: "must match /AUTH-[0-9]{8}/", value });
    }
    return Ok(value as AuthId);
  },
  unsafe(value: string): AuthId {
    return value as AuthId;
  },
};

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

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

/**
 * A payment authorisation. Internal record paired with the public AuthId.
 */
export interface Authorisation {
  readonly id: AuthId;
  readonly amount: Money;
  readonly customer: CustomerId;
}

export const Authorisation = {
};

