package net.minecraft.world.level.chunk;

import net.minecraft.nbt.ListTag;
import net.minecraft.network.FriendlyByteBuf;
import javax.annotation.Nullable;
import org.apache.commons.lang3.ArrayUtils;
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> ft, final int integer, final PaletteResize<T> car, final Function<CompoundTag, T> function) {
        this.registry = ft;
        this.values = (T[])new Object[1 << integer];
        this.bits = integer;
        this.resizeHandler = car;
        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 T object) {
        return ArrayUtils.contains((Object[])this.values, object);
    }
    
    @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 kv) {
        this.size = kv.readVarInt();
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            this.values[integer3] = this.registry.byId(kv.readVarInt());
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) {
        kv.writeVarInt(this.size);
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            kv.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 jz) {
        for (int integer3 = 0; integer3 < jz.size(); ++integer3) {
            this.values[integer3] = this.reader.apply(jz.getCompound(integer3));
        }
        this.size = jz.size();
    }
}
