import { GameConfig } from "./config.js";

/**
 * @param {number} x
 * @param {number} y
 * @returns {string}
 */
export function cellKey(x, y) {
  return `${x}_${y}`;
}

/**
 * @param {number} plotLevel
 * @returns {[number, number]}
 */
export function plotSizeFromLevel(plotLevel) {
  const level = Math.max(1, Math.min(GameConfig.Plot.MaxLevel, plotLevel));
  const extra = (level - 1) * GameConfig.Plot.ExpandByLevel;
  return [
    GameConfig.Plot.BaseWidth + extra,
    GameConfig.Plot.BaseHeight + extra,
  ];
}

/**
 * @param {number} width
 * @param {number} height
 * @param {number} x
 * @param {number} y
 * @returns {boolean}
 */
export function withinBounds(width, height, x, y) {
  return x >= 1 && y >= 1 && x <= width && y <= height;
}

/**
 * Keys for a rectangular block (origin = top-left). All coordinates 1-based.
 * @param {number} originX
 * @param {number} originY
 * @param {number} w
 * @param {number} h
 * @returns {string[]}
 */
export function getBlockKeys(originX, originY, w, h) {
  const keys = [];
  for (let dy = 0; dy < h; dy++) {
    for (let dx = 0; dx < w; dx++) {
      keys.push(cellKey(originX + dx, originY + dy));
    }
  }
  return keys;
}

/**
 * @param {string} key "x_y"
 * @param {import("./types.js").Cell} cell
 * @returns {boolean}
 */
export function isOriginCell(key, cell) {
  if (cell === null || cell === undefined || cell.kind !== "animal") return false;
  return cell.originKey === null || cell.originKey === undefined || cell.originKey === key;
}
