// scripts/utils.js
import { BlockPermutation } from '@minecraft/server';

/** Return cardinal direction string used by your data (North/South/East/West) */
export function getCardinalDirection(player) {
  let yaw = player.getRotation().y % 360;
  if (yaw < 0) yaw += 360;
  if (yaw >= 315 || yaw < 45) return "South";
  if (yaw >= 45 && yaw < 135) return "West";
  if (yaw >= 135 && yaw < 225) return "North";
  return "East";
}

/** Safely run a command for a player (returns boolean) */
export function runLoad(player, cmd) {
  try {
    player.runCommand(cmd);
    return true;
  } catch {
    try { player.sendMessage("§cFailed to load structure."); } catch {}
    return false;
  }
}

/** Provide safe BlockPermutation.resolve helper (used by region.restore) */
export function resolvePermutation(typeId, states = {}) {
  try {
    return BlockPermutation.resolve(typeId, states);
  } catch {
    return BlockPermutation.resolve("minecraft:air");
  }
}
