/**
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 { expression } = require('@babel/template');

/**
 * @typedef {Object} Spec
 * @property {string} name
 * @property {string=} type
 * @property {string=} token
 * @property {Object} modes
 * @property {boolean} modes.assess
 * @property {boolean} modes.protect
 */

/** @type {Spec[]} */
const specs = [
  // Binary Expression
  {
    name: '__add',
    type: 'BinaryExpression',
    token: '+',
    modes: { assess: true, protect: false }
  },
  {
    name: '__tripleEqual',
    type: 'BinaryExpression',
    token: '===',
    modes: { assess: true, protect: false }
  },
  {
    name: '__notTripleEqual',
    type: 'BinaryExpression',
    token: '!==',
    modes: { assess: true, protect: false }
  },
  {
    name: '__doubleEqual',
    type: 'BinaryExpression',
    token: '==',
    modes: { assess: true, protect: false }
  },
  {
    name: '__notDoubleEqual',
    type: 'BinaryExpression',
    token: '!=',
    modes: { assess: true, protect: false }
  },
  // Call Expression
  {
    name: '__contrastEval',
    type: 'CallExpression',
    token: 'eval',
    modes: { assess: true, protect: true }
  },
  // Import Declaration
  {
    name: '__importDefault',
    type: 'ImportDefaultSpecifier',
    modes: { assess: true, protect: true }
  },
  {
    name: '__importNamespace',
    type: 'ImportNamespaceSpecifier',
    modes: { assess: true, protect: true }
  },
  {
    name: '__import',
    type: 'ImportSpecifier',
    modes: { assess: true, protect: true }
  },
  // Member Expression
  {
    name: '__forceCopy',
    type: 'MemberExpression',
    modes: { assess: true, protect: false }
  },
  // Template Literal
  {
    name: '__contrastTag',
    type: 'TemplateLiteral',
    token: '${',
    modes: { assess: true, protect: false }
  },
  // Switch Statement
  {
    name: '__cast',
    type: 'SwitchStatement',
    token: 'switch',
    modes: { assess: true, protect: false }
  },
  // Misc
  // NOTE: do these really belong here as callees? there must be a more elegant way to include these in `rewrite-log`
  {
    name: 'wrap',
    modes: { assess: true, protect: true }
  },
  {
    name: 'staticVisitors',
    modes: { assess: true, protect: false }
  }
];

const calleeBuilder = expression('ContrastMethods.%%name%%');

/**
 * Optimization: build all the callees ahead of traversal. Depending on agent mode,
 * some transformations are not necessary.
 * @returns {{
 *  specs: Spec[],
 *  callees: { [name: string]: import('@babel/types').MemberExpression }
 * }}
 */
module.exports = function createCallees(agent) {
  const assessMode = agent.isInAssessMode();
  const protectMode = agent.isInDefendMode();

  const callees = specs.reduce(
    (memo, spec) =>
      (assessMode && spec.modes.assess) || (protectMode && spec.modes.protect)
        ? Object.assign(memo, {
            [spec.name]: calleeBuilder({ name: spec.name })
          })
        : memo,
    {}
  );

  return { specs, callees };
};
