"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _ExecutionEnvironment = require("./ExecutionEnvironment.js");

var _ExecutionEnvironment2 = _interopRequireDefault(_ExecutionEnvironment);

var _getVendorPrefixedEventName = require("./getVendorPrefixedEventName.js");

var _getVendorPrefixedEventName2 = _interopRequireDefault(_getVendorPrefixedEventName);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* =========================================
* Copyright (c) 2015-present, Billgo.
* Copyright (c) 2015-present, Facebook, Inc.
*
* All rights reserved.
*======================================== */

var endEvents = [];

function detectEvents() {
  var animEnd = (0, _getVendorPrefixedEventName2.default)("animationend");
  var transEnd = (0, _getVendorPrefixedEventName2.default)("transitionend");

  if (animEnd) {
    endEvents.push(animEnd);
  }

  if (transEnd) {
    endEvents.push(transEnd);
  }
}

if (_ExecutionEnvironment2.default.canUseDOM) {
  detectEvents();
}

function addEventListener(node, eventName, eventListener) {
  node.addEventListener(eventName, eventListener, false);
}

function removeEventListener(node, eventName, eventListener) {
  node.removeEventListener(eventName, eventListener, false);
}

var TransitionEvents = {
  addEndEventListener: function addEndEventListener(node, eventListener) {
    if (endEvents.length === 0) {
      window.setTimeout(eventListener, 0);
      return;
    }
    endEvents.forEach(function (endEvent) {
      addEventListener(node, endEvent, eventListener);
    });
  },

  removeEndEventListener: function removeEndEventListener(node, eventListener) {
    if (endEvents.length === 0) {
      return;
    }
    endEvents.forEach(function (endEvent) {
      removeEventListener(node, endEvent, eventListener);
    });
  }
};

exports.default = TransitionEvents;