package net.minecraft.world.level.chunk;

import net.minecraft.nbt.ListTag;
import net.minecraft.network.FriendlyByteBuf;
import javax.annotation.Nullable;
import java.util.function.Predicate;
import net.minecraft.nbt.CompoundTag;
import java.util.function.Function;
import net.minecraft.core.IdMapper;

public class LinearPalette<T> implements Palette<T> {
    private final IdMapper<T> registry;
    private final T[] values;
    private final PaletteResize<T> resizeHandler;
    private final Function<CompoundTag, T> reader;
    private final int bits;
    private int size;
    
    public LinearPalette(final IdMapper<T> ge, final int integer, final PaletteResize<T> chp, final Function<CompoundTag, T> function) {
        this.registry = ge;
        this.values = (T[])new Object[1 << integer];
        this.bits = integer;
        this.resizeHandler = chp;
        this.reader = function;
    }
    
    @Override
    public int idFor(final T object) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            if (this.values[integer3] == object) {
                return integer3;
            }
        }
        int integer3 = this.size;
        if (integer3 < this.values.length) {
            this.values[integer3] = object;
            ++this.size;
            return integer3;
        }
        return this.resizeHandler.onResize(this.bits + 1, object);
    }
    
    @Override
    public boolean maybeHas(final Predicate<T> predicate) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            if (predicate.test(this.values[integer3])) {
                return true;
            }
        }
        return false;
    }
    
    @Nullable
    @Override
    public T valueFor(final int integer) {
        if (integer >= 0 && integer < this.size) {
            return this.values[integer];
        }
        return null;
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) {
        this.size = mg.readVarInt();
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            this.values[integer3] = this.registry.byId(mg.readVarInt());
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) {
        mg.writeVarInt(this.size);
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            mg.writeVarInt(this.registry.getId(this.values[integer3]));
        }
    }
    
    @Override
    public int getSerializedSize() {
        int integer2 = FriendlyByteBuf.getVarIntSize(this.getSize());
        for (int integer3 = 0; integer3 < this.getSize(); ++integer3) {
            integer2 += FriendlyByteBuf.getVarIntSize(this.registry.getId(this.values[integer3]));
        }
        return integer2;
    }
    
    public int getSize() {
        return this.size;
    }
    
    @Override
    public void read(final ListTag lk) {
        for (int integer3 = 0; integer3 < lk.size(); ++integer3) {
            this.values[integer3] = this.reader.apply(lk.getCompound(integer3));
        }
        this.size = lk.size();
    }
}
