/**
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 { settings } = require('@contrast/protobuf-api');

const { wrapToObject } = require('../../helpers');
const {
  EXCLUSION_TYPES,
  EXCLUSION_INPUT_TYPES,
  EXCLUSION_MATCH_STRATEGIES
} = require('../../enums');

function Exclusion({
  assessmentRules,
  inputName,
  inputType,
  matchStrategy,
  modes,
  name,
  rules,
  type,
  urls,
  denylist
}) {
  return wrapToObject(
    new settings.Exclusion([
      ExclusionType(type), //                    1             type
      name, //                                   2             name
      ExclusionMatchStrategy(matchStrategy), //  3   match_strategy
      undefined, //                              4                -
      rules, //                                  5 protection_rules
      assessmentRules, //                        6 assessment_rules
      urls, //                                   7             urls
      undefined, //                              8                -
      ExclusionInputType(inputType), //          9       input_type
      inputName, //                             10       input_name
      modes.includes('assess'), //              11           assess
      modes.includes('defend'), //              12          protect
      denylist //                               13         denylist
    ]),
    (exclusion) => {
      exclusion.inputType = EXCLUSION_INPUT_TYPES[exclusion.inputType];
      exclusion.matchStrategy =
        EXCLUSION_MATCH_STRATEGIES[exclusion.matchStrategy];
      exclusion.type = EXCLUSION_TYPES[exclusion.type];
    }
  );
}

function ExclusionInputType(inputType) {
  return inputType in EXCLUSION_INPUT_TYPES
    ? EXCLUSION_INPUT_TYPES[inputType]
    : 0;
}

function ExclusionType(type) {
  return EXCLUSION_TYPES[type];
}

function ExclusionMatchStrategy(strategy) {
  return strategy in EXCLUSION_MATCH_STRATEGIES
    ? EXCLUSION_MATCH_STRATEGIES[strategy]
    : 0;
}

function Exclusions({
  codeExceptions = [],
  inputExceptions = [],
  urlExceptions = []
} = {}) {
  return [
    { list: codeExceptions, type: 'CODE' },
    { list: inputExceptions, type: 'INPUT' },
    { list: urlExceptions, type: 'URL' }
  ].reduce(
    (acc, { list, type }) =>
      list.reduce((acc, exception) => {
        acc.push(Exclusion(Object.assign({ type }, exception)));
        return acc;
      }, acc),
    []
  );
}

module.exports = Exclusions;
