/**
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 Action = require('./action');
const Type = require('./type');
const TraceEventObject = require('./trace-event-object');
const TraceEventSignature = require('./trace-event-signature');
const TraceEventSource = require('./trace-event-source');
const TraceStack = require('./trace-stack');
const TraceTaintRange = require('./trace-taint-range');
const { mapToModelArray } = require('../../../helpers');

module.exports = function TraceEvent(event = {}) {
  const action = Action(event.action);
  const type = Type(event.type);
  const signature = TraceEventSignature(event.signature).array;
  const object = TraceEventObject(event.object).array;
  const ret = TraceEventObject(event.ret).array;
  const args = mapToModelArray(TraceEventObject, event.args);
  const stack = mapToModelArray(TraceStack, event.stack);
  const eventSources = mapToModelArray(TraceEventSource, event.eventSources);
  const taintRanges = mapToModelArray(TraceTaintRange, event.trackedRanges);

  return new dtm.TraceEvent({
    0: action, //         1        action
    1: type, //           2          type
    2: event.time, //     3  timestamp_ms
    3: event.thread, //   4        thread
    5: signature, //      6     signature
    7: event.context, //  8       context
    8: event.code, //     9          code
    15: object, //       16        object
    16: ret, //          17           ret
    17: args, //         18          args
    18: stack, //        19         stack
    19: eventSources, // 20 event_sources
    20: event.source, // 21        source
    21: event.target, // 22        target
    22: taintRanges //   23  taint_ranges
  });
};
