/**
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 BACKTICK = '`';
const PLACEHOLDER_OPEN = '${';
const PLACEHOLDER_CLOSE = '}';

class Placeholders {
  constructor(str) {
    this.string = str;
  }

  /**
   * Looks for placeholders in template strings. If the
   * contents of the placeholder is a string literal, then
   * it will remove the placeholder as if it were evaluated
   * and interpolated into the string.
   */
  renderStringLiterals() {
    this.string = _renderStringLiterals(this.string);
    return this;
  }

  /**
   * Returns the string value property. Because there may
   * be multiple operations that could be done to parse and
   * modify placeholders, this class's methods return the
   * instance so that operations can be chained. This gives
   * a way to get the resulting value of chained calls.
   */
  toString() {
    return this.string;
  }

  /**
   * Static method to perform all cleaning operations on a string.
   */
  static clean(str) {
    return new Placeholders(str).renderStringLiterals().toString();
  }
}

/**
 * If a placeholder ${expression} is found in a template string,
 * then this function will attempt to remove the placeholder if
 * the expression is simply a string literal;
 *
 * @param   {String} str
 * @param   {Number} fromIdx
 * @returns {String}
 */
function _renderStringLiterals(str, fromIdx) {
  fromIdx = fromIdx || 0;

  /* Find indices of chars and validate thier arrangement. */
  const btStartIdx = str.indexOf(BACKTICK, fromIdx);
  if (-1 === btStartIdx) {
    return str;
  }

  const phStartIdx = str.indexOf(PLACEHOLDER_OPEN, btStartIdx + 1);
  if (-1 === phStartIdx) {
    return str;
  }

  const phEndIdx = str.indexOf(PLACEHOLDER_CLOSE, phStartIdx + 1);
  if (-1 === phEndIdx) {
    return str;
  }

  const btEndIdx = str.indexOf(BACKTICK, phEndIdx + 1);
  if (-1 === btEndIdx) {
    return str;
  }

  const singles = /^\${\s*(?:')([^']*?)(?:')(\s*})$/;
  const doubles = /^\${\s*(?:")([^"]*?)(?:")(\s*})$/;
  const ticks = /^\${\s*(?:`)([^`]*?)(?:`)(\s*})$/;
  const section = str.substring(phStartIdx, phEndIdx + 1);
  const match =
    singles.exec(section) || doubles.exec(section) || ticks.exec(section);

  if (!match) {
    return str;
  }

  /* Recurse to capture all cases. */
  return _renderStringLiterals(
    str.substring(0, phStartIdx) + match[1] + str.substring(phEndIdx + 1),
    phEndIdx - match[2].length + 1
  );
}

module.exports = Placeholders;
