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

package io.sentry.logger;

import io.sentry.protocol.User;
import io.sentry.HostnameCache;
import io.sentry.protocol.SdkVersion;
import java.util.Iterator;
import io.sentry.util.Platform;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributeType;
import java.util.HashMap;
import io.sentry.SpanId;
import io.sentry.protocol.SentryId;
import io.sentry.ISpan;
import io.sentry.PropagationContext;
import io.sentry.IScope;
import io.sentry.SentryOptions;
import io.sentry.SentryLogEventAttributeValue;
import java.util.Map;
import io.sentry.SentryLogEvent;
import io.sentry.util.TracingUtils;
import io.sentry.SentryLevel;
import io.sentry.SentryAttributes;
import io.sentry.SentryDate;
import io.sentry.SentryLogLevel;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.Scopes;

public final class LoggerApi implements ILoggerApi
{
    @NotNull
    private final Scopes scopes;
    
    public LoggerApi(@NotNull final Scopes scopes) {
        this.scopes = scopes;
    }
    
    @Override
    public void trace(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.TRACE, message, args);
    }
    
    @Override
    public void debug(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.DEBUG, message, args);
    }
    
    @Override
    public void info(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.INFO, message, args);
    }
    
    @Override
    public void warn(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.WARN, message, args);
    }
    
    @Override
    public void error(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.ERROR, message, args);
    }
    
    @Override
    public void fatal(@Nullable final String message, @Nullable final Object... args) {
        this.log(SentryLogLevel.FATAL, message, args);
    }
    
    @Override
    public void log(@NotNull final SentryLogLevel level, @Nullable final String message, @Nullable final Object... args) {
        this.captureLog(level, SentryLogParameters.create(null, null), message, args);
    }
    
    @Override
    public void log(@NotNull final SentryLogLevel level, @Nullable final SentryDate timestamp, @Nullable final String message, @Nullable final Object... args) {
        this.captureLog(level, SentryLogParameters.create(timestamp, null), message, args);
    }
    
    @Override
    public void log(@NotNull final SentryLogLevel level, @NotNull final SentryLogParameters params, @Nullable final String message, @Nullable final Object... args) {
        this.captureLog(level, params, message, args);
    }
    
    private void captureLog(@NotNull final SentryLogLevel level, @NotNull final SentryLogParameters params, @Nullable final String message, @Nullable final Object... args) {
        final SentryOptions options = this.scopes.getOptions();
        try {
            if (!this.scopes.isEnabled()) {
                options.getLogger().log(SentryLevel.WARNING, "Instance is disabled and this 'logger' call is a no-op.", new Object[0]);
                return;
            }
            if (!options.getLogs().isEnabled()) {
                options.getLogger().log(SentryLevel.WARNING, "Sentry Log is disabled and this 'logger' call is a no-op.", new Object[0]);
                return;
            }
            if (message == null) {
                return;
            }
            final SentryDate timestamp = params.getTimestamp();
            final SentryDate timestampToUse = (timestamp == null) ? options.getDateProvider().now() : timestamp;
            final String messageToUse = this.maybeFormatMessage(message, args);
            final IScope combinedScope = this.scopes.getCombinedScopeView();
            final PropagationContext propagationContext = combinedScope.getPropagationContext();
            final ISpan span = combinedScope.getSpan();
            if (span == null) {
                TracingUtils.maybeUpdateBaggage(combinedScope, options);
            }
            final SentryId traceId = (span == null) ? propagationContext.getTraceId() : span.getSpanContext().getTraceId();
            final SpanId spanId = (span == null) ? propagationContext.getSpanId() : span.getSpanContext().getSpanId();
            final SentryLogEvent logEvent = new SentryLogEvent(traceId, timestampToUse, messageToUse, level);
            logEvent.setAttributes(this.createAttributes(params, message, spanId, args));
            logEvent.setSeverityNumber(level.getSeverityNumber());
            this.scopes.getClient().captureLog(logEvent, combinedScope);
        }
        catch (final Throwable e) {
            options.getLogger().log(SentryLevel.ERROR, "Error while capturing log event", e);
        }
    }
    
    @NotNull
    private String maybeFormatMessage(@NotNull final String message, @Nullable final Object[] args) {
        if (args == null || args.length == 0) {
            return message;
        }
        try {
            return String.format(message, args);
        }
        catch (final Throwable t) {
            this.scopes.getOptions().getLogger().log(SentryLevel.ERROR, "Error while running log through String.format", t);
            return message;
        }
    }
    
    @NotNull
    private HashMap<String, SentryLogEventAttributeValue> createAttributes(@NotNull final SentryLogParameters params, @NotNull final String message, @NotNull final SpanId spanId, @Nullable final Object... args) {
        final HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<String, SentryLogEventAttributeValue>();
        final String origin = params.getOrigin();
        if (!"manual".equalsIgnoreCase(origin)) {
            attributes.put("sentry.origin", new SentryLogEventAttributeValue(SentryAttributeType.STRING, origin));
        }
        final SentryAttributes incomingAttributes = params.getAttributes();
        if (incomingAttributes != null) {
            for (final SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
                final Object value = attribute.getValue();
                final SentryAttributeType type = (attribute.getType() == null) ? this.getType(value) : attribute.getType();
                attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
            }
        }
        if (args != null) {
            int i = 0;
            for (final Object arg : args) {
                final SentryAttributeType type2 = this.getType(arg);
                attributes.put("sentry.message.parameter." + i, new SentryLogEventAttributeValue(type2, arg));
                ++i;
            }
            if (i > 0 && attributes.get("sentry.message.template") == null) {
                attributes.put("sentry.message.template", new SentryLogEventAttributeValue(SentryAttributeType.STRING, message));
            }
        }
        final SdkVersion sdkVersion = this.scopes.getOptions().getSdkVersion();
        if (sdkVersion != null) {
            attributes.put("sentry.sdk.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getName()));
            attributes.put("sentry.sdk.version", new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getVersion()));
        }
        final String environment = this.scopes.getOptions().getEnvironment();
        if (environment != null) {
            attributes.put("sentry.environment", new SentryLogEventAttributeValue(SentryAttributeType.STRING, environment));
        }
        final SentryId scopeReplayId = this.scopes.getCombinedScopeView().getReplayId();
        if (!SentryId.EMPTY_ID.equals(scopeReplayId)) {
            attributes.put("sentry.replay_id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, scopeReplayId.toString()));
        }
        else {
            final SentryId controllerReplayId = this.scopes.getOptions().getReplayController().getReplayId();
            if (!SentryId.EMPTY_ID.equals(controllerReplayId)) {
                attributes.put("sentry.replay_id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, controllerReplayId.toString()));
                attributes.put("sentry._internal.replay_is_buffering", new SentryLogEventAttributeValue(SentryAttributeType.BOOLEAN, true));
            }
        }
        final String release = this.scopes.getOptions().getRelease();
        if (release != null) {
            attributes.put("sentry.release", new SentryLogEventAttributeValue(SentryAttributeType.STRING, release));
        }
        attributes.put("sentry.trace.parent_span_id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, spanId));
        if (Platform.isJvm()) {
            this.setServerName(attributes);
        }
        this.setUser(attributes);
        return attributes;
    }
    
    private void setServerName(@NotNull final HashMap<String, SentryLogEventAttributeValue> attributes) {
        final SentryOptions options = this.scopes.getOptions();
        final String optionsServerName = options.getServerName();
        if (optionsServerName != null) {
            attributes.put("server.address", new SentryLogEventAttributeValue(SentryAttributeType.STRING, optionsServerName));
        }
        else if (options.isAttachServerName()) {
            final String hostname = HostnameCache.getInstance().getHostname();
            if (hostname != null) {
                attributes.put("server.address", new SentryLogEventAttributeValue(SentryAttributeType.STRING, hostname));
            }
        }
    }
    
    private void setUser(@NotNull final HashMap<String, SentryLogEventAttributeValue> attributes) {
        final User user = this.scopes.getCombinedScopeView().getUser();
        if (user == null) {
            final String id = this.scopes.getOptions().getDistinctId();
            if (id != null) {
                attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
            }
        }
        else {
            final String id = user.getId();
            if (id != null) {
                attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
            }
            final String username = user.getUsername();
            if (username != null) {
                attributes.put("user.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, username));
            }
            final String email = user.getEmail();
            if (email != null) {
                attributes.put("user.email", new SentryLogEventAttributeValue(SentryAttributeType.STRING, email));
            }
        }
    }
    
    @NotNull
    private SentryAttributeType getType(@Nullable final Object arg) {
        if (arg instanceof Boolean) {
            return SentryAttributeType.BOOLEAN;
        }
        if (arg instanceof Integer) {
            return SentryAttributeType.INTEGER;
        }
        if (arg instanceof Number) {
            return SentryAttributeType.DOUBLE;
        }
        return SentryAttributeType.STRING;
    }
}
