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

import { Ok, Err, Some, None, type Result, type Option, type ValidationError, type JsonValue, type BoundaryError, type ServiceBinding, callService, boundaryError } from "../../runtime.js";

export type AuthId = string & { readonly __brand: "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: "GatewayDown" };

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

export interface Logger {
  info(message: string): Promise<void>;
}

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

export interface Http {
  post(path: string): Promise<Result<AuthId, PaymentError>>;
}

export const HttpToken: unique symbol = Symbol("Http");

export interface Payments {
  authorise(amount: number): Promise<Result<AuthId, PaymentError>>;
}

export const PaymentsToken: unique symbol = Symbol("Payments");

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

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

export class FetchHttp implements Http {
  async post(path: string): Promise<Result<AuthId, PaymentError>> {
    return Ok(AuthId.unsafe("AUTH-12345678"));
  }
}

export const FetchHttpProvider = { token: HttpToken, factory: () => new FetchHttp() };

export class StripePayments implements Payments {
  constructor(private deps: { Http: Http; Logger: Logger }) {}
  async authorise(amount: number): Promise<Result<AuthId, PaymentError>> {
    const __r0 = await this.deps.Logger.info("charging");
    const result = await this.deps.Http.post("/charge");
    return result;
  }
}

export const StripePaymentsProvider = { token: PaymentsToken, factory: (deps: any) => new StripePayments(deps) };

export const authorise = {
  async call(amount: number, deps: { Payments: Payments }): Promise<Result<AuthId, PaymentError>> {
    const r = await deps.Payments.authorise(amount);
    return r;
  },
};

export function serialise_AuthId(value: AuthId): JsonValue {
  return value as unknown as string;
}

export function deserialise_AuthId(json: JsonValue, path: string = "$"): Result<AuthId, BoundaryError> {
  if (typeof json !== "string") {
    return Err({ kind: "StructuralMismatch", path, expected: "string", actual: typeof json });
  }
  const validated = (typeof (AuthId as any).of === "function")
    ? (AuthId as any).of(json)
    : Ok(json as unknown as AuthId);
  if (validated.tag === "Err") {
    return Err({ kind: "RefinementViolation", path, violation: validated.error });
  }
  return Ok(validated.value as AuthId);
}

export function serialise_PaymentError(value: PaymentError): JsonValue {
  switch (value.tag) {
    case "Declined":
      return { kind: "Declined" };
    case "GatewayDown":
      return { kind: "GatewayDown" };
  }
}

export function deserialise_PaymentError(json: JsonValue, path: string = "$"): Result<PaymentError, BoundaryError> {
  if (typeof json !== "object" || json === null || Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "object", actual: typeof json });
  }
  const obj = json as { [k: string]: JsonValue };
  const kind = obj["kind"];
  switch (kind) {
    case "Declined":
      return Ok({ tag: "Declined" } as PaymentError);
    case "GatewayDown":
      return Ok({ tag: "GatewayDown" } as PaymentError);
    default:
      return Err({ kind: "StructuralMismatch", path, expected: "sum variant kind", actual: String(kind) });
  }
}


export function serialise_Result_AuthId_PaymentError(value: Result<AuthId, PaymentError>): JsonValue {
  if (value.tag === "Ok") return { kind: "Ok", value: serialise_AuthId(value.value) };
  return { kind: "Err", error: serialise_PaymentError(value.error) };
}

export function deserialise_Result_AuthId_PaymentError(json: JsonValue, path: string = "$"): Result<Result<AuthId, PaymentError>, BoundaryError> {
  if (typeof json !== "object" || json === null || Array.isArray(json)) {
    return Err({ kind: "StructuralMismatch", path, expected: "object", actual: typeof json });
  }
  const obj = json as { [k: string]: JsonValue };
  if (obj["kind"] === "Ok") {
  const __r_v = deserialise_AuthId(obj["value"], `${path}.value`);
  if (__r_v.tag === "Err") return __r_v;
  const __v = __r_v.value;
    return Ok(Ok(__v) as Result<AuthId, PaymentError>);
  } else if (obj["kind"] === "Err") {
  const __r_e = deserialise_PaymentError(obj["error"], `${path}.error`);
  if (__r_e.tag === "Err") return __r_e;
  const __e = __r_e.value;
    return Ok(Err(__e) as Result<AuthId, PaymentError>);
  }
  return Err({ kind: "StructuralMismatch", path, expected: "Ok | Err", actual: String(obj["kind"]) });
}

