/**
Copyright: 2024 Contrast Security, Inc
  Contact: support@contrastsecurity.com
  License: Commercial

  NOTICE: This Software and the patented inventions embodied within may only be
  used as part of Contrast Security’s commercial offerings. Even though it is
  made available through public repositories, use of this Software is subject to
  the applicable End User Licensing Agreement found at
  https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
  between Contrast Security and the End User. The Software may not be reverse
  engineered, modified, repackaged, sold, redistributed or otherwise used in a
  way not consistent with the End User License Agreement.
*/
'use strict';

const BaseConnectionState = require('./base-connection-state');
const { CONNECTION_STATES } = require('./constants');

/**
 * Localizes state-specific behavior related to sending messages to the
 * contrast-service while the Speedracer instance is not enabled.
 * A FAILURE state results from the inability to connect to the service, and
 * the inability to start the service. Once in this state, we do not recover.
 */
class FailureConnectionState extends BaseConnectionState {
  constructor(speedracer) {
    super(speedracer);
    speedracer._enabled = false;
    this.speedracer = speedracer;
    this.state = CONNECTION_STATES.FAILURE;
  }

  /**
   * When speedracer is disabled and messages are being sent we return promise
   * which resolves an empty Buffer.
   * @param {BaseMessage} message The message being sent
   * @returns {Buffer}
   */
  send(message) {
    return Promise.resolve(this.speedracer.client.defaultResponse);
  }
}

module.exports = FailureConnectionState;
