/**
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 agentEmitter = require('../../agent-emitter');
const { EVENT_TYPES } = require('../../constants');
const BaseEvent = require('./base-event');

module.exports = class SourceEvent extends BaseEvent {
  /**
   * Create a source event.
   * @param {Object}      opts
   * @param {CallContext} opts.context
   * @param {string}      opts.name the name of the property being accessed
   * @param {Signature}   opts.signature
   * @param {TagRange[]}  opts.tagRanges
   * @param {string}      opts.target
   * @param {string}      opts.type 'BODY', 'HEADER', etc.
   * @param {boolean}     opts.isRequestObject opts.name is directly on the request object
   * @param {boolean}     opts.isArray true if req.body is an array (from JSON body).
   */
  constructor({
    code,
    context,
    name,
    signature,
    tagRanges,
    target,
    type,
    isRequestObject,
    isArray,
    eventSources = [{ sourceType: type.toUpperCase(), sourceName: name }]
  } = {}) {
    super(context, signature, tagRanges);
    this.code = code;
    this.target = target;
    this.eventSources = eventSources;
    this.parents = [];
    // the following are used to customize the format of the code displayed.
    this.isRequestObject = isRequestObject;
    this.isArray = isArray;

    agentEmitter.emit('dataflowEvent', this);
  }

  getAllParents(set = new Set()) {
    return set;
  }

  /**
   * All sources need to generate the code in the same way
   * Overriding BaseEvent method to perform appropriate actions
   * for the code property
   */
  mapCustomMethods() {
    const type = this.eventSources[0].sourceType.toLowerCase();
    const name = this.eventSources[0].sourceName;
    if (this.code) {
      return;
    }

    if (this.isRequestObject) {
      this.code = `const ${type} = req.${type}`;
    } else if (this.isArray) {
      this.code = `const s = req.${type}${name}`;
    } else {
      this.code = `const ${name} = req.${type}.${name}`;
    }
  }

  expand() {
    // checking if this is Object before calling super
    // because it rewrites it to this.signature.class
    const isContextObj = this.context.obj === 'Object';

    super.expand();
    this.action = 'CREATION';
    this.type = EVENT_TYPES.PROPAGATION;
    if (isContextObj) {
      this.context.obj = this.eventSources[0].sourceType;
    }
  }
};
