import { ItemStack } from "@minecraft/server";
import { world, namespace as ns } from "./eternal/constants";
import { runInterval } from "./eternal/utils";

const itemsToReplace = [
  {
    itemIds: [`${ns}:lilypad_vanilla_1`, `${ns}:lilypad_vanilla_2`, `${ns}:lilypad_vanilla_3`],
    replacementId: `${ns}:lilypad_vanilla`,
  },
  {
    itemIds: [
      `${ns}:lilypad_fake_box_n`,
      `${ns}:lilypad_fake_box_ne`,
      `${ns}:lilypad_fake_box_e`,
      `${ns}:lilypad_fake_box_se`,
      `${ns}:lilypad_fake_box_s`,
      `${ns}:lilypad_fake_box_sw`,
      `${ns}:lilypad_fake_box_w`,
      `${ns}:lilypad_fake_box_nw`,
    ],
    replacementId: `${ns}:lilypad_giant`,
  },
];

runInterval(() => {
  for (const player of world.players) {
    for (const obj of itemsToReplace) {
      for (const id of obj.itemIds) {
        const emap = player.getItems(id).equipments;
        const imap = player.getItems(id).inventory;

        const item = new ItemStack(obj.replacementId);

        for (const [k, v] of emap) {
          player.setEquipment(k, item);
        }

        for (const [k, v] of imap) {
          player.inventory.setItem(k, item);
        }
      }
    }
  }
});
