"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TreeFactory = exports.Tree = void 0;
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const pickRandom = arr => arr[Math.floor(Math.random() * arr.length)];
const positionsMap = {
  left: {
    0: 0,
    1: 100,
    2: 200,
    3: 300
  },
  middle: {
    0: 500,
    1: 600,
    2: 700,
    3: 800
  },
  right: {
    0: 1000,
    1: 1100,
    2: 1200,
    3: 1300
  },
  'left-middle': {
    0: 200,
    1: 200,
    2: 500,
    3: 500
  },
  'right-middle': {
    0: 1100,
    1: 1100,
    2: 1300,
    3: 1300
  }
};
class TreeFactory {
  constructor(scene) {
    _defineProperty(this, "possiblePositions", ["left", "middle", "right", "left-middle", "right-middle"]);
    this.scene = scene;
  }
  create(obstaclesGroup) {
    const groupYPosition = Phaser.Math.Between(this.scene.scale.height, this.scene.scale.height * 1.8);
    const numberOfTrees = Math.floor(Math.random() * 4) + 1;
    const position = pickRandom(this.possiblePositions);
    for (let i = 0; i < numberOfTrees; i++) {
      const tree = new Tree(this.scene, position, i, groupYPosition);
      obstaclesGroup.add(tree);
    }
  }
}
exports.TreeFactory = TreeFactory;
class Tree extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, position, place, groupYPosition) {
    const x = 0;
    const y = 0;
    super(scene, x, y, "tree");
    this.scene = scene;
    this.position = position;
    this.place = place;
    this.groupYPosition = groupYPosition;
    scene.add.existing(this);
    scene.physics.add.existing(this);
    this.init();
    this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
  }
  init() {
    this.setOrigin(0, 1).setCollideWorldBounds(true).setBodySize(150, 250).setOffset(25, 15).setDepth(2);

    // Handle resizing
    this.scene.scale.once('resize', this.scaleToFit, this);
    this.scene.scale.once('resize', this.positionOnMountain, this);
    this.scaleToFit();
    this.positionOnMountain();
  }
  positionOnMountain() {
    var _this$scene$mountain;
    const scaleFactorX = ((_this$scene$mountain = this.scene.mountain) === null || _this$scene$mountain === void 0 ? void 0 : _this$scene$mountain.scaleFactors.width) || 1;
    this.setX(positionsMap[this.position][this.place] * scaleFactorX);
    this.setY(this.groupYPosition);
  }
  scaleToFit() {
    var _this$scene$mountain2;
    // Use the scale factor from the Mountain class to maintain consistency
    const scaleFactor = ((_this$scene$mountain2 = this.scene.mountain) === null || _this$scene$mountain2 === void 0 ? void 0 : _this$scene$mountain2.scaleFactors.height) || 1;
    const width = 142;
    const height = 204;

    // Apply the scale factor to the Tree
    this.setDisplaySize(width * scaleFactor, height * scaleFactor);
  }
  update() {}
}
exports.Tree = Tree;