/**
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 { SYSLOG_CONNECTION_TYPES, SYSLOG_SEVERITIES } = require('../../enums');

function Syslog({
  syslogEnabled,
  syslogPortNumber,
  syslogFacilityCode,
  syslogConnectionType,
  syslogIpAddress,
  syslogSeverityExploited,
  syslogSeverityBlocked,
  syslogSeverityProbed
} = {}) {
  return wrapToObject(
    new settings.Syslog([
      syslogEnabled, //                              1            enabled
      syslogIpAddress, //                            2         ip_address
      syslogPortNumber, //                           3               port
      syslogFacilityCode, //                         4      facility_code
      SyslogConnectionType(syslogConnectionType), // 5    connection_type
      SyslogSeverity(syslogSeverityExploited), //    6 severity_exploited
      SyslogSeverity(syslogSeverityBlocked), //      7   severity_blocked
      SyslogSeverity(syslogSeverityProbed) //        8    severity_probed
    ]),
    (obj) => {
      // Convert integer-valued enum fields to corresponding string values.
      obj.connectionType = SYSLOG_CONNECTION_TYPES[obj.connectionType];
      obj.severityBlocked = SYSLOG_SEVERITIES[obj.severityBlocked];
      obj.severityExploited = SYSLOG_SEVERITIES[obj.severityExploited];
      obj.severityProbed = SYSLOG_SEVERITIES[obj.severityProbed];
    }
  );
}

function SyslogConnectionType(type) {
  // Default to ENCRYPTED
  return type in SYSLOG_CONNECTION_TYPES ? SYSLOG_CONNECTION_TYPES[type] : 1;
}

function SyslogSeverity(severity) {
  return severity in SYSLOG_SEVERITIES ? SYSLOG_SEVERITIES[severity] : 0;
}

module.exports = Syslog;
