package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import java.util.Iterator;
import net.minecraft.world.level.saveddata.maps.MapDecoration;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.resources.ResourceLocation;

public class MapExtendingRecipe extends ShapedRecipe {
    public MapExtendingRecipe(final ResourceLocation sm) {
        super(sm, "", 3, 3, NonNullList.<Ingredient>of(Ingredient.EMPTY, new Ingredient[] { Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER), Ingredient.of(Items.FILLED_MAP), Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER), Ingredient.of(Items.PAPER) }), new ItemStack(Items.MAP));
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        if (!super.matches(baz, bjw)) {
            return false;
        }
        ItemStack ben4 = ItemStack.EMPTY;
        for (int integer5 = 0; integer5 < baz.getContainerSize() && ben4.isEmpty(); ++integer5) {
            final ItemStack ben5 = baz.getItem(integer5);
            if (ben5.getItem() == Items.FILLED_MAP) {
                ben4 = ben5;
            }
        }
        if (ben4.isEmpty()) {
            return false;
        }
        final MapItemSavedData crg5 = MapItem.getOrCreateSavedData(ben4, bjw);
        return crg5 != null && !this.isExplorationMap(crg5) && crg5.scale < 4;
    }
    
    private boolean isExplorationMap(final MapItemSavedData crg) {
        if (crg.decorations != null) {
            for (final MapDecoration crd4 : crg.decorations.values()) {
                if (crd4.getType() == MapDecoration.Type.MANSION || crd4.getType() == MapDecoration.Type.MONUMENT) {
                    return true;
                }
            }
        }
        return false;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        ItemStack ben3 = ItemStack.EMPTY;
        for (int integer4 = 0; integer4 < baz.getContainerSize() && ben3.isEmpty(); ++integer4) {
            final ItemStack ben4 = baz.getItem(integer4);
            if (ben4.getItem() == Items.FILLED_MAP) {
                ben3 = ben4;
            }
        }
        ben3 = ben3.copy();
        ben3.setCount(1);
        ben3.getOrCreateTag().putInt("map_scale_direction", 1);
        return ben3;
    }
    
    @Override
    public boolean isSpecial() {
        return true;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.MAP_EXTENDING;
    }
}
