/**
 * @license
 * Copyright The Closure Library Authors.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @fileoverview DOM pattern to match any children of a tag, and
 * specifically collect those that match a child pattern.
 */


// TODO(user): We're trying to migrate all ES5 subclasses of Closure
// Library to ES6. In ES6 this cannot be referenced before super is called. This
// file has at least one this before a super call (in ES5) and cannot be
// automatically upgraded to ES6 as a result. Please fix this if you have a
// chance. Note: This can sometimes be caused by not calling the super
// constructor at all. You can run the conversion tool yourself to see what it
// does on this file: blaze run //javascript/refactoring/es6_classes:convert.

goog.provide('goog.dom.pattern.ChildMatches');

goog.require('goog.dom.pattern.AllChildren');
goog.require('goog.dom.pattern.MatchType');
goog.requireType('goog.dom.TagWalkType');
goog.requireType('goog.dom.pattern.AbstractPattern');



/**
 * Pattern object that matches any nodes at or below the current tree depth.
 *
 * @param {goog.dom.pattern.AbstractPattern} childPattern Pattern to collect
 *     child matches of.
 * @param {number=} opt_minimumMatches Enforce a minimum nuber of matches.
 *     Defaults to 0.
 * @constructor
 * @extends {goog.dom.pattern.AllChildren}
 * @final
 */
goog.dom.pattern.ChildMatches = function(childPattern, opt_minimumMatches) {
  'use strict';
  /**
   * The child pattern to collect matches from.
   *
   * @private {goog.dom.pattern.AbstractPattern}
   */
  this.childPattern_ = childPattern;

  /**
   * Array of matched child nodes.
   *
   * @type {Array<Node>}
   */
  this.matches = [];

  /**
   * Minimum number of matches.
   *
   * @private {number}
   */
  this.minimumMatches_ = opt_minimumMatches || 0;

  /**
   * Whether the pattern has recently matched or failed to match and will need
   * to be reset when starting a new round of matches.
   *
   * @private {boolean}
   */
  this.needsReset_ = false;

  goog.dom.pattern.ChildMatches.base(this, 'constructor');
};
goog.inherits(goog.dom.pattern.ChildMatches, goog.dom.pattern.AllChildren);


/**
 * Test whether the given token is on the same level.
 *
 * @param {Node} token Token to match against.
 * @param {goog.dom.TagWalkType} type The type of token.
 * @return {goog.dom.pattern.MatchType} `MATCHING` if the token is on the
 *     same level or deeper and `BACKTRACK_MATCH` if not.
 * @override
 */
goog.dom.pattern.ChildMatches.prototype.matchToken = function(token, type) {
  'use strict';
  // Defer resets so we maintain our matches array until the last possible time.
  if (this.needsReset_) {
    this.reset();
  }

  // Call the super-method to ensure we stay in the child tree.
  const status =
      goog.dom.pattern.AllChildren.prototype.matchToken.apply(this, arguments);

  switch (status) {
    case goog.dom.pattern.MatchType.MATCHING:
      let backtrack = false;

      switch (this.childPattern_.matchToken(token, type)) {
        case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
          backtrack = true;
        case goog.dom.pattern.MatchType.MATCH:
          // Collect the match.
          this.matches.push(this.childPattern_.matchedNode);
          break;

        default:
          // Keep trying if we haven't hit a terminal state.
          break;
      }

      if (backtrack) {
        // The only interesting result is a MATCH, since BACKTRACK_MATCH means
        // we are hitting an infinite loop on something like a Repeat(0).
        if (this.childPattern_.matchToken(token, type) ==
            goog.dom.pattern.MatchType.MATCH) {
          this.matches.push(this.childPattern_.matchedNode);
        }
      }
      return goog.dom.pattern.MatchType.MATCHING;

    case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
      // TODO(robbyw): this should return something like BACKTRACK_NO_MATCH
      // when we don't meet our minimum.
      this.needsReset_ = true;
      return (this.matches.length >= this.minimumMatches_) ?
          goog.dom.pattern.MatchType.BACKTRACK_MATCH :
          goog.dom.pattern.MatchType.NO_MATCH;

    default:
      this.needsReset_ = true;
      return status;
  }
};


/**
 * Reset any internal state this pattern keeps.
 * @override
 */
goog.dom.pattern.ChildMatches.prototype.reset = function() {
  'use strict';
  this.needsReset_ = false;
  this.matches.length = 0;
  this.childPattern_.reset();
  goog.dom.pattern.AllChildren.prototype.reset.call(this);
};
