/**
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';

/**
 * @module
 */

const Request = require('../request');
const { LAYER_STACK } = require('../../../constants');

/**
 * Model of request objects sent to TeamServer
 */
class ExpressRequest extends Request {
  /**
   * @param {express.Request} req
   * @see {@link http://expressjs.com/en/4x/api.html#req|Express request docs}
   */
  constructor(req) {
    super(req);
    this.body = req.body;
    this.parameters = req.params;
  }

  set normalizedUri(val) {}

  get normalizedUri() {
    if (this._raw[LAYER_STACK]) {
      let normalized = '';
      this._raw[LAYER_STACK].forEach((entry) => {
        if (!entry) {
          return;
        }
        entry = entry.toString();
        if (entry !== '/') {
          if (entry[0] === '/') {
            normalized += entry;
          } else {
            normalized += `/${entry}`;
          }
        }
      });
      return normalized;
    } else {
      return '';
    }
  }

  /**
   * Like normalizedUri above, params could come in on each
   * middleware. this properly adds new key/value pairs to the params
   */
  set parameters(params) {
    this._params = { ...this.parameters, ...params };
  }

  get parameters() {
    return this._params;
  }
}

module.exports = ExpressRequest;
