import { LootTables } from "./loot-tables.js";
import { canPlaceMultiCell } from "./placement.js";

/**
 * @param {import("./types.js").GameState} state
 * @param {string} nurseryCellKey
 * @param {number} nowUnix
 * @returns {[false, string] | [true, import("./types.js").PendingBaby]}
 */
function getMatureBabyReady(state, nurseryCellKey, nowUnix) {
  const baby = (state.pendingBabies ?? []).find((p) => p.nurseryCellKey === nurseryCellKey);
  if (baby === null || baby === undefined) return [false, "NoBaby"];
  if (nowUnix < baby.readyAt) return [false, "BabyNotReady"];
  return [true, baby];
}

/**
 * @param {import("./types.js").GameState} state
 * @param {{ nurseryCellKey: string, toX: number, toY: number, nowUnix: number }} opts
 * @returns {[false, string] | [true, import("./types.js").AnimalCell]}
 */
export function getMatureBabyPlacementData(state, opts) {
  const { nurseryCellKey, toX, toY, nowUnix } = opts;
  const babyResult = getMatureBabyReady(state, nurseryCellKey, nowUnix);
  if (babyResult[0] === false) return [false, babyResult[1]];
  const baby = babyResult[1];
  const def = LootTables.Animals[baby.animalId];
  if (def === null || def === undefined) return [false, "UnknownAnimal"];
  const w = def.cellsWide ?? 1;
  const h = def.cellsHigh ?? 1;
  const [ok, reason] = canPlaceMultiCell(state, { originX: toX, originY: toY, w, h });
  if (!ok) return [false, reason ?? "NoPlace"];
  const animalData = {
    kind: "animal",
    id: baby.animalId,
    mutation: "none",
    level: 1,
    placedAt: nowUnix,
    lastVisitedAt: nowUnix,
    lastFedAt: nowUnix,
    cellsWide: w,
    cellsHigh: h,
    fromOtherZoo: baby.fromOtherZoo === true,
  };
  return [true, animalData];
}

/**
 * @param {import("./types.js").GameState} state
 * @param {string} receptionCellKey
 * @param {number} nowUnix
 * @returns {[false, string] | [true, import("./types.js").ReceptionAnimal]}
 */
function getReceptionAnimalReady(state, receptionCellKey, nowUnix) {
  const rec = (state.receptionAnimals ?? []).find((r) => r.receptionCellKey === receptionCellKey);
  if (rec === null || rec === undefined) return [false, "NoReceptionAnimal"];
  if (nowUnix < rec.readyAt) return [false, "AnimalNotReady"];
  return [true, rec];
}

/**
 * @param {import("./types.js").GameState} state
 * @param {{ receptionCellKey: string, toX: number, toY: number, nowUnix: number }} opts
 * @returns {[false, string] | [true, import("./types.js").AnimalCell]}
 */
export function getReceptionAnimalPlacementData(state, opts) {
  const { receptionCellKey, toX, toY, nowUnix } = opts;
  const recResult = getReceptionAnimalReady(state, receptionCellKey, nowUnix);
  if (recResult[0] === false) return [false, recResult[1]];
  const rec = recResult[1];
  const def = LootTables.Animals[rec.animalId];
  if (def === null || def === undefined) return [false, "UnknownAnimal"];
  const w = def.cellsWide ?? 1;
  const h = def.cellsHigh ?? 1;
  const [ok, reason] = canPlaceMultiCell(state, { originX: toX, originY: toY, w, h });
  if (!ok) return [false, reason ?? "NoPlace"];
  const animalData = {
    kind: "animal",
    id: rec.animalId,
    mutation: "none",
    level: 1,
    placedAt: nowUnix,
    lastVisitedAt: nowUnix,
    lastFedAt: nowUnix,
    cellsWide: w,
    cellsHigh: h,
    fromOtherZoo: true,
  };
  return [true, animalData];
}
