//
//  CMDQueryStringValueTransformer.m
//  CMDQueryStringSerialization
//
//  Created by Caleb Davenport on 7/11/14.
//  Copyright (c) 2014 Caleb Davenport. All rights reserved.
//

#import "CMDQueryStringValueTransformer.h"
#import "NSString+CMDQueryStringSerialization.h"

@implementation NSObject (CMDQueryStringValueTransformer)

- (NSString *)CMDQueryStringValueTransformer_queryStringWithKey:(NSString *)key options:(CMDQueryStringWritingOptions)options {
    NSString *escapedKey = [key cmd_stringByAddingEscapes];
    NSString *escapedValue = [[self description] cmd_stringByAddingEscapes];
    return [NSString stringWithFormat:@"%@=%@", escapedKey, escapedValue];
}

@end

@implementation NSArray (CMDQueryStringValueTransformer)

- (NSString *)CMDQueryStringValueTransformer_queryStringWithKey:(NSString *)key options:(CMDQueryStringWritingOptions)options {
    NSString *escapedKey = [key cmd_stringByAddingEscapes];
    NSArray *escapedValues = [self valueForKey:@"cmd_stringByAddingEscapes"];
    if ((options & CMDQueryStringWritingOptionArrayCommaSeparatedValues) == CMDQueryStringWritingOptionArrayCommaSeparatedValues) {
        return [NSString stringWithFormat:@"%@=%@", escapedKey, [escapedValues componentsJoinedByString:@","]];
    }
    else if ((options & CMDQueryStringWritingOptionArrayRepeatKeysWithBrackets) == CMDQueryStringWritingOptionArrayRepeatKeysWithBrackets) {
        NSMutableArray *pairs = [[NSMutableArray alloc] init];
        [escapedValues enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
            NSString *pair = [NSString stringWithFormat:@"%@[]=%@", escapedKey, object];
            [pairs addObject:pair];
        }];
        return [pairs componentsJoinedByString:@"&"];
    }
    else {
        NSMutableArray *pairs = [[NSMutableArray alloc] init];
        [escapedValues enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
            NSString *pair = [NSString stringWithFormat:@"%@=%@", escapedKey, object];
            [pairs addObject:pair];
        }];
        return [pairs componentsJoinedByString:@"&"];
    }
}

@end

@implementation NSDictionary (CMDQueryStringValueTransformer)

- (NSString *)CMDQueryStringValueTransformer_queryStringWithKey:(NSString *)namespace options:(CMDQueryStringWritingOptions)options {
    NSMutableArray *pairs = [[NSMutableArray alloc] init];
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
        
        // Ensure that key is a string
        if (![key isKindOfClass:[NSString class]]) {
            [NSException raise:NSInvalidArgumentException format:@"%@ is not a valid query string key.", key];
        }
        
        // Convert values
        NSString *pair = [object CMDQueryStringValueTransformer_queryStringWithKey:key options:options];
        [pairs addObject:pair];
    }];
    [pairs sortUsingSelector:@selector(compare:)];
    return [pairs componentsJoinedByString:@"&"];
}

@end
