text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.EmitterInstance = void 0;
  const Utils_1 = require("../../Utils");
  const Enums_1 = require("../../Enums");
  const EmitterSize_1 = require("./Options/Classes/EmitterSize");
  class EmitterInstance {
      constructor(emitters, container, emitterOptions, position) {
          var _a, _b, _c;
          this.emitters = emitters;
          this.container = container;
          this.initialPosition = position;
          this.emitterOptions = Utils_1.Utils.deepExtend({}, emitterOptions);
          this.position = (_a = this.initialPosition) !== null && _a !== void 0 ? _a : this.calcPosition();
          let particlesOptions = Utils_1.Utils.deepExtend({}, this.emitterOptions.particles);
          if (particlesOptions === undefined) {
              particlesOptions = {};
          }
          if (particlesOptions.move === undefined) {
              particlesOptions.move = {};
          }
          if (particlesOptions.move.direction === undefined) {
              particlesOptions.move.direction = this.emitterOptions.direction;
          }
          this.particlesOptions = particlesOptions;
          this.size = (_b = this.emitterOptions.size) !== null && _b !== void 0 ? _b : (() => {
              const size = new EmitterSize_1.EmitterSize();
              size.load({
                  height: 0,
                  mode: Enums_1.SizeMode.percent,
                  width: 0,
              });
              return size;
          })();
          this.lifeCount = (_c = this.emitterOptions.life.count) !== null && _c !== void 0 ? _c : -1;
          this.immortal = this.lifeCount <= 0;
          this.play();
      }
      play() {
          if (this.lifeCount > 0 || this.immortal || !this.emitterOptions.life.count) {
              if (this.startInterval === undefined) {
                  this.startInterval = window.setInterval(() => {
                      this.emit();
                  }, 1000 * this.emitterOptions.rate.delay);
              }
              if (this.lifeCount > 0 || this.immortal) {
                  this.prepareToDie();
              }
          }
      }
      pause() {
          const interval = this.startInterval;
          if (interval !== undefined) {
              clearInterval(interval);
              delete this.startInterval;
          }
      }
      resize() {
          const initialPosition = this.initialPosition;
          this.position =
              initialPosition && Utils_1.Utils.isPointInside(initialPosition, this.container.canvas.size)
                  ? initialPosition
                  : this.calcPosition();
      }
      prepareToDie() {
          var _a;
          const duration = (_a = this.emitterOptions.life) === null || _a === void 0 ? void 0 : _a.duration;
          if ((this.lifeCount > 0 || this.immortal) && duration !== undefined && duration > 0) {
              window.setTimeout(() => {
                  var _a;
                  this.pause();
                  if (!this.immortal) {
                      this.lifeCount--;
                  }
                  if (this.lifeCount > 0 || this.immortal) {
                      this.position = this.calcPosition();
                      window.setTimeout(() => {
                          this.play();
                      }, ((_a = this.emitterOptions.life.delay) !== null && _a !== void 0 ? _a : 0) * 1000);
                  }
                  else {
                      this.destroy();
                  }
              }, duration * 1000);
          }
      }
      destroy() {
          this.emitters.removeEmitter(this);
      }
      calcPosition() {
          var _a;
          const container = this.container;
          const percentPosition = (_a = this.emitterOptions.position) !== null && _a !== void 0 ? _a : {
              x: Math.random() * 100,
              y: Math.random() * 100,
          };
          return {
              x: (percentPosition.x / 100) * container.canvas.size.width,
              y: (percentPosition.y / 100) * container.canvas.size.height,
          };
      }
      emit() {
          const container = this.container;
          const position = this.position;
          const offset = {
              x: this.size.mode === Enums_1.SizeMode.percent
                  ? (container.canvas.size.width * this.size.width) / 100
                  : this.size.width,
              y: this.size.mode === Enums_1.SizeMode.percent
                  ? (container.canvas.size.height * this.size.height) / 100
                  : this.size.height,
          };
          for (let i = 0; i < this.emitterOptions.rate.quantity; i++) {
              container.particles.addParticle({
                  x: position.x + offset.x * (Math.random() - 0.5),
                  y: position.y + offset.y * (Math.random() - 0.5),
              }, this.particlesOptions);
          }
      }
  }
  exports.EmitterInstance = EmitterInstance;
  