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

/**
 * A sequence of tokens. It is mainly an Array
 * with some additional properties / methods.
 */
class TokenSequence extends Array {
  constructor(codeString) {
    super();
    this.codeString = codeString;
  }

  /**
   * If a boundary is crossed, we have to have the start index within
   * within the token (inclusive) and the stop index exclusively above.
   * @param {Number[]} tuple The start/stop values to compare to boundaries.
   */
  crossesBoundary(tuple) {
    const start = tuple[0];
    const stop = tuple[1];

    let index = -1;

    for (const token of this) {
      index++;
      if (token.start <= start && start <= token.stop && token.stop < stop) {
        if (index !== 0) {
          token['previous'] = this[index - 1];
        }
        return token;
      }
      if (token.start <= stop && stop <= token.stop && start < token.start) {
        if (index !== 0) {
          token['previous'] = this[index - 1];
        }
        return token;
      }
    }
    return false;
  }
}

module.exports = TokenSequence;
