export let PaymentErrorTypes = /*#__PURE__*/function (PaymentErrorTypes) {
  PaymentErrorTypes["PaymentExpired"] = "PaymentExpired";
  PaymentErrorTypes["PaymentIncomplete"] = "PaymentIncomplete";
  PaymentErrorTypes["PaymentAmountMismatch"] = "PaymentAmountMismatch";
  return PaymentErrorTypes;
}({});
function getStateKeys(component) {
  const extraStateKeys = component.page?.getStateKeys(component) ?? [];
  return [component.name].concat(extraStateKeys);
}
export class PaymentPreAuthError extends Error {
  component;
  userMessage;

  /**
   * Whether to reset the component state and redirect to the component's page.
   * - `true`: Reset state and redirect (e.g., payment expired - user must re-enter)
   * - `false`: Keep state and stay on current page with error (e.g., capture failed - user can retry)
   */
  shouldResetState;

  /**
   * When supplied, an "Important" notification banner will be shown based on the value.
   */
  errorType;
  constructor(component, userMessage, shouldResetState, errorType) {
    super('Payment capture failed');
    this.name = 'PaymentPreAuthError';
    this.component = component;
    this.userMessage = userMessage;
    this.shouldResetState = shouldResetState;
    this.errorType = errorType;
  }
  getStateKeys() {
    return getStateKeys(this.component);
  }
}

/**
 * Thrown when form submission fails after payment has been captured.
 * User needs to retry or contact support for a refund.
 */
export class PaymentSubmissionError extends Error {
  referenceNumber;
  helpLink;
  constructor(referenceNumber, helpLink) {
    super('Form submission failed after payment capture');
    this.name = 'PaymentSubmissionError';
    this.referenceNumber = referenceNumber;
    this.helpLink = helpLink;
  }
  static checkPaymentAmount(stateAmount, expectedAmount, component) {
    if (stateAmount / 100 !== expectedAmount) {
      throw new PaymentPreAuthError(component, 'The pre-authorised payment amount is somehow different from that requested. Try adding payment details again.', true, PaymentErrorTypes.PaymentIncomplete);
    }
  }
}

/**
 * Thrown when a component has an invalid state. This is typically only required where state needs
 * to be checked against an external source upon submission of a form. For example: file upload
 * has internal state (file upload IDs) but also external state (files in S3). The internal state
 * is always validated by the engine, but the external state needs validating too.
 *
 * This should be used within a formComponent.onSubmit(...).
 */
export class InvalidComponentStateError extends Error {
  component;
  userMessage;
  constructor(component, userMessage) {
    const message = `Invalid component state for: ${component.name}`;
    super(message);
    this.name = 'InvalidComponentStateError';
    this.component = component;
    this.userMessage = userMessage;
  }
  getStateKeys() {
    return getStateKeys(this.component);
  }
}
//# sourceMappingURL=errors.js.map