package net.minecraft.server.packs.repository;

import net.minecraft.server.packs.FilePackResources;
import net.minecraft.server.packs.FolderPackResources;
import net.minecraft.server.packs.PackResources;
import java.util.function.Supplier;
import java.util.function.Consumer;
import java.io.File;
import java.io.FileFilter;

public class FolderRepositorySource implements RepositorySource {
    private static final FileFilter RESOURCEPACK_FILTER;
    private final File folder;
    private final PackSource packSource;
    
    public FolderRepositorySource(final File file, final PackSource aas) {
        this.folder = file;
        this.packSource = aas;
    }
    
    @Override
    public <T extends Pack> void loadPacks(final Consumer<T> consumer, final Pack.PackConstructor<T> a) {
        if (!this.folder.isDirectory()) {
            this.folder.mkdirs();
        }
        final File[] arr4 = this.folder.listFiles(FolderRepositorySource.RESOURCEPACK_FILTER);
        if (arr4 == null) {
            return;
        }
        for (final File file8 : arr4) {
            final String string9 = "file/" + file8.getName();
            final T aap10 = Pack.<T>create(string9, false, this.createSupplier(file8), a, Pack.Position.TOP, this.packSource);
            if (aap10 != null) {
                consumer.accept(aap10);
            }
        }
    }
    
    private Supplier<PackResources> createSupplier(final File file) {
        if (file.isDirectory()) {
            return (Supplier<PackResources>)(() -> new FolderPackResources(file));
        }
        return (Supplier<PackResources>)(() -> new FilePackResources(file));
    }
    
    static {
        final boolean boolean2;
        final boolean b;
        final boolean boolean3;
        RESOURCEPACK_FILTER = (file -> {
            boolean2 = (file.isFile() && file.getName().endsWith(".zip"));
            Label_0053_1: {
                if (file.isDirectory()) {
                    if (new File(file, "pack.mcmeta").isFile()) {
                        break Label_0053_1;
                    }
                }
            }
            boolean3 = b;
            return boolean2 || boolean3;
        });
    }
}
