/**
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';

class AppCreate {
  constructor(agent, appInfo) {
    const { config } = agent;
    if (config && config.application) {
      const {
        code,
        group,
        metadata,
        session_id,
        session_metadata,
        tags
      } = config.application;

      this.tags = tags;
      this.group = group;
      this.metadata = metadata;
      this.code = code;
      this.version = appInfo.version;

      // Per the spec, only set these if they are non-empty values.
      // { session_id: null } is technically wrong.
      if (session_id) {
        this.session_id = session_id;
      } else if (session_metadata) {
        this.session_metadata = session_metadata;
      }
    }
    this.assess = agent.isInAssessMode();
    this.protect = agent.isInDefendMode();
  }

  /*
   * The teamserver DTM only includes whether protect is enabled
   */
  toSerializable() {
    const result = Object.assign({}, this);
    delete result.assess;
    delete result.protect;

    result.instrumentation = {
      protect: { enable: this.protect }
    };
    return result;
  }
}

module.exports = AppCreate;
