
#import "TPISentry.h"
#import <Cordova/NSDictionary+CordovaPreferences.h>
@import SentryObjC;

@implementation TPISentry

- (void) pluginInitialize {
    NSDictionary* settings = self.commandDelegate.settings;

    NSString* sentryDSN = [settings cordovaSettingForKey:@"SENTRY_DSN"];

    if (sentryDSN == nil || (sentryDSN != nil && [sentryDSN isEqualToString: @"null"])) {
        NSLog(@"Missing SENTRY_DSN preference. Skipping Sentry Native integration.");
        return;
    }

    BOOL sentryDebug = [settings cordovaBoolSettingForKey:@"SENTRY_DEBUG" defaultValue: false];
    NSString* sentryEnvironment = [settings cordovaSettingForKey:@"SENTRY_ENVIRONMENT"];
    NSString* sentryDist = [settings cordovaSettingForKey:@"SENTRY_DIST"];
    NSString* sentryRelease = [settings cordovaSettingForKey:@"SENTRY_RELEASE"];

    [SentryObjCSDK startWithConfigureOptions:^(SentryObjCOptions* options) {
        options.dsn = sentryDSN;
        options.debug = sentryDebug;
        
        if (sentryRelease != nil) {
            options.releaseName = sentryRelease;
        }
        
        if (sentryEnvironment != nil) {
            options.environment = sentryEnvironment;
        }
        
        if (sentryDist != nil) {
            options.dist = sentryDist;
        }
    }];
}

- (void) testNativeException:(CDVInvokedUrlCommand*) command {
    NSError* error = [
        [NSError alloc]
        initWithDomain: @"TPISentry"
        code: 0
        userInfo: @{
            NSLocalizedDescriptionKey: @"Test Native Error"
        }
    ];
    
    [SentryObjCSDK captureError: error];
}

@end
