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

const StackTraceElement = require('./stack-trace-element');
const UserInput = require('./user-input');
const { mapToModelArray } = require('../../helpers');
const {
  RULES: {
    BOT_BLOCKER,
    CMD_INJECTION,
    CMD_INJECTION_COMMAND_BACKDOORS,
    CMD_INJECTION_SEMANTIC_CHAINED_COMMANDS,
    CMD_INJECTION_SEMANTIC_DANGEROUS_PATHS,
    IP_DENYLIST,
    METHOD_TAMPERING,
    NOSQL_INJECTION,
    PATH_TRAVERSAL,
    REFLECTED_XSS,
    SQL_INJECTION,
    SSJS_INJECTION,
    VIRTUAL_PATCH,
    UNTRUSTED_DESERIALIZATION,
    XXE
  }
} = require('../../../../constants');

const DETAILS_MAP = {
  [BOT_BLOCKER]: {
    index: 16,
    Constructor: require('./bot-blocker-details')
  },
  [CMD_INJECTION]: {
    index: 17,
    Constructor: require('./cmd-injection-details')
  },
  [CMD_INJECTION_COMMAND_BACKDOORS]: {
    index: 41,
    Constructor: require('./cmd-injection-semantic-analysis-details')
  },
  [CMD_INJECTION_SEMANTIC_CHAINED_COMMANDS]: {
    index: 41,
    Constructor: require('./cmd-injection-semantic-analysis-details')
  },
  [CMD_INJECTION_SEMANTIC_DANGEROUS_PATHS]: {
    index: 41,
    Constructor: require('./cmd-injection-semantic-analysis-details')
  },
  [UNTRUSTED_DESERIALIZATION]: {
    index: 20,
    Constructor: require('./untrusted-deserialization-details')
  },
  [IP_DENYLIST]: {
    index: 44,
    Constructor: require('./ip-denylist-details')
  },
  [PATH_TRAVERSAL]: {
    index: 25,
    Constructor: require('./path-traversal-details')
  },
  [SQL_INJECTION]: {
    index: 27,
    Constructor: require('./sql-injection-details')
  },
  [VIRTUAL_PATCH]: {
    index: 29,
    Constructor: require('./virtual-patch-details')
  },
  [REFLECTED_XSS]: {
    index: 30,
    Constructor: require('./xss-details')
  },
  [XXE]: {
    index: 31,
    Constructor: require('./xxe-details')
  },
  [NOSQL_INJECTION]: {
    index: 32,
    Constructor: require('./no-sql-injection-details')
  },
  [METHOD_TAMPERING]: {
    index: 33,
    Constructor: require('./http-method-tampering-details')
  },
  [SSJS_INJECTION]: {
    index: 43,
    Constructor: require('./ssjs-injection-details')
  }
};

const getDetails = ({ ruleId, ...details } = {}) => {
  const array = new Array(29);
  const lookup = DETAILS_MAP[ruleId];

  if (lookup) {
    const { index, Constructor } = lookup;
    array[index] = new Constructor(details).array;
  }

  return array;
};

module.exports = function RaspRuleSample({
  timestamp,
  stack = [],
  ruleId,
  input,
  details = {}
} = {}) {
  if (timestamp && timestamp.start) {
    timestamp = timestamp.start;
  }

  return new dtm.RaspRuleSample({
    1: timestamp, //                                            2         timestamp_ms
    3: mapToModelArray(StackTraceElement, stack), //            4 stack_trace_elements
    4: input ? new UserInput(input).array : undefined, //       5           user_input
    ...getDetails({ ...details, ruleId }) //              17...44              details
  });
};
