import _extends from "@babel/runtime/helpers/esm/extends";
var defaultOptions = {
  duration: 150,
  delay: 0,
  iterations: 1,
  easing: 'ease-in'
};
var animations = new WeakMap();

/**
 * Мерцание при вводе недоступных символов
 *
 * @param {Object} args
 * @param {HTMLElement|null} args.el Элемент, к которому применяется эффект.
 * @param {string} args.blinkColor Цвет мерцания в любом допустимом CSS-формате.
 * @param {KeyframeAnimationOptions} args.options Опции анимации (timing, duration и т.д.).
 *
 * @return {void}
 */
export function blink(_ref) {
  var _el$dataset$originalT, _el$dataset$originalB;
  var el = _ref.el,
    blinkColor = _ref.blinkColor,
    _ref$options = _ref.options,
    options = _ref$options === void 0 ? {} : _ref$options;
  if (!el) {
    return;
  }
  var config = _extends({}, defaultOptions, options);
  if ('animate' in el && typeof el.animate === 'function') {
    var _animations$get;
    (_animations$get = animations.get(el)) == null || _animations$get.cancel();
    animations.set(el, el.animate([{
      backgroundColor: blinkColor
    }, {
      backgroundColor: 'inherit'
    }], config));
    return;
  }

  // Fallback Safari <13.1
  el.dataset.originalTransition = (_el$dataset$originalT = el.dataset.originalTransition) != null ? _el$dataset$originalT : el.style.transition;
  el.dataset.originalBackgroundColor = (_el$dataset$originalB = el.dataset.originalBackgroundColor) != null ? _el$dataset$originalB : el.style.backgroundColor;
  clearTimeout(el.dataset.timeoutId);
  el.style.transition = el.dataset.originalTransition;
  el.style.backgroundColor = blinkColor;
  setTimeout(function () {
    var _config$duration;
    if (!el) {
      return;
    }
    el.style.transition = "background-color " + config.duration + "ms " + config.delay + "ms " + config.easing;
    el.style.backgroundColor = 'inherit';
    el.dataset.timeoutId = setTimeout(function () {
      var _el$dataset$originalT2, _el$dataset$originalB2;
      if (!el) {
        return;
      }
      el.style.transition = (_el$dataset$originalT2 = el.dataset.originalTransition) != null ? _el$dataset$originalT2 : '';
      el.style.backgroundColor = (_el$dataset$originalB2 = el.dataset.originalBackgroundColor) != null ? _el$dataset$originalB2 : '';
    }, parseInt(((_config$duration = config.duration) == null ? void 0 : _config$duration.toString()) || '0')).toString();
  }, 1);
}