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

package com.hypixel.hytale.server.worldgen.loader;

import java.util.List;
import java.io.IOException;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import java.util.function.BiConsumer;
import com.hypixel.hytale.sneakythrow.SneakyThrow;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.nio.file.Path;
import com.hypixel.hytale.server.worldgen.util.cache.TimeoutCache;
import com.hypixel.hytale.server.core.prefab.selection.buffer.PrefabLoader;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.worldgen.prefab.PrefabStoreRoot;

public class WorldGenPrefabLoader
{
    public static final String PREFAB_FOLDER = "Prefabs";
    @Nonnull
    private final PrefabStoreRoot store;
    @Nonnull
    private final PrefabLoader prefabLoader;
    @Nonnull
    private final TimeoutCache<String, WorldGenPrefabSupplier[]> cache;
    
    public WorldGenPrefabLoader(@Nonnull final PrefabStoreRoot store, @Nonnull final Path dataFolder) {
        final Path storePath = PrefabStoreRoot.resolvePrefabStore(store, dataFolder);
        this.store = store;
        this.prefabLoader = new PrefabLoader(storePath);
        this.cache = new TimeoutCache<String, WorldGenPrefabSupplier[]>(30L, TimeUnit.SECONDS, SneakyThrow.sneakyFunction(key -> {
            final ArrayList suppliers = new ArrayList();
            this.resolve(key, path -> suppliers.add(new WorldGenPrefabSupplier(this, key, path)));
            return (Object[])suppliers.toArray(WorldGenPrefabSupplier[]::new);
        }), null);
    }
    
    @Nonnull
    public PrefabStoreRoot getStore() {
        return this.store;
    }
    
    public Path getRootFolder() {
        return this.prefabLoader.getRootFolder();
    }
    
    @Nullable
    public WorldGenPrefabSupplier[] get(final String prefabName) {
        return this.cache.get(prefabName);
    }
    
    private void resolve(@Nonnull final String prefabName, @Nonnull final Consumer<Path> consumer) throws IOException {
        this.prefabLoader.resolvePrefabs(prefabName, consumer);
    }
}
