const CUNICULUM_BLOCKED_MAGNET_ITEMS = [
  'sophisticatedbackpacks:pickup_upgrade',
  'sophisticatedbackpacks:advanced_pickup_upgrade',
  'sophisticatedbackpacks:magnet_upgrade',
  'sophisticatedbackpacks:advanced_magnet_upgrade',
  'sophisticatedstorage:pickup_upgrade',
  'sophisticatedstorage:advanced_pickup_upgrade',
  'sophisticatedstorage:magnet_upgrade',
  'sophisticatedstorage:advanced_magnet_upgrade',
  'quark:magnet',
  'artifacts:universal_attractor',
  'lightmanscurrency:coin_chest_magnet_upgrade_1',
  'lightmanscurrency:coin_chest_magnet_upgrade_2',
  'lightmanscurrency:coin_chest_magnet_upgrade_3',
  'lightmanscurrency:coin_chest_magnet_upgrade_4'
];

const CUNICULUM_BLOCKED_OVERPOWER_ITEMS = [
  'artifacts:helium_flamingo'
];

const CUNICULUM_BLOCKED_MAGNET_RECIPE_IDS = [
  'sophisticatedbackpacks:pickup_upgrade',
  'sophisticatedbackpacks:advanced_pickup_upgrade',
  'sophisticatedbackpacks:magnet_upgrade',
  'sophisticatedbackpacks:advanced_magnet_upgrade',
  'sophisticatedbackpacks:advanced_magnet_upgrade_from_basic',
  'sophisticatedstorage:pickup_upgrade',
  'sophisticatedstorage:advanced_pickup_upgrade',
  'sophisticatedstorage:magnet_upgrade',
  'sophisticatedstorage:advanced_magnet_upgrade',
  'sophisticatedstorage:advanced_magnet_upgrade_from_basic',
  'sophisticatedstorage:backpack_pickup_upgrade_from_storage_pickup_upgrade',
  'sophisticatedstorage:backpack_advanced_pickup_upgrade_from_storage_advanced_pickup_upgrade',
  'sophisticatedstorage:storage_pickup_upgrade_from_backpack_pickup_upgrade',
  'sophisticatedstorage:storage_advanced_pickup_upgrade_from_backpack_advanced_pickup_upgrade',
  'sophisticatedstorage:backpack_magnet_upgrade_from_storage_magnet_upgrade',
  'sophisticatedstorage:backpack_advanced_magnet_upgrade_from_storage_advanced_magnet_upgrade',
  'sophisticatedstorage:storage_magnet_upgrade_from_backpack_magnet_upgrade',
  'sophisticatedstorage:storage_advanced_magnet_upgrade_from_backpack_advanced_magnet_upgrade',
  'quark:oddities/crafting/magnet',
  'quark:oddities/crafting/magnet_pre_end',
  'lightmanscurrency:upgrades/coin_chest_magnet_upgrade_1',
  'lightmanscurrency:upgrades/coin_chest_magnet_upgrade_2',
  'lightmanscurrency:upgrades/coin_chest_magnet_upgrade_3',
  'lightmanscurrency:upgrades/coin_chest_magnet_upgrade_4'
];

const CUNICULUM_BLOCKED_DIRECT_ENDGAME_ITEMS = [
  'minecraft:netherite_sword',
  'minecraft:netherite_axe',
  'minecraft:netherite_pickaxe',
  'minecraft:netherite_shovel',
  'minecraft:netherite_hoe',
  'minecraft:netherite_helmet',
  'minecraft:netherite_chestplate',
  'minecraft:netherite_leggings',
  'minecraft:netherite_boots'
];

function cuniculumIsFlyingPotionStack(stack) {
  try {
    var text = String(stack || '');
    return text.indexOf('apothic_attributes:flying') >= 0;
  } catch (ignored) {
  }
  return false;
}

ServerEvents.recipes(event => {
  CUNICULUM_BLOCKED_MAGNET_ITEMS.forEach(itemId => {
    event.remove({ output: itemId });
  });

  CUNICULUM_BLOCKED_OVERPOWER_ITEMS.forEach(itemId => {
    event.remove({ output: itemId });
  });

  CUNICULUM_BLOCKED_MAGNET_RECIPE_IDS.forEach(recipeId => {
    event.remove({ id: recipeId });
  });

  // Netherite is allowed through smithing progression. Removing by output also
  // removes the intended upgrade recipes, so these items are no longer blocked.
});

ItemEvents.rightClicked(event => {
  if (!event || !event.player || !cuniculumIsFlyingPotionStack(event.item)) {
    return;
  }

  event.cancel();
  event.server.runCommandSilent(`tellraw ${event.player.username} ${JSON.stringify({ text: '[CUNICULUM] Las pociones de vuelo quedan bloqueadas por progresion.', color: 'gold', italic: false })}`);
});

ServerEvents.tags('item', event => {
  CUNICULUM_BLOCKED_MAGNET_ITEMS.forEach(itemId => {
    event.add('kubejs:cuniculum_blocked_magnet_items', itemId);
  });

  CUNICULUM_BLOCKED_OVERPOWER_ITEMS.forEach(itemId => {
    event.add('kubejs:cuniculum_blocked_overpower_items', itemId);
  });

  CUNICULUM_BLOCKED_DIRECT_ENDGAME_ITEMS.forEach(itemId => {
    event.add('kubejs:cuniculum_blocked_direct_endgame_items', itemId);
  });
});
