export type CheckoutConfirmationDecision = 'confirmed' | 'cancelled' | 'timeout';

const VALID_DECISIONS: CheckoutConfirmationDecision[] = [
  'confirmed',
  'cancelled',
  'timeout',
];

export class CheckoutConfirmation {
  private readonly _value: CheckoutConfirmationDecision;

  private constructor(value: CheckoutConfirmationDecision) {
    this._value = value;
  }

  static create(value: CheckoutConfirmationDecision): CheckoutConfirmation {
    if (!VALID_DECISIONS.includes(value)) {
      throw new Error('Invalid checkout confirmation decision');
    }
    return new CheckoutConfirmation(value);
  }

  static confirmed(): CheckoutConfirmation {
    return CheckoutConfirmation.create('confirmed');
  }

  static cancelled(): CheckoutConfirmation {
    return CheckoutConfirmation.create('cancelled');
  }

  static timeout(): CheckoutConfirmation {
    return CheckoutConfirmation.create('timeout');
  }

  get value(): CheckoutConfirmationDecision {
    return this._value;
  }

  equals(other: CheckoutConfirmation): boolean {
    return this._value === other._value;
  }

  isConfirmed(): boolean {
    return this._value === 'confirmed';
  }

  isCancelled(): boolean {
    return this._value === 'cancelled';
  }

  isTimeout(): boolean {
    return this._value === 'timeout';
  }
}