// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.universe.world.storage;

import org.bson.BsonDocument;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.util.BsonUtil;
import com.hypixel.hytale.component.Holder;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;
import java.util.Objects;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Store;

public abstract class BufferChunkLoader implements IChunkLoader
{
    @Nonnull
    private final Store<ChunkStore> store;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public BufferChunkLoader(@Nonnull final Store<ChunkStore> store) {
        Objects.requireNonNull(store);
        this.store = store;
    }
    
    @Nonnull
    public Store<ChunkStore> getStore() {
        return this.store;
    }
    
    public abstract CompletableFuture<ByteBuffer> loadBuffer(final int p0, final int p1);
    
    @Nonnull
    @Override
    public CompletableFuture<Holder<ChunkStore>> loadHolder(final int x, final int z) {
        return this.loadBuffer(x, z).thenApplyAsync(buffer -> {
            if (buffer == null) {
                return null;
            }
            else {
                final BsonDocument bsonDocument = BsonUtil.readFromBuffer(buffer);
                final Holder<ChunkStore> holder = ChunkStore.REGISTRY.deserialize(bsonDocument);
                final WorldChunk worldChunkComponent = holder.getComponent(WorldChunk.getComponentType());
                if (!BufferChunkLoader.$assertionsDisabled && worldChunkComponent == null) {
                    throw new AssertionError();
                }
                else {
                    worldChunkComponent.loadFromHolder(this.store.getExternalData().getWorld(), x, z, holder);
                    return (Holder<ChunkStore>)(Holder<ChunkStore>)holder;
                }
            }
        });
    }
}
