1//
2//  BranchUniversalObject+RNBranch.m
3//  RNBranch
4//
5//  Created by Jimmy Dee on 1/26/17.
6//  Copyright © 2017 Branch Metrics. All rights reserved.
7//
8
9#import <React/RCTLog.h>
10
11#import "BranchUniversalObject+RNBranch.h"
12#import "BranchContentMetadata+RNBranch.h"
13#import "NSObject+RNBranch.h"
14#import "RNBranchProperty.h"
15
16@implementation BranchUniversalObject(RNBranch)
17
18+ (NSDictionary<NSString *,RNBranchProperty *> *)supportedProperties
19{
20    static NSDictionary<NSString *, RNBranchProperty *> *_universalObjectProperties;
21    static dispatch_once_t once = 0;
22    dispatch_once(&once, ^{
23        _universalObjectProperties =
24        @{
25          @"automaticallyListOnSpotlight": [RNBranchProperty propertyWithSetterSelector:@selector(setAutomaticallyListOnSpotlightWithNumber:) type:NSNumber.class],
26          @"canonicalUrl": [RNBranchProperty propertyWithSetterSelector:@selector(setCanonicalUrl:) type:NSString.class],
27          @"contentDescription": [RNBranchProperty propertyWithSetterSelector:@selector(setContentDescription:) type:NSString.class],
28          @"contentImageUrl": [RNBranchProperty propertyWithSetterSelector:@selector(setImageUrl:) type:NSString.class],
29          @"contentIndexingMode": [RNBranchProperty propertyWithSetterSelector:@selector(setContentIndexingMode:) type:NSString.class],
30          @"contentMetadata": [RNBranchProperty propertyWithSetterSelector:@selector(setContentMetadataWithMap:) type:NSDictionary.class],
31          @"currency": [RNBranchProperty propertyWithSetterSelector:@selector(setCurrency:) type:NSString.class],
32          @"expirationDate": [RNBranchProperty propertyWithSetterSelector:@selector(setExpirationDateWithString:) type:NSString.class],
33          @"keywords": [RNBranchProperty propertyWithSetterSelector:@selector(setKeywords:) type:NSArray.class],
34          @"locallyIndex": [RNBranchProperty propertyWithSetterSelector:@selector(setLocallyIndexWithNumber:) type:NSNumber.class],
35          @"metadata": [RNBranchProperty propertyWithSetterSelector:@selector(setMetadata:) type:NSDictionary.class],
36          @"price": [RNBranchProperty propertyWithSetterSelector:@selector(setPriceWithNumber:) type:NSNumber.class],
37          @"publiclyIndex": [RNBranchProperty propertyWithSetterSelector:@selector(setPubliclyIndexWithNumber:) type:NSNumber.class],
38          @"title": [RNBranchProperty propertyWithSetterSelector:@selector(setTitle:) type:NSString.class],
39          @"type": [RNBranchProperty propertyWithSetterSelector:@selector(setType:) type:NSString.class]
40          };
41    });
42
43    return _universalObjectProperties;
44}
45
46- (instancetype)initWithMap:(NSDictionary *)map
47{
48    NSString *canonicalIdentifier = map[@"canonicalIdentifier"];
49    NSMutableDictionary *mutableMap = map.mutableCopy;
50    [mutableMap removeObjectForKey:@"canonicalIdentifier"];
51
52    self = [self initWithCanonicalIdentifier:canonicalIdentifier];
53    if (self) {
54        [self setSupportedPropertiesWithMap:mutableMap];
55    }
56    return self;
57}
58
59#pragma clang diagnostic push
60#pragma clang diagnostic ignored "-Wdeprecated-declarations"
61- (void)setContentIndexingMode:(NSString *)contentIndexingMode
62{
63    if ([contentIndexingMode isEqualToString:@"private"]) {
64        self.contentIndexMode = BranchContentIndexModePrivate;
65    }
66    else if ([contentIndexingMode isEqualToString:@"public"]) {
67        self.contentIndexMode = BranchContentIndexModePublic;
68    }
69    else {
70        RCTLogWarn(@"Invalid value \"%@\" for \"contentIndexingMode\". Supported values are \"public\" and \"private\".", contentIndexingMode);
71    }
72}
73
74- (void)setPriceWithNumber:(NSNumber *)price
75{
76    self.price = price.floatValue;
77}
78
79- (void)setAutomaticallyListOnSpotlightWithNumber:(NSNumber *)flag
80{
81    self.automaticallyListOnSpotlight = flag.boolValue;
82}
83
84#pragma clang diagnostic pop
85
86- (void)setExpirationDateWithString:(NSString *)expirationDate
87{
88    struct tm expiration;
89    if (!strptime(expirationDate.UTF8String, "%Y-%m-%dT%H:%M:%S", &expiration)) {
90        RCTLogWarn(@"Invalid expiration date format. Valid format is YYYY-mm-ddTHH:MM:SS, e.g. 2017-02-01T00:00:00. All times UTC.");
91        return;
92    }
93
94    self.expirationDate = [NSDate dateWithTimeIntervalSince1970:timegm(&expiration)];
95}
96
97- (void)setLocallyIndexWithNumber:(NSNumber *)locallyIndex
98{
99    self.locallyIndex = locallyIndex.boolValue;
100}
101
102- (void)setPubliclyIndexWithNumber:(NSNumber *)publiclyIndex
103{
104    self.publiclyIndex = publiclyIndex.boolValue;
105}
106
107- (void)setContentMetadataWithMap:(NSDictionary *)map
108{
109    self.contentMetadata = [[BranchContentMetadata alloc] initWithMap:map];
110}
111
112@end
113