import { world, BlockPermutation } from "@minecraft/server";

export default function stripLog(log, fruit, player, logID) {
  const invalidStates = ["old_log_type", "new_log_type"];
  let logPerm, strippedlog, newPermutation, states, key, value;
  logPerm = log.permutation;
  strippedlog = logID.replace(/:(\w+_)/, ":stripped_$1");
  try {
    if (logID.startsWith("minecraft:")) {
      // If it's a vanilla Minecraft log, copy the current permutation and change the block type
      newPermutation = BlockPermutation.resolve(strippedlog);
      states = logPerm.getAllStates();

      for ([key, value] of Object.entries(states)) {
        if (!invalidStates.includes(key)) {
          newPermutation = newPermutation.withState(key, value);
        }
      }
    } else {
      // If it's a custom log, handle setting the block_face
      newPermutation = BlockPermutation.resolve(strippedlog, {
        "minecraft:block_face": logPerm.getState("minecraft:block_face"),
      });
    }

    if (newPermutation) {
      log.setPermutation(newPermutation);
    }
  } catch {}
  newPermutation = logPerm = strippedlog = states = key = value = undefined;
}
