text: /*! @license Firebase v4.3.1
  Build: rev-b4fe95f
  Terms: https://firebase.google.com/terms/ */
  
  'use strict';
  
  Object.defineProperty(exports, "__esModule", {
      value: true
  });
  exports.ChildEventRegistration = exports.ValueEventRegistration = undefined;
  
  var _DataSnapshot = require('../../api/DataSnapshot');
  
  var _Event = require('./Event');
  
  var _obj = require('../../../utils/obj');
  
  var _assert = require('../../../utils/assert');
  
  /**
   * Represents registration for 'value' events.
   */
  /**
  * Copyright 2017 Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  var ValueEventRegistration = function () {
      /**
       * @param {?function(!DataSnapshot)} callback_
       * @param {?function(Error)} cancelCallback_
       * @param {?Object} context_
       */
      function ValueEventRegistration(callback_, cancelCallback_, context_) {
          this.callback_ = callback_;
          this.cancelCallback_ = cancelCallback_;
          this.context_ = context_;
      }
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.respondsTo = function (eventType) {
          return eventType === 'value';
      };
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.createEvent = function (change, query) {
          var index = query.getQueryParams().getIndex();
          return new _Event.DataEvent('value', this, new _DataSnapshot.DataSnapshot(change.snapshotNode, query.getRef(), index));
      };
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.getEventRunner = function (eventData) {
          var ctx = this.context_;
          if (eventData.getEventType() === 'cancel') {
              (0, _assert.assert)(this.cancelCallback_, 'Raising a cancel event on a listener with no cancel callback');
              var cancelCB_1 = this.cancelCallback_;
              return function () {
                  // We know that error exists, we checked above that this is a cancel event
                  cancelCB_1.call(ctx, eventData.error);
              };
          } else {
              var cb_1 = this.callback_;
              return function () {
                  cb_1.call(ctx, eventData.snapshot);
              };
          }
      };
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.createCancelEvent = function (error, path) {
          if (this.cancelCallback_) {
              return new _Event.CancelEvent(this, error, path);
          } else {
              return null;
          }
      };
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.matches = function (other) {
          if (!(other instanceof ValueEventRegistration)) {
              return false;
          } else if (!other.callback_ || !this.callback_) {
              // If no callback specified, we consider it to match any callback.
              return true;
          } else {
              return other.callback_ === this.callback_ && other.context_ === this.context_;
          }
      };
      /**
       * @inheritDoc
       */
      ValueEventRegistration.prototype.hasAnyCallback = function () {
          return this.callback_ !== null;
      };
      return ValueEventRegistration;
  }();
  exports.ValueEventRegistration = ValueEventRegistration;
  /**
   * Represents the registration of 1 or more child_xxx events.
   *
   * Currently, it is always exactly 1 child_xxx event, but the idea is we might let you
   * register a group of callbacks together in the future.
   *
   * @constructor
   * @implements {EventRegistration}
   */
  
  var ChildEventRegistration = function () {
      /**
       * @param {?Object.<string, function(!DataSnapshot, ?string=)>} callbacks_
       * @param {?function(Error)} cancelCallback_
       * @param {Object=} context_
       */
      function ChildEventRegistration(callbacks_, cancelCallback_, context_) {
          this.callbacks_ = callbacks_;
          this.cancelCallback_ = cancelCallback_;
          this.context_ = context_;
      }
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.respondsTo = function (eventType) {
          var eventToCheck = eventType === 'children_added' ? 'child_added' : eventType;
          eventToCheck = eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck;
          return (0, _obj.contains)(this.callbacks_, eventToCheck);
      };
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.createCancelEvent = function (error, path) {
          if (this.cancelCallback_) {
              return new _Event.CancelEvent(this, error, path);
          } else {
              return null;
          }
      };
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.createEvent = function (change, query) {
          (0, _assert.assert)(change.childName != null, 'Child events should have a childName.');
          var ref = query.getRef().child /** @type {!string} */(change.childName);
          var index = query.getQueryParams().getIndex();
          return new _Event.DataEvent(change.type, this, new _DataSnapshot.DataSnapshot(change.snapshotNode, ref, index), change.prevName);
      };
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.getEventRunner = function (eventData) {
          var ctx = this.context_;
          if (eventData.getEventType() === 'cancel') {
              (0, _assert.assert)(this.cancelCallback_, 'Raising a cancel event on a listener with no cancel callback');
              var cancelCB_2 = this.cancelCallback_;
              return function () {
                  // We know that error exists, we checked above that this is a cancel event
                  cancelCB_2.call(ctx, eventData.error);
              };
          } else {
              var cb_2 = this.callbacks_[eventData.eventType];
              return function () {
                  cb_2.call(ctx, eventData.snapshot, eventData.prevName);
              };
          }
      };
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.matches = function (other) {
          if (other instanceof ChildEventRegistration) {
              if (!this.callbacks_ || !other.callbacks_) {
                  return true;
              } else if (this.context_ === other.context_) {
                  var otherCount = (0, _obj.getCount)(other.callbacks_);
                  var thisCount = (0, _obj.getCount)(this.callbacks_);
                  if (otherCount === thisCount) {
                      // If count is 1, do an exact match on eventType, if either is defined but null, it's a match.
                      //  If event types don't match, not a match
                      // If count is not 1, exact match across all
                      if (otherCount === 1) {
                          var otherKey /** @type {!string} */ = (0, _obj.getAnyKey)(other.callbacks_);
                          var thisKey /** @type {!string} */ = (0, _obj.getAnyKey)(this.callbacks_);
                          return thisKey === otherKey && (!other.callbacks_[otherKey] || !this.callbacks_[thisKey] || other.callbacks_[otherKey] === this.callbacks_[thisKey]);
                      } else {
                          // Exact match on each key.
                          return (0, _obj.every)(this.callbacks_, function (eventType, cb) {
                              return other.callbacks_[eventType] === cb;
                          });
                      }
                  }
              }
          }
          return false;
      };
      /**
       * @inheritDoc
       */
      ChildEventRegistration.prototype.hasAnyCallback = function () {
          return this.callbacks_ !== null;
      };
      return ChildEventRegistration;
  }();
  exports.ChildEventRegistration = ChildEventRegistration;
  //# sourceMappingURL=EventRegistration.js.map
  