//
//  NSArray+MFConvenience.h
//  myverizon
//
//  Created by Scott Pfeil on 12/5/13.
//  Copyright (c) 2013 Verizon Wireless. All rights reserved.
//
//  Adds frequently used convenience methods to NSArray.

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface NSArray (MFConvenience)

// Gets the object from the array and verfies that it is of a given type.
- (nullable id)objectAtIndex:(NSUInteger)index ofType:(nonnull Class)type;

// shorthand for objectAtIndex
- (nullable NSString *)optionalStringAtIndex:(NSUInteger)index;
- (nullable NSDictionary *)dictionaryAtIndex:(NSUInteger)index;
- (nullable NSArray *)arrayAtIndex:(NSUInteger)index;
- (BOOL)boolAtIndex:(NSUInteger)index;
- (NSInteger)integerAtIndex:(NSUInteger)index;
- (CGFloat)floatAtIndex:(NSUInteger)index;
- (double)doubleAtIndex:(NSUInteger)index;

// Gets an object that is nested using a series of keys or indexes to reach it.
// All keys should be of type NSString and is used for nested dictionaries.
// All indexes should be of type NSNumber and is used for nested arrays.
// Root object should be either a NSDictionary or NSArray.
- (nullable id)objectChainOfIndexesOrKeys:(nonnull NSArray *)indexesOrKeys;

// Gets an object that is nested using a series of keys or indexes to reach it and verifies it is of a specific type.
// See objectChainOfIndexesOrKeys
- (nullable id)objectChainOfIndexesOrKeys:(nonnull NSArray *)indexesOrKeys ofType:(nonnull Class)type;

// Gets the string object at a given index. Returns empty string if object at index is not a valid string.
- (nonnull NSString *)stringAtIndex:(NSUInteger)index;

// Maps this array to a new array of T objects given a conversion block. Objective-C implementation of Swift's map. https://developer.apple.com/documentation/swift/array/2908681-map
- (nonnull NSArray *)map:(id _Nonnull (^_Nonnull)(id _Nonnull obj))mapBlock;
- (nonnull NSArray *)filter:(BOOL (^_Nonnull)(id _Nonnull obj))block;
- (nonnull id)reduce:(id _Nonnull)initial block:(id _Nonnull (^_Nonnull)(id _Nonnull obj1,id _Nonnull obj2))block;
- (nonnull NSArray *)flatMap:(id _Nonnull (^_Nonnull)(id _Nonnull obj))block;

@end
