"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FlagsFactory = exports.Flags = 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: 70,
  middle: 1000,
  right: 1900,
  'left-middle': 500,
  'right-middle': 1500
};
class FlagsFactory {
  constructor(scene) {
    _defineProperty(this, "possiblePositions", ["left", "middle", "right", "left-middle", "right-middle"]);
    this.scene = scene;
  }
  create(group) {
    const position = pickRandom(this.possiblePositions);
    const flags = new Flags(this.scene, position);
    group.add(flags);
    // Testing
    // {
    //     const position = "right";
    //     const flags = new Flags(this.scene, position);
    //     group.add(flags);
    //     return flags;
    // }

    return flags;
  }
}
exports.FlagsFactory = FlagsFactory;
class Flags extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, position) {
    const x = 0;
    const y = 0;
    super(scene, x, y, "flags");
    this.scene = scene;
    this.position = position;
    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).setDepth(2);

    // this.scene.scale.once('resize', this.positionOnMountain, this);

    this.positionOnMountain();
  }
  positionOnMountain() {
    var _this$scene$mountain, _this$scene$mountain2, _this$scene$mountain3, _this$scene$mountain4, _this$scene$mountain5;
    // Keep the display size as you originally set it.
    const scaleFactorY = ((_this$scene$mountain = this.scene.mountain) === null || _this$scene$mountain === void 0 ? void 0 : _this$scene$mountain.scaleFactors.height) || 1;
    const scaleFactorX = ((_this$scene$mountain2 = this.scene.mountain) === null || _this$scene$mountain2 === void 0 ? void 0 : _this$scene$mountain2.scaleFactors.width) || 1;
    const flagWidth = 480;
    const flagHeight = 240;
    this.setDisplaySize(flagWidth * scaleFactorX, flagHeight * scaleFactorY);
    this.body.setSize(flagWidth / 2, 10);
    this.body.setOffset(0, flagHeight / 2);
    const distance = Phaser.Math.Between(((_this$scene$mountain3 = this.scene.mountain) === null || _this$scene$mountain3 === void 0 ? void 0 : _this$scene$mountain3.height) || 0, (((_this$scene$mountain4 = this.scene.mountain) === null || _this$scene$mountain4 === void 0 ? void 0 : _this$scene$mountain4.height) || 0) * 1.4);
    this.setY(distance * scaleFactorY);
    const flagsX = positionsMap[this.position] * scaleFactorX;
    const x = flagsX;
    this.setX(x);
    const mountainWidth = ((_this$scene$mountain5 = this.scene.mountain) === null || _this$scene$mountain5 === void 0 ? void 0 : _this$scene$mountain5.width) || 0;

    // Create bad areas
    this.badAreaLeft = new Area(this.scene, 0, this.y - 10, this.x, 10);
    this.badAreaRight = new Area(this.scene, this.x + (this.width + 80) * scaleFactorX, this.y - 10, mountainWidth, 10);
  }
  update() {
    // Optionally update safe zone if needed on resize.
  }
}
exports.Flags = Flags;
class Area extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x, y, width, height) {
    super(scene, x, y, "");
    _defineProperty(this, "heightScaled", 0);
    _defineProperty(this, "scaleFactor", 0);
    this.width = width;
    this.height = height;
    this.scene = scene;
    scene.add.existing(this);
    scene.physics.add.existing(this, false);
    this.init();
  }
  init() {
    this.setOrigin(0, 0).setVisible(false).setDepth(2).setDisplaySize(this.width, this.height);
    // Handle resizing
    // this.scene.scale.once('resize', this.positionOnMountain, this);
  }

  // positionOnMountain() {
  //     this.setDisplaySize(this.width, 10);

  //     this.setY(0);
  //     this.setX(this.scene.scale.width - 10);
  // }
}