package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.Typed;
import java.util.Optional;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.OpticFinder;
import java.util.Arrays;
import java.util.stream.IntStream;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public class ChunkBiomeFix extends DataFix {
    public ChunkBiomeFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    protected TypeRewriteRule makeRule() {
        final Type<?> type2 = this.getInputSchema().getType(References.CHUNK);
        final OpticFinder<?> opticFinder3 = type2.findField("Level");
        final Optional<IntStream> optional2;
        int[] arr3;
        int[] arr4;
        int integer5;
        int integer6;
        int integer7;
        int integer8;
        int integer9;
        int integer10;
        return this.fixTypeEverywhereTyped("Leaves fix", (Type)type2, typed -> typed.updateTyped((OpticFinder)opticFinder3, typed -> typed.update(DSL.remainderFinder(), dynamic -> {
            optional2 = (Optional<IntStream>)dynamic.get("Biomes").asIntStreamOpt();
            if (!optional2.isPresent()) {
                return dynamic;
            }
            else {
                arr3 = optional2.get().toArray();
                arr4 = new int[1024];
                for (integer5 = 0; integer5 < 4; ++integer5) {
                    for (integer6 = 0; integer6 < 4; ++integer6) {
                        integer7 = (integer6 << 2) + 2;
                        integer8 = (integer5 << 2) + 2;
                        integer9 = (integer8 << 4 | integer7);
                        arr4[integer5 << 2 | integer6] = ((integer9 < arr3.length) ? arr3[integer9] : -1);
                    }
                }
                for (integer10 = 1; integer10 < 64; ++integer10) {
                    System.arraycopy(arr4, 0, arr4, integer10 * 16, 16);
                }
                return dynamic.set("Biomes", dynamic.createIntList(Arrays.stream(arr4)));
            }
        })));
    }
}
