/**
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 { mapToModelArray } = require('../../helpers');

/**
 * DTM wrapper for RouteCoverage in app update message
 * This just reports the route signatures
 */
function RouteCoverage({ route } = {}) {
  return new dtm.RouteCoverage({
    2: route // 3 route
  });
}

/**
 * Used to report coverage in application-update
 */
function RoutesList(routes = []) {
  return mapToModelArray(
    RouteCoverage,
    routes.map(({ signature }) => ({ route: signature }))
  );
}

/**
 * DTM wrapper for RouteCoverage for attaching to findings
 */
function FindingRoute({ verb, url, route } = {}) {
  return new dtm.RouteCoverage({
    0: verb,
    1: url,
    2: route // 3 route
  });
}

/**
 * Used to report routes on a Finding within activity
 */
function RoutesListWithObservations(routes = []) {
  return mapToModelArray(
    FindingRoute,
    routes.map(({ signature, observations }) => ({
      route: signature,
      verb: observations && observations[0] && observations[0].verb,
      url: observations && observations[0] && observations[0].url
    }))
  );
}

module.exports = {
  RoutesList,
  RoutesListWithObservations
};
