text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
      value: true
  });
  
  var _getIterator2 = require('babel-runtime/core-js/get-iterator');
  
  var _getIterator3 = _interopRequireDefault(_getIterator2);
  
  var _ErrorHandler = require('../utils/ErrorHandler');
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  var waitForExist = function waitForExist(selector, ms, reverse) {
      var _this = this;
  
      /**
       * we can't use default values for function parameter here because this would
       * break the ability to chain the command with an element if reverse is used, like
       *
       * ```js
       * var elem = $('#elem');
       * elem.waitForXXX(10000, true);
       * ```
       */
      reverse = typeof reverse === 'boolean' ? reverse : false;
  
      /*!
       * ensure that ms is set properly
       */
      if (typeof ms !== 'number') {
          ms = this.options.waitforTimeout;
      }
  
      return this.waitUntil(function () {
          return _this.isExisting(selector).then(function (isExisting) {
              if (!Array.isArray(isExisting)) {
                  return isExisting !== reverse;
              }
  
              var result = reverse;
              var _iteratorNormalCompletion = true;
              var _didIteratorError = false;
              var _iteratorError = undefined;
  
              try {
                  for (var _iterator = (0, _getIterator3.default)(isExisting), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                      var val = _step.value;
  
                      if (!reverse) {
                          result = result || val;
                      } else {
                          result = result && val;
                      }
                  }
              } catch (err) {
                  _didIteratorError = true;
                  _iteratorError = err;
              } finally {
                  try {
                      if (!_iteratorNormalCompletion && _iterator.return) {
                          _iterator.return();
                      }
                  } finally {
                      if (_didIteratorError) {
                          throw _iteratorError;
                      }
                  }
              }
  
              return result !== reverse;
          });
      }, ms).catch(function (e) {
          selector = selector || _this.lastResult.selector;
  
          if ((0, _ErrorHandler.isTimeoutError)(e)) {
              var isReversed = reverse ? '' : 'not ';
              throw new _ErrorHandler.WaitUntilTimeoutError('element (' + selector + ') still ' + isReversed + 'existing after ' + ms + 'ms');
          }
          throw e;
      });
  }; /**
      *
      * Wait for an element (selected by css selector) for the provided amount of
      * milliseconds to be present within the DOM. Returns true if the selector
      * matches at least one element that exists in the DOM, otherwise throws an
      * error. If the reverse flag is true, the command will instead return true
      * if the selector does not match any elements.
      *
      * <example>
         :waitForExistSyncExample.js
         it('should display a notification message after successful form submit', function () {
             var form = $('form');
             var notification = $('.notification');
     
             form.submit();
             notification.waitForExist(5000); // same as `browser.waitForExist('.notification', 5000)`
             expect(notification.getText()).to.be.equal('Data transmitted successfully!')
         });
      * </example>
      *
      * @alias browser.waitForExist
      * @param {String}   selector CSS selector to query
      * @param {Number=}  ms       time in ms (default: 500)
      * @param {Boolean=} reverse  if true it instead waits for the selector to not match any elements (default: false)
      * @uses utility/waitUntil, state/isExisting
      * @type utility
      *
      */
  
  exports.default = waitForExist;
  module.exports = exports['default'];
  