"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.sort.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _Star = require("../entities/Star");
var _Level = _interopRequireDefault(require("./Level"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const zoomToShardSizeMap = {
  0.5: 124 / 2,
  0.6: 150 / 2,
  0.75: 175 / 2,
  1: 240 / 2,
  1.25: 300 / 2,
  1.5: 365 / 2,
  1.75: 425 / 2
};
const levelToZoomMap = {
  0: 1,
  1: .75,
  2: .6,
  3: .5,
  4: .5
};

/**
 * Given an arbitrary zoom value and camera dimensions,
 * pick the nearest zoom grade from zoomToShardSizeMap
 * (clamping to min/max), then floor-divide camera dims
 * by that shard size to get how many shards fit.
 */
function fitShardsForZoom(zoom, camWidth, camHeight) {
  // 1) gather & sort your zoom grades
  const grades = Object.keys(zoomToShardSizeMap).map(Number).sort((a, b) => a - b);

  // 2) clamp zoom to [minGrade, maxGrade]
  const minGrade = grades[0];
  const maxGrade = grades[grades.length - 1];
  const clampedZoom = Math.min(maxGrade, Math.max(minGrade, zoom));

  // 3) find the greatest grade <= clampedZoom
  let usedZoomGrade = minGrade;
  for (const g of grades) {
    if (g <= clampedZoom) usedZoomGrade = g;else break;
  }

  // 4) lookup the pixel-size of one shard at that grade
  const shardSize = zoomToShardSizeMap[usedZoomGrade];

  // 5) how many whole shards fit in each axis?
  const countX = Math.max(1, Math.floor(camWidth / shardSize));
  const countY = Math.max(1, Math.floor(camHeight / shardSize));
  return {
    usedZoomGrade,
    shardSize,
    countX,
    countY
  };
}

// top‐of‐file constants
const SHARD_W = 16; // tiles per shard
const SHARD_H = 16;
const TILE_W = 8; // pixels per tile
const TILE_H = 8;
class MazeLevel extends _Level.default {
  constructor(config) {
    super(config || {
      key: 'mazelevel'
    });
    this.prefabShardWidth = SHARD_W * TILE_W; // 16 × 8 = 128
    this.prefabShardHeight = SHARD_H * TILE_H; // 16 × 8 = 128
    this.prefabMapWidth = 0;
    this.prefabMapHeight = 0;
  }
  zoomBasedOnLevel(level) {
    const zoom = levelToZoomMap[level] || 1;
    this.cameras.main.setZoom(zoom);
  }
  create() {
    super.create();
    this.addPlayer({
      x: this.startPosition.x,
      y: this.startPosition.y
    });
    this.createStars();
    this.handleStarCollisions();
    const prevW = this.maze.gridWidth || 0;
    const prevH = this.maze.gridHeight || 0;

    // --- use our new calculator ---
    const zoom = levelToZoomMap[this.level] || 1;
    const {
      countX,
      countY
    } = fitShardsForZoom(zoom, this.cameras.main.width, this.cameras.main.height);
    const mazeWidth = countX;
    const mazeHeight = countY;
    this.cameras.main.setZoom(zoom);
    this.maze.generate(mazeWidth, mazeHeight, this.level);
    if (this.level !== 0) this.maze.openTopLeft();
    this.maze.openBottomRight();

    // total map size in pixels
    this.prefabMapWidth = this.prefabShardWidth * this.maze.gridWidth;
    this.prefabMapHeight = this.prefabShardHeight * this.maze.gridHeight;

    // adjust camera & physics bounds
    this.cameras.main.setBounds(16, 0, this.prefabMapWidth - 32, this.prefabMapHeight);
    this.physics.world.setBounds(0, 0, this.prefabMapWidth, this.prefabMapHeight);

    // build your tilemap exactly as before
    const tiledata = this.maze.createMapData({
      key: 'map',
      shardW: SHARD_W,
      shardH: SHARD_H
    });
    const map = this.make.tilemap({
      data: tiledata,
      tileWidth: TILE_W,
      tileHeight: TILE_H
    });
    const tiles = map.addTilesetImage('tiles', 'tiles', TILE_W, TILE_H, 0, 0);
    const layer = map.createLayer(0, tiles || [], 0, 0);
    map.setCollisionBetween(192, 255);
    layer && this.player && this.physics.add.collider(this.player, layer);

    // exits
    this.addExit({
      scene: 'mazelevel',
      level: this.level - 1,
      x: 0,
      w: 32,
      y: 32,
      h: 64,
      startX: prevW * this.prefabShardWidth - 64,
      startY: prevH * this.prefabShardHeight - 64,
      facing: 'left'
    });
    this.addExit({
      scene: 'mazelevel',
      level: this.level + 1,
      x: this.prefabMapWidth - 32,
      w: 32,
      y: this.prefabMapHeight - 96,
      h: 64,
      startX: 64,
      startY: 64,
      facing: 'right'
    });
    this.cameras.main.setBackgroundColor('#85615a');
    this.randomlyAddBonusObject();
    this.postCreate();
  }
  setZoomBasedOnLevelAndScreenWidth() {
    const width = this.cameras.main.width;
    const height = this.cameras.main.height;
    const zoom = Math.min(width / this.prefabMapWidth, height / this.prefabMapHeight);
    this.cameras.main.setZoom(zoom);
  }
  randomlyAddBonusObject() {
    const col = Math.floor(Math.random() * this.maze.gridWidth);
    const row = Math.floor(Math.random() * this.maze.gridHeight);

    // const graphics = this.add.graphics();
    // graphics.fillStyle(0xff00ff, 0.5); // sets the fill color and alpha
    // graphics.fillRect(
    //     col * this.prefabShardWidth, row * this.prefabShardHeight, 
    //     this.prefabShardWidth, this.prefabShardHeight
    // );
    // graphics.fillPath(); 

    const starFactory = new _Star.StarFactory(this);
    this.stars && starFactory.create(this.stars, col * this.prefabShardWidth + this.prefabShardWidth / 2, row * this.prefabShardHeight + this.prefabShardHeight / 2);
  }
  update(time, delta) {
    var _this$player;
    (_this$player = this.player) === null || _this$player === void 0 || _this$player.update(this.controls, time, delta);
    this.checkExits();
  }
}
var _default = exports.default = MazeLevel;