const pickRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
const positionsMap = {
    left: 50,
    right: 550,
};
export class FlagsFactory {
    scene;
    possiblePositions = ["left", "right"];
    constructor(scene) {
        this.scene = scene;
    }
    create(group) {
        const position = pickRandom(this.possiblePositions);
        const flags = new Flags(this.scene, position);
        group.add(flags);
        return flags;
    }
}
export class Flags extends Phaser.Physics.Arcade.Sprite {
    scene;
    position;
    badAreaLeft;
    badAreaRight;
    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() {
        // Keep the display size as you originally set it.
        const scaleFactorY = this.scene.mountain?.scaleFactors.height || 1;
        const scaleFactorX = this.scene.mountain?.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.mountain?.height || 0), (this.scene.mountain?.height || 0) * 1.4);
        this.setY(distance * scaleFactorY);
        const flagsX = positionsMap[this.position] * scaleFactorX;
        const x = flagsX;
        this.setX(x);
        const mountainWidth = this.scene.mountain?.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.
    }
}
class Area extends Phaser.Physics.Arcade.Sprite {
    scene;
    heightScaled = 0;
    scaleFactor = 0;
    constructor(scene, x, y, width, height) {
        super(scene, x, y, "");
        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);
    }
}
