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

package io.sentry.internal.debugmeta;

import java.io.InputStream;
import java.util.Enumeration;
import java.io.IOException;
import io.sentry.SentryLevel;
import java.net.URL;
import io.sentry.util.DebugMetaPropertiesApplier;
import java.util.ArrayList;
import java.util.Properties;
import java.util.List;
import io.sentry.util.ClassLoaderUtils;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.ILogger;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class ResourcesDebugMetaLoader implements IDebugMetaLoader
{
    @NotNull
    private final ILogger logger;
    @NotNull
    private final ClassLoader classLoader;
    
    public ResourcesDebugMetaLoader(@NotNull final ILogger logger) {
        this(logger, ResourcesDebugMetaLoader.class.getClassLoader());
    }
    
    ResourcesDebugMetaLoader(@NotNull final ILogger logger, @Nullable final ClassLoader classLoader) {
        this.logger = logger;
        this.classLoader = ClassLoaderUtils.classLoaderOrDefault(classLoader);
    }
    
    @Nullable
    @Override
    public List<Properties> loadDebugMeta() {
        final List<Properties> debugPropertyList = new ArrayList<Properties>();
        try {
            final Enumeration<URL> resourceUrls = this.classLoader.getResources(DebugMetaPropertiesApplier.DEBUG_META_PROPERTIES_FILENAME);
            while (resourceUrls.hasMoreElements()) {
                final URL currentUrl = resourceUrls.nextElement();
                try (final InputStream is = currentUrl.openStream()) {
                    final Properties currentProperties = new Properties();
                    currentProperties.load(is);
                    debugPropertyList.add(currentProperties);
                    this.logger.log(SentryLevel.INFO, "Debug Meta Data Properties loaded from %s", currentUrl);
                    if (is == null) {
                        continue;
                    }
                }
                catch (final RuntimeException e) {
                    this.logger.log(SentryLevel.ERROR, e, "%s file is malformed.", currentUrl);
                }
            }
        }
        catch (final IOException e2) {
            this.logger.log(SentryLevel.ERROR, e2, "Failed to load %s", DebugMetaPropertiesApplier.DEBUG_META_PROPERTIES_FILENAME);
        }
        if (debugPropertyList.isEmpty()) {
            this.logger.log(SentryLevel.INFO, "No %s file was found.", DebugMetaPropertiesApplier.DEBUG_META_PROPERTIES_FILENAME);
            return null;
        }
        return debugPropertyList;
    }
}
