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

package io.sentry.util;

import io.sentry.SentryIntegrationPackageStorage;
import java.util.Iterator;
import io.sentry.SentryLevel;
import org.jetbrains.annotations.Nullable;
import java.util.Properties;
import java.util.List;
import io.sentry.SentryOptions;
import org.jetbrains.annotations.NotNull;

public final class DebugMetaPropertiesApplier
{
    @NotNull
    public static String DEBUG_META_PROPERTIES_FILENAME;
    
    public static void apply(@NotNull final SentryOptions options, @Nullable final List<Properties> debugMetaProperties) {
        if (debugMetaProperties != null) {
            applyToOptions(options, debugMetaProperties);
            applyBuildTool(options, debugMetaProperties);
            applyDistributionOptions(options, debugMetaProperties);
        }
    }
    
    public static void applyToOptions(@NotNull final SentryOptions options, @Nullable final List<Properties> debugMetaProperties) {
        if (debugMetaProperties != null) {
            applyBundleIds(options, debugMetaProperties);
            applyProguardUuid(options, debugMetaProperties);
        }
    }
    
    private static void applyBundleIds(@NotNull final SentryOptions options, @NotNull final List<Properties> debugMetaProperties) {
        if (options.getBundleIds().isEmpty()) {
            for (final Properties properties : debugMetaProperties) {
                final String bundleIdStrings = properties.getProperty("io.sentry.bundle-ids");
                options.getLogger().log(SentryLevel.DEBUG, "Bundle IDs found: %s", bundleIdStrings);
                if (bundleIdStrings != null) {
                    final String[] split;
                    final String[] bundleIds = split = bundleIdStrings.split(",", -1);
                    for (final String bundleId : split) {
                        options.addBundleId(bundleId);
                    }
                }
            }
        }
    }
    
    private static void applyProguardUuid(@NotNull final SentryOptions options, @NotNull final List<Properties> debugMetaProperties) {
        if (options.getProguardUuid() == null) {
            for (final Properties properties : debugMetaProperties) {
                final String proguardUuid = getProguardUuid(properties);
                if (proguardUuid != null) {
                    options.getLogger().log(SentryLevel.DEBUG, "Proguard UUID found: %s", proguardUuid);
                    options.setProguardUuid(proguardUuid);
                    break;
                }
            }
        }
    }
    
    private static void applyBuildTool(@NotNull final SentryOptions options, @NotNull final List<Properties> debugMetaProperties) {
        for (final Properties properties : debugMetaProperties) {
            final String buildTool = getBuildTool(properties);
            if (buildTool != null) {
                String buildToolVersion = getBuildToolVersion(properties);
                if (buildToolVersion == null) {
                    buildToolVersion = "unknown";
                }
                options.getLogger().log(SentryLevel.DEBUG, "Build tool found: %s, version %s", buildTool, buildToolVersion);
                SentryIntegrationPackageStorage.getInstance().addPackage(buildTool, buildToolVersion);
                break;
            }
        }
    }
    
    @Nullable
    public static String getProguardUuid(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.ProguardUuids");
    }
    
    @Nullable
    public static String getBuildTool(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.build-tool");
    }
    
    @Nullable
    public static String getBuildToolVersion(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.build-tool-version");
    }
    
    private static void applyDistributionOptions(@NotNull final SentryOptions options, @NotNull final List<Properties> debugMetaProperties) {
        for (final Properties properties : debugMetaProperties) {
            final String orgSlug = getDistributionOrgSlug(properties);
            final String projectSlug = getDistributionProjectSlug(properties);
            final String orgAuthToken = getDistributionAuthToken(properties);
            final String buildConfiguration = getDistributionBuildConfiguration(properties);
            if (orgSlug != null || projectSlug != null || orgAuthToken != null || buildConfiguration != null) {
                final SentryOptions.DistributionOptions distributionOptions = options.getDistribution();
                if (orgSlug != null && !orgSlug.isEmpty() && distributionOptions.orgSlug.isEmpty()) {
                    options.getLogger().log(SentryLevel.DEBUG, "Distribution org slug found: %s", orgSlug);
                    distributionOptions.orgSlug = orgSlug;
                }
                if (projectSlug != null && !projectSlug.isEmpty() && distributionOptions.projectSlug.isEmpty()) {
                    options.getLogger().log(SentryLevel.DEBUG, "Distribution project slug found: %s", projectSlug);
                    distributionOptions.projectSlug = projectSlug;
                }
                if (orgAuthToken != null && !orgAuthToken.isEmpty() && distributionOptions.orgAuthToken.isEmpty()) {
                    options.getLogger().log(SentryLevel.DEBUG, "Distribution org auth token found", new Object[0]);
                    distributionOptions.orgAuthToken = orgAuthToken;
                }
                if (buildConfiguration != null && !buildConfiguration.isEmpty() && distributionOptions.buildConfiguration == null) {
                    options.getLogger().log(SentryLevel.DEBUG, "Distribution build configuration found: %s", buildConfiguration);
                    distributionOptions.buildConfiguration = buildConfiguration;
                    break;
                }
                break;
            }
        }
    }
    
    @Nullable
    private static String getDistributionOrgSlug(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.distribution.org-slug");
    }
    
    @Nullable
    private static String getDistributionProjectSlug(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.distribution.project-slug");
    }
    
    @Nullable
    private static String getDistributionAuthToken(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.distribution.auth-token");
    }
    
    @Nullable
    private static String getDistributionBuildConfiguration(@NotNull final Properties debugMetaProperties) {
        return debugMetaProperties.getProperty("io.sentry.distribution.build-configuration");
    }
    
    static {
        DebugMetaPropertiesApplier.DEBUG_META_PROPERTIES_FILENAME = "sentry-debug-meta.properties";
    }
}
