// priority: 0

//https://kubejs.com/wiki/addons/built-in/rei

REIEvents.hide("item", (e) => {
  e.hide("chiselsandbits:block_bit");
});

REIEvents.groupEntries((event) => {
  event.groupItems("kubejs:rei_groups/spawn_eggs", "Spawn Eggs", [
    /^(.*)spawn_egg/,
  ]);
  const useNbt = [
    "minecraft:potion",
    "minecraft:enchanted_book",
    "minecraft:splash_potion",
    "minecraft:tipped_arrow",
    "minecraft:suspicious_stew",
    "minecraft:goat_horn",
    "minecraft:firework_rocket",
    "minecraft:lingering_potion",
    "minecraft:painting",
    "minecraft:firework_star",
    "bloodmagic:upgradetome",
    "sophisticatedbackpacks:backpack",
    "enderio:broken_spawner",
    "enderio:filled_soul_vial",
    "quark:seed_pouch",
  ];

  ["chest", "barrel", "shulker_box"].forEach((id) => {
    useNbt.push(`sophisticatedstorage:${id}`);
    useNbt.push(`sophisticatedstorage:copper_${id}`);
    useNbt.push(`sophisticatedstorage:iron_${id}`);
    useNbt.push(`sophisticatedstorage:gold_${id}`);
    useNbt.push(`sophisticatedstorage:diamond_${id}`);
    useNbt.push(`sophisticatedstorage:netherite_${id}`);
  });
  for (let i = 1; i <= 4; i++) {
    useNbt.push(`sophisticatedstorage:limited_barrel_${i}`);
    useNbt.push(`sophisticatedstorage:limited_iron_barrel_${i}`);
    useNbt.push(`sophisticatedstorage:limited_copper_barrel_${i}`);
    useNbt.push(`sophisticatedstorage:limited_gold_barrel_${i}`);
    useNbt.push(`sophisticatedstorage:limited_diamond_barrel_${i}`);
    useNbt.push(`sophisticatedstorage:limited_netherite_barrel_${i}`);
  }

  useNbt.forEach((id) => {
    const item = Item.of(id);
    const { namespace, path } = Utils.id(item.id);
    event.groupSameItem(
      `kubejs:rei_groups/${namespace}/${path}`,
      item.id,
      item
    );
  });
});

