/**
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 { PropagationEvent, Signature, CallContext } = require('../../models');

/**
 * Create the event to append to the result's metadata
 *
 * @param {string} the name of the signature
 * @param {CleanStack} stack
 * @param {TagRange[]} tags tag ranges on the url part we're propagating to
 * @param {string} part url part created from the parse
 * @param {*[]}  args list of arguments passed to url.parse
 * @param {*} result the result of the call to url.parse
 * @return {PropagationEvent} event to append to a part's metadata
 */
module.exports.createEvent = function(
  name,
  stack,
  tagRanges,
  part,
  args,
  result
) {
  const signature = new Signature(name);

  // To be 100% correct we would use result/url (should be the same)
  // for obj/result, but it's nicer to show propagation to the specific part.
  // Since the operation is P2O, we replace O with the url part.
  const context = CallContext.create({
    obj: part,
    args,
    result
  });
  context.stack = stack;

  const event = new PropagationEvent({
    context,
    signature,
    tagRanges,
    source: 'P',
    target: 'O'
  });

  return event;
};
