"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _event = _interopRequireDefault(require("./event"));

var _dom = require("../dom");

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

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

var prefixes = ["-webkit-", "-moz-", "-o-", "ms-", ""];

function getStyleProperty(node, name) {
  // old ff need null, https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
  var style = window.getComputedStyle(node, null);
  var ret = "";

  for (var i = 0; i < prefixes.length; i++) {
    ret = style.getPropertyValue(prefixes[i] + name);

    if (ret) {
      break;
    }
  }

  return ret;
}

function checkAnimation(node) {
  var transitionDelay = parseFloat(getStyleProperty(node, "transition-delay")) || 0;
  var transitionDuration = parseFloat(getStyleProperty(node, "transition-duration")) || 0;
  var animationDelay = parseFloat(getStyleProperty(node, "animation-delay")) || 0;
  var animationDuration = parseFloat(getStyleProperty(node, "animation-duration")) || 0;

  if (transitionDelay || transitionDuration || animationDelay || animationDuration) {
    return;
  }

  setTimeout(function () {
    node.endAniListener && node.endAniListener();
  }, 0);
}

var animation = function animation(node, name, callback) {
  var className = name;
  var activeName;
  var start;
  var end = callback;

  if (name && _typeof(name) === "object") {
    className = name.name;
    activeName = name.active;
  } else {
    activeName = "".concat(name, "-active");
  }

  if (callback && _typeof(callback) === "object") {
    start = callback.start;
    end = callback.end;
  }

  if (node.endAniListener) {
    node.endAniListener();
  }

  if (node.activeTimer) {
    clearTimeout(node.activeTimer);
    node.activeTimer = null;
  }

  node.endAniListener = function (e) {
    if (e && e.target != node) return;
    (0, _dom.removeClass)(node, className);
    (0, _dom.removeClass)(node, activeName);

    _event["default"].removeEndListener(node, node.endAniListener);

    node.endAniListener = null;
    end && end();
  };

  _event["default"].addEndListener(node, node.endAniListener);

  (0, _dom.addClass)(node, className);
  node.activeTimer = setTimeout(function () {
    (0, _dom.addClass)(node, activeName);
    checkAnimation(node);
  }, 30);
  start && start();
  return {
    stop: function stop() {
      node.endAniListener && node.endAniListener();
    }
  };
};

var _default = animation;
exports["default"] = _default;