function groupModTags(modName, event, exclude) {
  const modIngredient = Ingredient.of(`@${modName}`);

  REIEvents.hide("item", (e) => {
    e.hide("chiselsandbits:block_bit");
  });

  REIEvents.groupEntries((event) => {
    event.groupItems("kubejs:rei_groups/spawn_eggs", "Spawn Eggs", [
      /^(.*)spawn_egg/,
    ]);
    const useNbt = [
      "minecraft:potion",
      "minecraft:enchanted_book",
      "minecraft:splash_potion",
      "minecraft:tipped_arrow",
      "minecraft:suspicious_stew",
      "minecraft:goat_horn",
      "minecraft:firework_rocket",
      "minecraft:lingering_potion",
      "minecraft:painting",
      "minecraft:firework_star",
      "bloodmagic:upgradetome",
      "sophisticatedbackpacks:backpack",
      "enderio:broken_spawner",
      "enderio:filled_soul_vial",
      "quark:seed_pouch",
    ];

    ["chest", "barrel", "shulker_box"].forEach((id) => {
      useNbt.push(`sophisticatedstorage:${id}`);
      useNbt.push(`sophisticatedstorage:copper_${id}`);
      useNbt.push(`sophisticatedstorage:iron_${id}`);
      useNbt.push(`sophisticatedstorage:gold_${id}`);
      useNbt.push(`sophisticatedstorage:diamond_${id}`);
      useNbt.push(`sophisticatedstorage:netherite_${id}`);
    });
    for (let i = 1; i <= 4; i++) {
      useNbt.push(`sophisticatedstorage:limited_barrel_${i}`);
      useNbt.push(`sophisticatedstorage:limited_iron_barrel_${i}`);
      useNbt.push(`sophisticatedstorage:limited_copper_barrel_${i}`);
      useNbt.push(`sophisticatedstorage:limited_gold_barrel_${i}`);
      useNbt.push(`sophisticatedstorage:limited_diamond_barrel_${i}`);
      useNbt.push(`sophisticatedstorage:limited_netherite_barrel_${i}`);
    }

    useNbt.forEach((id) => {
      const item = Item.of(id);
      const { namespace, path } = Utils.id(item.id);
      event.groupSameItem(
        `kubejs:rei_groups/${namespace}/${path}`,
        item.id,
        item
      );
    });
  });

  function groupModTags(modName, event, exclude) {
    const modIngredient = Ingredient.of(`@${modName}`);

    modIngredient.stacks
      .toList()
      .stream()
      .flatMap((stack) => stack.tags)
      .distinct() // Exclude duplicate tag entries
      .filter((tag) => tag.toString().indexOf(modName) !== -1)
      .forEach((tag) => {
        const path = new String(
          tag.toString().split(`${modName}:`)[1].split("]")[0]
        );
        // Human-readable title case, e.g white_wool -> White Wool
        const name = path
          .replace(/^(\w)/, (c) => c.toUpperCase())
          .replace(/_(\w)/g, (_, c) => " " + c.toUpperCase())
          .toString();

        if (!exclude)
          event.groupItemsByTag(
            `kubejs:rei_groups/${modName}/${path}`,
            name,
            `${modName}:${path}`
          );
        else {
          let items = Ingredient.of(`#${modName}:${path}`).itemIds.filter(
            (item) => Item.of(item).getMod() == `${modName}`
          );
          event.groupItems(`kubejs:rei_groups/${modName}/${path}`, name, items);
        }
      });
  }

  REIEvents.groupEntries((event) => {
    groupModTags("chipped", event, true);
    groupModTags("enderio", event, true);
    groupModTags("botanypots", event, true);
    groupModTags("design_decor", event, true);
    groupModTags("xtonesreworked", event, true);
    groupModTags("refinedstorage", event, true);
    groupModTags("buildersdelight", event, true);
    groupModTags("dramaticdoors", event, true);
  });

  REIEvents.groupEntries((event) => {
    let rechiseledBlocks = [
      "amethyst",
      "basalt",
      "bamboo",
      "blue_ice",
      "bone_block",
      "cherry_planks",
      "coal_block",
      "copper_block",
      "diamond_block",
      "emerald_block",
      "gold_block",
      "iron_block",
      "lapis_block",
      "mangrove_planks",
      "netherite_block",
      "redstone_block",
      "acacia_planks",
      "andesite",
      "blackstone",
      "cobbled",
      "birch_planks",
      "cobblestone",
      "crimson_planks",
      "dark_oak_planks",
      "dark_prismarine",
      "diorite",
      "dirt",
      "end_stone",
      "glowstone",
      "granite",
      "jungle_planks",
      "mossy_cobblestone",
      "netherrack",
      "nether_bricks",
      "oak_planks",
      "obsidian",
      "prismarine_bricks",
      "purpur",
      "quartz_block",
      "red_nether_bricks",
      "red_sandstone",
      "sandstone",
      "spruce_planks",
      "stone",
      "warped_planks",
    ];

    rechiseledBlocks.forEach((type) => {
      var rsRegex = new RegExp(`rechiseled:${type}.*`);
      let itemName = toMultiTitleCase(type);
      if (itemName.charAt(itemName.length - 1) == "s") {
        event.groupItems(
          `rechiseled:rei_groups/${type}`,
          `${toMultiTitleCase(type)}`,
          rsRegex
        );
      } else {
        event.groupItems(
          `rechiseled:rei_groups/${type}`,
          `${toMultiTitleCase(type)}s`,
          rsRegex
        );
      }
    });
  });

  function toMultiTitleCase(str) {
    var i,
      frags = str.split("_");
    for (i = 0; i < frags.length; i++) {
      frags[i] = frags[i].charAt(0).toUpperCase() + frags[i].slice(1);
    }
    return frags.join(" ");
  }
}
