/**
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 Auth = require('./auth');
const BotBlocker = require('./bot-blocker');
const IpFilter = require('./ip-filter');
const LogEnhancer = require('./log-enhancer');
const RuleDefinition = require('./rule-definition');
const Syslog = require('./syslog');
const { wrapToObject } = require('../../helpers');
const {
  STRING_DEFINITION_TYPE,
  SYSLOG_CONNECTION_TYPES,
  SYSLOG_SEVERITIES
} = require('../../enums');

function DefendFeatures(defend = {}) {
  const {
    auth,
    botBlockers = [],
    logEnhancers = [],
    ruleDefinitionList = [],
    ipDenylists = [],
    ipAllowlists = []
  } = defend;
  return wrapToObject(
    new settings.DefendFeatures([
      defend.enabled, //                                             1          enabled
      defend['bot-blocker'], //                                      2      bot_blocker
      botBlockers.map((blocker) => BotBlocker(blocker).array), //    3     bot_blockers
      undefined, //                                                  4                -
      undefined, //                                                  5                -
      logEnhancers.map((le) => LogEnhancer(le).array), //            6    log_enhancers (unsupported as of q2-2019)
      ruleDefinitionList.map((def) => RuleDefinition(def).array), // 7 rule_definitions
      Syslog(defend.syslog).array, //                                8           syslog
      Auth(auth).array, //                                           9             auth
      ipDenylists.map((dl) => IpFilter(dl).array), //                10    ip_denylists
      ipAllowlists.map((al) => IpFilter(al).array) //                11   ip_allowlists
    ]),
    ({ syslog, ruleDefinitionsList }) => {
      ruleDefinitionsList.forEach(({ keywordsList, patternsList }) => {
        keywordsList.forEach((kw) => {
          kw.type = STRING_DEFINITION_TYPE[kw.type];
        });
        patternsList.forEach((p) => {
          p.type = STRING_DEFINITION_TYPE[p.type];
        });
      });

      // Convert integer-valued enum fields to corresponding string values.
      syslog.connectionType = SYSLOG_CONNECTION_TYPES[syslog.connectionType];
      syslog.severityBlocked = SYSLOG_SEVERITIES[syslog.severityBlocked];
      syslog.severityExploited = SYSLOG_SEVERITIES[syslog.severityExploited];
      syslog.severityProbed = SYSLOG_SEVERITIES[syslog.severityProbed];
    }
  );
}

module.exports = DefendFeatures;
