/**
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 _ = require('lodash');

const Assessment = require('../assessment');
const { BaseEvaluator } = require('../signatures/evaluator');
const JSCleanString = require('../../../util/clean-string/jsclean-string');

class Evaluator extends BaseEvaluator {
  /**
   * This override evaluates both the original string, as well as a copy
   * of the string that has been "normalized" by having various JavaScript
   * tokens removed, merged, or transformed.
   *
   * @param   {String} value The string to evaluate against rule definitions
   * @returns {Object}       The evaluation details
   */
  evaluate(userInput) {
    const assessment = Assessment();
    const { value } = userInput;
    const normalized = JSCleanString.clean(value).toString();

    /* If the normalized string and the original are the same length, then */
    /* they are the same, and there's no need to evaluate both of them.    */
    return normalized.length === value.length
      ? super.evaluate(value, { assessment })
      : _.last([
          super.evaluate(value, { assessment }),
          super.evaluate(normalized, { assessment })
        ]);
  }
}

module.exports = Evaluator;
