/** Config / mount / secret-guard failure (programming or wiring error). */
export class PaymentProviderError extends Error {
  readonly name = 'PaymentProviderError'
  constructor(message: string) {
    super(message)
  }
}

/** A payment confirmation failure (declined card, etc.). NOT a throw — returned in PaymentConfirmResult. */
export class PaymentConfirmError extends Error {
  readonly name = 'PaymentConfirmError'
  /** The PSP's own error-code string, passed through verbatim (Stripe error.code). A plain string. */
  readonly code: string
  constructor(message: string, code: string) {
    super(message)
    this.code = code
  }
}

/** Structural guard — keys on name + Error shape, NEVER instanceof (cross-package dedup-safe). */
export function isPaymentProviderError(e: unknown): e is PaymentProviderError {
  return e instanceof Error && (e as { name?: unknown }).name === 'PaymentProviderError'
}

export function isPaymentConfirmError(e: unknown): e is PaymentConfirmError {
  return (
    e instanceof Error &&
    (e as { name?: unknown }).name === 'PaymentConfirmError' &&
    typeof (e as { code?: unknown }).code === 'string'
  )
}