"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var PI = Math.PI; // const defaultEmoji = [
//   '👍',
//   '😊',
//   '🎉'
// ]

var createElements = function createElements(root, elementCount, elementSize, zIndex, _ref) {
  var _ref$emoji = _ref.emoji,
      emoji = _ref$emoji === void 0 ? [] : _ref$emoji,
      _ref$images = _ref.images,
      images = _ref$images === void 0 ? [] : _ref$images;
  return Array.from({
    length: elementCount
  }).map(function (_, index) {
    var i = index % (emoji.length + images.length);
    var element;

    if (i < emoji.length) {
      element = document.createElement("span");
      element.innerHTML = emoji[i];
      element.style.fontSize = "".concat(elementSize, "px");
      element.style.position = "absolute";
      element.style.zIndex = zIndex;
      root.appendChild(element);
    } else {
      element = document.createElement("img");
      element.style.width = "".concat(elementSize, "px");
      element.style.height = "".concat(elementSize, "px");
      element.src = images[i];
      element.style.position = "absolute";
      root.appendChild(element);
    }

    element.style.pointerEvents = "none";
    return element;
  });
};

var radiansFrom = function radiansFrom(degrees) {
  return degrees * (PI / 180);
};

var generatePhysics = function generatePhysics(angle, spread, startVelocity, random, x, y) {
  var radAngle = radiansFrom(angle);
  var radSpread = radiansFrom(spread);
  return {
    x: x,
    y: y,
    wobble: random() * 10,
    velocity: startVelocity * 0.5 + random() * startVelocity,
    angle2D: -radAngle + (0.5 * radSpread - random() * radSpread),
    angle3D: -(PI / 4) + random() * (PI / 2),
    tiltAngle: random() * PI,
    velocityx: startVelocity * 0.5 + random() * startVelocity
  };
};

var updateMojis = function updateMojis(fetti, progress, decayY, decayX) {
  fetti.physics.x += Math.cos(fetti.physics.angle2D) * fetti.physics.velocityx;
  fetti.physics.y += Math.sin(fetti.physics.angle2D) * fetti.physics.velocity;
  fetti.physics.wobble += 0;
  fetti.physics.velocity *= decayY;
  fetti.physics.velocityx *= decayX;
  fetti.physics.y += 15 * progress;
  fetti.physics.tiltAngle += 0.1;
  var _fetti$physics = fetti.physics,
      x = _fetti$physics.x,
      y = _fetti$physics.y,
      tiltAngle = _fetti$physics.tiltAngle;
  var wobbleX = x + 0;
  var wobbleY = y + 0;
  var transform = "translate3d(".concat(wobbleX, "px, ").concat(wobbleY, "px, 0) rotate(").concat(tiltAngle, "rad)");
  fetti.element.style.transform = transform;
  fetti.element.style.opacity = progress > 0.5 ? (1 - progress) * 2 : 1;
};

var animate = function animate(root, mojis, decayY, decayX, lifetime) {
  return new Promise(function (res) {
    var totalTicks = lifetime;
    var tick = 0;

    var update = function update() {
      mojis.forEach(function (fetti) {
        return updateMojis(fetti, tick / totalTicks, decayY, decayX);
      });
      tick += 1;

      if (tick < totalTicks) {
        window.requestAnimationFrame(update);
      } else {
        res();
      }
    };

    window.requestAnimationFrame(update);
  });
};

var shoot = function shoot(root, x, y) {
  var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
      _ref2$angle = _ref2.angle,
      angle = _ref2$angle === void 0 ? 90 : _ref2$angle,
      _ref2$decayY = _ref2.decayY,
      decayY = _ref2$decayY === void 0 ? 0.9 : _ref2$decayY,
      _ref2$decayX = _ref2.decayX,
      decayX = _ref2$decayX === void 0 ? 0.98 : _ref2$decayX,
      _ref2$spread = _ref2.spread,
      spread = _ref2$spread === void 0 ? 90 : _ref2$spread,
      _ref2$startVelocity = _ref2.startVelocity,
      startVelocity = _ref2$startVelocity === void 0 ? 35 : _ref2$startVelocity,
      _ref2$elementCount = _ref2.elementCount,
      elementCount = _ref2$elementCount === void 0 ? 20 : _ref2$elementCount,
      _ref2$elementSize = _ref2.elementSize,
      elementSize = _ref2$elementSize === void 0 ? 25 : _ref2$elementSize,
      _ref2$lifetime = _ref2.lifetime,
      lifetime = _ref2$lifetime === void 0 ? 150 : _ref2$lifetime,
      _ref2$zIndex = _ref2.zIndex,
      zIndex = _ref2$zIndex === void 0 ? 0 : _ref2$zIndex,
      _ref2$emoji = _ref2.emoji,
      emoji = _ref2$emoji === void 0 ? [] : _ref2$emoji,
      _ref2$random = _ref2.random,
      random = _ref2$random === void 0 ? Math.random : _ref2$random,
      images = _ref2.images;

  var elements = createElements(root, elementCount, elementSize, zIndex, {
    images: images,
    emoji: emoji
  });
  var mojis = elements.map(function (element) {
    return {
      element: element,
      physics: generatePhysics(angle, spread, startVelocity, random, x, y)
    };
  });
  return animate(root, mojis, decayY, decayX, lifetime);
};

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