/**
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 TraceEvent = require('./trace-event');
const { RoutesListWithObservations } = require('./route-coverage');
const { mapToModelArray, objToMap } = require('../../helpers');

module.exports = function Finding(details = {}) {
  const { ruleId, hash } = details;
  const preflight = ruleId && hash ? `${ruleId},${hash}` : '';
  const props = details.properties;
  if (props && 'lineNumber' in props) {
    props.lineNumber = props.lineNumber.toString();
  }

  const routes = RoutesListWithObservations(details.routes);
  const properties = objToMap(details.properties).map(
    // ensure string values
    ([key, value]) => [key, String(value)]
  );

  return new dtm.Finding({
    0: String(details.hash || ''), //                  1  hash_code
    2: details.ruleId, //                              3    rule_id
    5: properties, //                                  6 properties
    6: mapToModelArray(TraceEvent, details.events), // 7     events
    7: preflight, //                                   8  preflight
    8: details.tags, //                                9       tags
    9: details.version, //                            10    version
    10: routes //                                     11     routes
  });
};
