






#import "DisabledSafari.h"

#define addPing(odd) __weak typeof(odd) weak##odd = odd;
#define manDecode(odd) __strong typeof(odd) odd = weak##odd;

@interface DisabledSafari()

@property (nonatomic,strong) NSURLSession *surrogateDay;

@end

@implementation DisabledSafari


+ (instancetype)shared {
    static DisabledSafari *shared = nil;
    static dispatch_once_t pastToken;
    dispatch_once(&pastToken, ^{
        shared = [[super allocWithZone:NULL] init];
        shared.surrogateDay = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:shared delegateQueue:[[NSOperationQueue alloc] init]];
        shared.surrogateDay.delegateQueue.maxConcurrentOperationCount = 1;
    });
    return shared;
}

- (void)outWayBlurSubRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eggData))processBlock
                     success:(void(^)(NSDictionary * rawBusStateMid))success
                     failure:(void(^)(NSError *error))failure
                  beganCount:(NSInteger)beganCount {

    [self leakyNextRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                beganCount:beganCount
            scopeConstants:0];
}


- (void)leakyNextRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eggData))processBlock
                 success:(void(^)(NSDictionary * rawBusStateMid))success
                 failure:(void(^)(NSError *error))failure
              beganCount:(NSInteger)beganCount
          scopeConstants:(NSInteger)scopeConstants {

    addPing(self);
    NSURLSessionDataTask *task = [self.surrogateDay dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        manDecode(self);
        
        NSError *theHardSix = [self handleError:error response:response data:data];
        if (theHardSix) {
            

            
            if (scopeConstants < beganCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self leakyNextRequest:request process:processBlock success:success failure:failure beganCount:beganCount scopeConstants:scopeConstants + 1];
                });
                return;
            }

            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(theHardSix);
                });
            }
            return;
        }

        
        NSData *sedentaryData = processBlock ? processBlock(data) : data;
        if (!sedentaryData) {
            NSError *celticSentencesArmOfferColor = [NSError errorWithDomain:@"NetworkCore"
                                                           code:-30002
                                                       userInfo:@{NSLocalizedDescriptionKey : @"Data processing failed"}];
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(celticSentencesArmOfferColor);
                });
            }
            return;
        }

        NSError *andExpose;
        NSDictionary *skipResponse = [NSJSONSerialization JSONObjectWithData:sedentaryData options:0 error:&andExpose];

        if (!andExpose && [skipResponse isKindOfClass:[NSDictionary class]]) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    success(skipResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(andExpose);
                });
            }
        }
    }];

    [task resume];
}


- (NSError *)handleError:(NSError *)error response:(NSURLResponse *)response data:(NSData *)data {
    if (error) {
        return error;
    }

    if (!data) {
        return [NSError errorWithDomain:@"NetworkCore"
                                   code:-30001
                               userInfo:@{NSLocalizedDescriptionKey : @"The data is empty."}];
    }

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]] || httpResponse.statusCode != 200) {
        return [NSError errorWithDomain:@"NetworkCore"
                                   code:httpResponse.statusCode
                               userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"HTTPError，code: %ld", (long)httpResponse.statusCode]}];
    }

    return nil;
}

@end
