/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import <XCTest/XCTest.h>
#import <WebDriverAgentLib/XCElementSnapshot.h>
#import <WebDriverAgentLib/FBElement.h>

NS_ASSUME_NONNULL_BEGIN

@interface XCUIElement (FBUtilities)

/**
 Waits for receiver's frame to become stable with timeout
 */
- (BOOL)fb_waitUntilFrameIsStable;

/**
 Checks if receiver is obstructed by alert
 */
- (BOOL)fb_isObstructedByAlert;

/**
 Checks if receiver obstructs given element

 @param element tested element
 @return YES if receiver obstructs 'element', otherwise NO
 */
- (BOOL)fb_obstructsElement:(XCUIElement *)element;

/**
 Gets the most recent snapshot of the current element. The element will be 
 automatically resolved if the snapshot is not available yet
 
 @return The recent snapshot of the element
 */
- (XCElementSnapshot *)fb_lastSnapshot;

/**
 Categorizes all descendants of the current (self) element based on unique types provided in byTypes set
 
 @param byTypes set of XCUIElementType items
 @return The dictionary containing arrays of descendant elements as values categorized by their types as keys
 An empty dictionary will be returned in case if byTypes set has no items
 */
- (NSDictionary<NSNumber *, NSArray<XCUIElement *> *> *)fb_categorizeDescendants:(NSSet<NSNumber *> *)byTypes;

/**
 Filters elements from mapping generated by fb_categorizeDescendants method by matching them
 to snapshots from the corresponding array
 
 @param elementsMap Mapping of elements by types. This dictionary can be generated by fb_categorizeDescendants method
 @param snapshots Array of snapshots to be matched with
 @param useReversedOrder Set this to YES if elements are supposed to be enumerated in reversed order. This may
                         speed up search if potential matches are closer to the tail of the list
 @return Array of filtered elements, which have matches in snapshots array
 */
+ (NSArray<XCUIElement *> *)fb_filterElements:(NSDictionary<NSNumber *, NSArray<XCUIElement *> *> *)elementsMap matchingSnapshots:(NSArray<XCElementSnapshot *> *)snapshots useReversedOrder:(BOOL)useReversedOrder;

@end

NS_ASSUME_NONNULL_END
