/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "RCTScrollView.h"

#import <React/RCTUIKit.h> // TODO(macOS ISS#2323203)

#import "RCTConvert.h"
#import "RCTLog.h"
#import "RCTScrollEvent.h"
#import "RCTUIManager.h"
#import "RCTUIManagerObserverCoordinator.h"
#import "RCTUIManagerUtils.h"
#import "RCTUtils.h"
#import "UIView+Private.h"
#import "UIView+React.h"


#if !TARGET_OS_TV && !TARGET_OS_OSX // TODO(macOS ISS#2323203)
#import "RCTRefreshControl.h"
#endif

/**
 * Include a custom scroll view subclass because we want to limit certain
 * default UIKit behaviors such as textFields automatically scrolling
 * scroll views that contain them.
 */
@interface RCTCustomScrollView :
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  RCTUIScrollView // TODO(macOS ISS#3536887)
#else
  UIScrollView <UIGestureRecognizerDelegate>
#endif // ]TODO(macOS ISS#2323203)

@property (nonatomic, assign) BOOL centerContent;
#if !TARGET_OS_TV && !TARGET_OS_OSX // TODO(macOS ISS#2323203)
@property (nonatomic, strong) UIView<RCTCustomRefreshContolProtocol> *customRefreshControl;
@property (nonatomic, assign) BOOL pinchGestureEnabled;
#endif
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
+ (BOOL)isCompatibleWithResponsiveScrolling;
@property (nonatomic, assign, getter=isScrollEnabled) BOOL scrollEnabled;
@property (nonatomic, strong) NSPanGestureRecognizer *panGestureRecognizer;
#endif // ]TODO(macOS ISS#2323203)
@end

@implementation RCTCustomScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
  if ((self = [super initWithFrame:frame])) {
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    self.scrollEnabled = YES;
    self.hasHorizontalScroller = YES;
    self.hasVerticalScroller = YES;
    self.panGestureRecognizer = [[NSPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCustomPan:)];
#else // ]TODO(macOS ISS#2323203)
    [self.panGestureRecognizer addTarget:self action:@selector(handleCustomPan:)];

    if ([self respondsToSelector:@selector(setSemanticContentAttribute:)]) {
      // We intentionally force `UIScrollView`s `semanticContentAttribute` to `LTR` here
      // because this attribute affects a position of vertical scrollbar; we don't want this
      // scrollbar flip because we also flip it with whole `UIScrollView` flip.
      self.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    }
#endif

#if !TARGET_OS_TV && !TARGET_OS_OSX // TODO(macOS ISS#2323203)
    _pinchGestureEnabled = YES;
#endif
  }
  return self;
}

#if !TARGET_OS_OSX // TODO(macOS ISS#2323203) NSScrollView's contentView is an NSClipView. Use documentView to access NSScrollView's content.
- (UIView *)contentView
{
  return ((RCTScrollView *)self.superview).contentView;
}
#endif // TODO(macOS ISS#2323203)

/**
 * @return Whether or not the scroll view interaction should be blocked because
 * JS was found to be the responder.
 */
- (BOOL)_shouldDisableScrollInteraction
{
  // Since this may be called on every pan, we need to make sure to only climb
  // the hierarchy on rare occasions.
  RCTPlatformView *JSResponder = [RCTUIManager JSResponder]; // TODO(macOS ISS#2323203)
  if (JSResponder && JSResponder != self.superview) {
    BOOL superviewHasResponder = RCTUIViewIsDescendantOfView(self, JSResponder); // TODO(macOS ISS#2323203) and TODO(macOS ISS#3536887)
    return superviewHasResponder;
  }
  return NO;
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
+ (BOOL)isCompatibleWithResponsiveScrolling
{
  return YES;
}

- (void)scrollWheel:(NSEvent *)theEvent
{
  if (!self.scrollEnabled) {
    [[self nextResponder] scrollWheel:theEvent];
    return;
  }
  [super scrollWheel:theEvent];
}
#endif // ]TODO(macOS ISS#2323203)

- (void)handleCustomPan:(__unused UIGestureRecognizer *)sender // TODO(macOS ISS#2323203)
{
  if ([self _shouldDisableScrollInteraction] && ![[RCTUIManager JSResponder] isKindOfClass:[RCTScrollView class]]) {
    self.panGestureRecognizer.enabled = NO;
    self.panGestureRecognizer.enabled = YES;
    // TODO: If mid bounce, animate the scroll view to a non-bounced position
    // while disabling (but only if `stopScrollInteractionIfJSHasResponder` was
    // called *during* a `pan`). Currently, it will just snap into place which
    // is not so bad either.
    // Another approach:
    // self.scrollEnabled = NO;
    // self.scrollEnabled = YES;
  }
}

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
{
  // Limiting scroll area to an area where we actually have content.
  CGSize contentSize = self.contentSize;
  UIEdgeInsets contentInset = self.contentInset;
  CGSize fullSize = CGSizeMake(
      contentSize.width + contentInset.left + contentInset.right,
      contentSize.height + contentInset.top + contentInset.bottom);

  rect = CGRectIntersection((CGRect){CGPointZero, fullSize}, rect);
  if (CGRectIsNull(rect)) {
    return;
  }
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
  [super scrollRectToVisible:rect animated:animated];
#else // [TODO(macOS ISS#2323203)
  [super scrollRectToVisible:rect];
#endif // ]TODO(macOS ISS#2323203)
}

/**
 * Returning `YES` cancels touches for the "inner" `view` and causes a scroll.
 * Returning `NO` causes touches to be directed to that inner view and prevents
 * the scroll view from scrolling.
 *
 * `YES` -> Allows scrolling.
 * `NO` -> Doesn't allow scrolling.
 *
 * By default this returns NO for all views that are UIControls and YES for
 * everything else. What that does is allows scroll views to scroll even when a
 * touch started inside of a `UIControl` (`UIButton` etc). For React scroll
 * views, we want the default to be the same behavior as `UIControl`s so we
 * return `YES` by default. But there's one case where we want to block the
 * scrolling no matter what: When JS believes it has its own responder lock on
 * a view that is *above* the scroll view in the hierarchy. So we abuse this
 * `touchesShouldCancelInContentView` API in order to stop the scroll view from
 * scrolling in this case.
 *
 * We are not aware of *any* other solution to the problem because alternative
 * approaches require that we disable the scrollview *before* touches begin or
 * move. This approach (`touchesShouldCancelInContentView`) works even if the
 * JS responder is set after touches start/move because
 * `touchesShouldCancelInContentView` is called as soon as the scroll view has
 * been touched and dragged *just* far enough to decide to begin the "drag"
 * movement of the scroll interaction. Returning `NO`, will cause the drag
 * operation to fail.
 *
 * `touchesShouldCancelInContentView` will stop the *initialization* of a
 * scroll pan gesture and most of the time this is sufficient. On rare
 * occasion, the scroll gesture would have already initialized right before JS
 * notifies native of the JS responder being set. In order to recover from that
 * timing issue we have a fallback that kills any ongoing pan gesture that
 * occurs when native is notified of a JS responder.
 *
 * Note: Explicitly returning `YES`, instead of relying on the default fixes
 * (at least) one bug where if you have a UIControl inside a UIScrollView and
 * tap on the UIControl and then start dragging (to scroll), it won't scroll.
 * Chat with @andras for more details.
 *
 * In order to have this called, you must have delaysContentTouches set to NO
 * (which is the not the `UIKit` default).
 */
- (BOOL)touchesShouldCancelInContentView:(__unused RCTUIView *)view // TODO(macOS ISS#3536887)
{
  BOOL shouldDisableScrollInteraction = [self _shouldDisableScrollInteraction];

#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  if (shouldDisableScrollInteraction == NO) {
    [super touchesShouldCancelInContentView:view];
  }
#endif // [TODO(macOS ISS#2323203)

  return !shouldDisableScrollInteraction;
}

/*
 * Automatically centers the content such that if the content is smaller than the
 * ScrollView, we force it to be centered, but when you zoom or the content otherwise
 * becomes larger than the ScrollView, there is no padding around the content but it
 * can still fill the whole view.
 */
- (void)setContentOffset:(CGPoint)contentOffset
{
  RCTUIView *contentView = nil; // TODO(macOS ISS#3536887)
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  contentView = (RCTUIView *) self.documentView;	// NSScrollView's documentView must be of type UIView/RCTView and TODO(macOS ISS#3536887)
#else
  contentView = [self contentView];
#endif // ]TODO(macOS ISS#2323203)
  if (contentView && _centerContent && !CGSizeEqualToSize(contentView.frame.size, CGSizeZero)) {
    CGSize subviewSize = contentView.frame.size;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    CGSize scrollViewSize = self.contentView.bounds.size;
#else // ]TODO(macOS ISS#2323203)
    CGSize scrollViewSize = self.bounds.size;
#endif // TODO(macOS ISS#2323203)
    if (subviewSize.width <= scrollViewSize.width) {
      contentOffset.x = -(scrollViewSize.width - subviewSize.width) / 2.0;
    }
    if (subviewSize.height <= scrollViewSize.height) {
      contentOffset.y = -(scrollViewSize.height - subviewSize.height) / 2.0;
    }
  }
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  if (!NSEqualPoints(contentOffset, self.documentVisibleRect.origin))
  {
    [self.contentView scrollToPoint:contentOffset];
  }
#else // ]TODO(macOS ISS#2323203)
  super.contentOffset = CGPointMake(
      RCTSanitizeNaNValue(contentOffset.x, @"scrollView.contentOffset.x"),
      RCTSanitizeNaNValue(contentOffset.y, @"scrollView.contentOffset.y"));
#endif // TODO(macOS ISS#2323203)
}

- (void)setFrame:(CGRect)frame
{
  // Preserving and revalidating `contentOffset`.
  CGPoint originalOffset = self.contentOffset;

  [super setFrame:frame];

  UIEdgeInsets contentInset = self.contentInset;
  CGSize contentSize = self.contentSize;

  // If contentSize has not been measured yet we can't check bounds.
  if (CGSizeEqualToSize(contentSize, CGSizeZero)) {
    self.contentOffset = originalOffset;
  } else {
#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    if (@available(iOS 11.0, *)) {
      if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, self.adjustedContentInset)) {
        contentInset = self.adjustedContentInset;
      }
    }
#endif // [TODO(macOS ISS#2323203)
    CGSize boundsSize = self.bounds.size;
    CGFloat xMaxOffset = contentSize.width - boundsSize.width + contentInset.right;
    CGFloat yMaxOffset = contentSize.height - boundsSize.height + contentInset.bottom;
    // Make sure offset doesn't exceed bounds. This can happen on screen rotation.
    if ((originalOffset.x >= -contentInset.left) && (originalOffset.x <= xMaxOffset) &&
        (originalOffset.y >= -contentInset.top) && (originalOffset.y <= yMaxOffset)) {
      return;
    }
    self.contentOffset = CGPointMake(
        MAX(-contentInset.left, MIN(xMaxOffset, originalOffset.x)),
        MAX(-contentInset.top, MIN(yMaxOffset, originalOffset.y)));
  }
}

#if !TARGET_OS_TV && !TARGET_OS_OSX // TODO(macOS ISS#2323203)
- (void)setCustomRefreshControl:(UIView<RCTCustomRefreshContolProtocol> *)refreshControl
{
  if (_customRefreshControl) {
    [_customRefreshControl removeFromSuperview];
  }
  _customRefreshControl = refreshControl;
  [self addSubview:_customRefreshControl];
}

- (void)setPinchGestureEnabled:(BOOL)pinchGestureEnabled
{
  self.pinchGestureRecognizer.enabled = pinchGestureEnabled;
  _pinchGestureEnabled = pinchGestureEnabled;
}

- (void)didMoveToWindow
{
  [super didMoveToWindow];
  // ScrollView enables pinch gesture late in its lifecycle. So simply setting it
  // in the setter gets overridden when the view loads.
  self.pinchGestureRecognizer.enabled = _pinchGestureEnabled;
}
#endif // TARGET_OS_TV

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (BOOL)canBecomeFirstResponder
{
	return YES;
}

- (BOOL)becomeFirstResponder
{
	return YES;
}

- (BOOL)resignFirstResponder
{
	return YES;
}

- (void)setAccessibilityLabel:(NSString *)accessibilityLabel 
{
  [super setAccessibilityLabel:accessibilityLabel];
  [[self documentView] setAccessibilityLabel:accessibilityLabel];
}
- (void)setDocumentView:(__kindof NSView *)documentView 
{
  [super setDocumentView:documentView];
  [documentView setAccessibilityLabel:[self accessibilityLabel]];
}
#endif // ]TODO(macOS ISS#2323203)

- (BOOL)shouldGroupAccessibilityChildren
{
  return YES;
}

@end

@interface RCTScrollView () <RCTUIManagerObserver>

@end

@implementation RCTScrollView {
  RCTEventDispatcher *_eventDispatcher;
  CGRect _prevFirstVisibleFrame;
  __weak RCTUIView *_firstVisibleView; // TODO(macOS ISS#3536887)
  RCTCustomScrollView *_scrollView;
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
  UIView *_contentView;
#endif // TODO(macOS ISS#2323203)
  NSTimeInterval _lastScrollDispatchTime;
  NSMutableArray<NSValue *> *_cachedChildFrames;
  BOOL _allowNextScrollNoMatterWhat;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  BOOL _notifyDidScroll;
#endif // ]TODO(macOS ISS#2323203)
  CGRect _lastClippedToRect;
  uint16_t _coalescingKey;
  NSString *_lastEmittedEventName;
  NSHashTable *_scrollListeners;
}

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
  RCTAssertParam(eventDispatcher);

  if ((self = [super initWithFrame:CGRectZero])) {
    _eventDispatcher = eventDispatcher;

    _scrollView = [[RCTCustomScrollView alloc] initWithFrame:CGRectZero];
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    _scrollView.drawsBackground = NO;
    _scrollView.postsBoundsChangedNotifications = YES;
#else // ]TODO(macOS ISS#2323203)
    _scrollView.delegate = self;
    _scrollView.delaysContentTouches = NO;
#endif // TODO(macOS ISS#2323203)

#pragma clang diagnostic push // TODO(OSS Candidate ISS#2710739)
#pragma clang diagnostic ignored "-Wunguarded-availability" // TODO(OSS Candidate ISS#2710739)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
    // `contentInsetAdjustmentBehavior` is only available since iOS 11.
    // We set the default behavior to "never" so that iOS
    // doesn't do weird things to UIScrollView insets automatically
    // and keeps it as an opt-in behavior.
    if ([_scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
      if (@available(iOS 11.0, *)) {
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
      }
    }
#endif
#pragma clang diagnostic pop // TODO(OSS Candidate ISS#2710739)

    _automaticallyAdjustContentInsets = YES;
    _DEPRECATED_sendUpdatedChildFrames = NO;
    _contentInset = UIEdgeInsetsZero;
    _contentSize = CGSizeZero;
    _lastClippedToRect = CGRectNull;

    _scrollEventThrottle = 0.0;
    _lastScrollDispatchTime = 0;
    _cachedChildFrames = [NSMutableArray new];

    _scrollListeners = [NSHashTable weakObjectsHashTable];

    [self addSubview:_scrollView];
  }
  return self;
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (BOOL)canBecomeKeyView
{
  return [self focusable];
}

- (CGRect)focusRingMaskBounds
{
  return [self bounds];
}

- (void)drawFocusRingMask
{
  if (self.enableFocusRing) {
    NSBezierPath *borderPath = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:2.0 yRadius:2.0];
    [borderPath stroke];
  }
}

- (RCTBridge *)bridge
{
  return [_eventDispatcher bridge];
}

- (RCTUIView *)contentView // TODO(macOS ISS#3536887)
{
  return _scrollView.documentView;
}

- (BOOL)canBecomeFirstResponder
{
  return [_scrollView canBecomeFirstResponder];
}

- (BOOL)becomeFirstResponder
{
  return [_scrollView becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
  return [_scrollView resignFirstResponder];
}

- (void)setAccessibilityLabel:(NSString *)accessibilityLabel 
{
  [_scrollView setAccessibilityLabel:accessibilityLabel];
}
#endif // ]TODO(macOS ISS#2323203)

RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)

static inline void RCTApplyTransformationAccordingLayoutDirection(
    RCTPlatformView *view, // TODO(macOS ISS#2323203)
    UIUserInterfaceLayoutDirection layoutDirection)
{
#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  view.transform = layoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ? CGAffineTransformIdentity
                                                                                : CGAffineTransformMakeScale(-1, 1);
#endif // ]TODO(macOS ISS#2323203)
}

- (void)setReactLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
{
  [super setReactLayoutDirection:layoutDirection];

  RCTApplyTransformationAccordingLayoutDirection(_scrollView, layoutDirection);
  RCTApplyTransformationAccordingLayoutDirection(self.contentView, layoutDirection); // TODO(OSS Candidate ISS#2710739) use property instead of ivar for mac
}

- (void)setRemoveClippedSubviews:(__unused BOOL)removeClippedSubviews
{
  // Does nothing
}

- (void)insertReactSubview:(RCTUIView *)view atIndex:(NSInteger)atIndex // TODO(macOS ISS#3536887)
{
  [super insertReactSubview:view atIndex:atIndex];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
	RCTAssert(self.contentView == nil, @"RCTScrollView may only contain a single subview");
	
  _scrollView.documentView = view;
#else // ]TODO(macOS ISS#2323203)
#if !TARGET_OS_TV
  if ([view conformsToProtocol:@protocol(RCTCustomRefreshContolProtocol)]) {
    [_scrollView setCustomRefreshControl:(UIView<RCTCustomRefreshContolProtocol> *)view];
    if (![view isKindOfClass:[UIRefreshControl class]] && [view conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
      [self addScrollListener:(UIView<UIScrollViewDelegate> *)view];
    }
  } else
#endif
  {
    RCTAssert(
        _contentView == nil,
        @"RCTScrollView may only contain a single subview, the already set subview looks like: %@",
        [_contentView react_recursiveDescription]);
    _contentView = view;
    RCTApplyTransformationAccordingLayoutDirection(_contentView, self.reactLayoutDirection);
    [_scrollView addSubview:view];
  }
#endif // TODO(macOS ISS#2323203)
}

- (void)removeReactSubview:(RCTUIView *)subview // TODO(macOS ISS#3536887)
{
  [super removeReactSubview:subview];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  _scrollView.documentView = nil;
  _contentSize = CGSizeZero;
#else // ]TODO(macOS ISS#2323203)
#if !TARGET_OS_TV
  if ([subview conformsToProtocol:@protocol(RCTCustomRefreshContolProtocol)]) {
    [_scrollView setCustomRefreshControl:nil];
    if (![subview isKindOfClass:[UIRefreshControl class]] &&
        [subview conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
      [self removeScrollListener:(UIView<UIScrollViewDelegate> *)subview];
    }
  } else
#endif
  {
    RCTAssert(_contentView == subview, @"Attempted to remove non-existent subview");
    _contentView = nil;
  }
#endif // TODO(macOS ISS#2323203)
}

- (void)didUpdateReactSubviews
{
  // Do nothing, as subviews are managed by `insertReactSubview:atIndex:`
}

- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
  if ([changedProps containsObject:@"contentSize"]) {
    [self updateContentOffsetIfNeeded];
  }
}

- (BOOL)centerContent
{
  return _scrollView.centerContent;
}

- (void)setCenterContent:(BOOL)centerContent
{
  _scrollView.centerContent = centerContent;
}

- (void)setClipsToBounds:(BOOL)clipsToBounds
{
  super.clipsToBounds = clipsToBounds;
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
  _scrollView.clipsToBounds = clipsToBounds;
#endif // TODO(macOS ISS#2323203)
}

- (void)dealloc
{
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
  _scrollView.delegate = nil;
#endif // TODO(macOS ISS#2323203)
  [_eventDispatcher.bridge.uiManager.observerCoordinator removeObserver:self];
}

- (void)layoutSubviews
{
  [super layoutSubviews];
  RCTAssert(self.subviews.count == 1, @"we should only have exactly one subview");
  RCTAssert([self.subviews lastObject] == _scrollView, @"our only subview should be a scrollview");

#if !TARGET_OS_TV && !TARGET_OS_OSX // TODO(macOS ISS#2323203)
  // Adjust the refresh control frame if the scrollview layout changes.
  UIView<RCTCustomRefreshContolProtocol> *refreshControl = _scrollView.customRefreshControl;
  if (refreshControl && refreshControl.isRefreshing) {
    refreshControl.frame =
        (CGRect){_scrollView.contentOffset, {_scrollView.frame.size.width, refreshControl.frame.size.height}};
  }
#endif

  [self updateClippedSubviews];
}

- (void)updateClippedSubviews
{
  // Find a suitable view to use for clipping
  RCTPlatformView *clipView = [self react_findClipView]; // TODO(macOS ISS#2323203)
  if (!clipView) {
    return;
  }

  static const CGFloat leeway = 1.0;

  const CGSize contentSize = _scrollView.contentSize;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  const CGRect bounds = _scrollView.contentView.bounds;
#else
	const CGRect bounds = _scrollView.bounds;
#endif // ]TODO(macOS ISS#2323203)
  const BOOL scrollsHorizontally = contentSize.width > bounds.size.width;
  const BOOL scrollsVertically = contentSize.height > bounds.size.height;

  const BOOL shouldClipAgain = CGRectIsNull(_lastClippedToRect) || !CGRectEqualToRect(_lastClippedToRect, bounds) ||
      (scrollsHorizontally &&
       (bounds.size.width < leeway || fabs(_lastClippedToRect.origin.x - bounds.origin.x) >= leeway)) ||
      (scrollsVertically &&
       (bounds.size.height < leeway || fabs(_lastClippedToRect.origin.y - bounds.origin.y) >= leeway));

  if (shouldClipAgain) {
    const CGRect clipRect = CGRectInset(clipView.bounds, -leeway, -leeway);
    [self react_updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
    _lastClippedToRect = bounds;
  }

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  [[self scrollView] setHasHorizontalScroller:[self isHorizontal:_scrollView]];
  [[self scrollView] setHasVerticalScroller:[self isVertical:_scrollView]];
#endif // ]TODO(macOS ISS#2323203)
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (void)viewDidMoveToWindow
{
  [super viewDidMoveToWindow];

  NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  if ([self window] == nil) {
    // Unregister for bounds change notifications
    [defaultCenter removeObserver:self
                             name:NSViewBoundsDidChangeNotification
                           object:_scrollView.contentView];
    [defaultCenter removeObserver:self
                             name:NSPreferredScrollerStyleDidChangeNotification
                           object:nil];
  } else {
    // Register for bounds change notifications so we can track scrolling
    [defaultCenter addObserver:self
                      selector:@selector(scrollViewDocumentViewBoundsDidChange:)
                          name:NSViewBoundsDidChangeNotification
                        object:_scrollView.contentView]; // NSClipView
    [defaultCenter addObserver:self
                      selector:@selector(preferredScrollerStyleDidChange:)
                          name:NSPreferredScrollerStyleDidChangeNotification
                        object:nil];
  }

  _notifyDidScroll = ([self window] != nil);
}
#endif // ]TODO(macOS ISS#2323203)

- (void)setContentInset:(UIEdgeInsets)contentInset
{
  if (UIEdgeInsetsEqualToEdgeInsets(contentInset, _contentInset)) {
    return;
  }

  CGPoint contentOffset = _scrollView.contentOffset;

  _contentInset = contentInset;
  [RCTView autoAdjustInsetsForView:self withScrollView:_scrollView updateOffset:NO];

  _scrollView.contentOffset = contentOffset;
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (BOOL)isHorizontal:(RCTCustomScrollView *)scrollView
#else // ]TODO(macOS ISS#2323203)
- (BOOL)isHorizontal:(UIScrollView *)scrollView
#endif // TODO(macOS ISS#2323203)
{
  return scrollView.contentSize.width > self.frame.size.width;
}

#if TARGET_OS_OSX // [TODO(macOS Candidate ISS#2710739)
- (BOOL)isVertical:(RCTCustomScrollView *)scrollView
{
  return scrollView.contentSize.height > self.frame.size.height;
}
#endif // ]TODO(macOS Candidate ISS#2710739)

- (void)scrollToOffset:(CGPoint)offset
{
  [self scrollToOffset:offset animated:YES];
}

- (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
{
  if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
    CGRect maxRect = CGRectMake(
        fmin(-_scrollView.contentInset.left, 0),
        fmin(-_scrollView.contentInset.top, 0),
        fmax(
            _scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right +
                fmax(_scrollView.contentInset.left, 0),
            0.01),
        fmax(
            _scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom +
                fmax(_scrollView.contentInset.top, 0),
            0.01)); // Make width and height greater than 0
    // Ensure at least one scroll event will fire
    _allowNextScrollNoMatterWhat = YES;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    (void) animated;
    _scrollView.contentOffset = offset;
#else // ]TODO(macOS ISS#2323203)
    if (!CGRectContainsPoint(maxRect, offset) && !self.scrollToOverflowEnabled) {
      CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect));
      x = fmin(x, CGRectGetMaxX(maxRect));
      CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect));
      y = fmin(y, CGRectGetMaxY(maxRect));
      offset = CGPointMake(x, y);
    }
    [_scrollView setContentOffset:offset animated:animated];
#endif // TODO(macOS ISS#2323203)
  }
}

/**
 * If this is a vertical scroll view, scrolls to the bottom.
 * If this is a horizontal scroll view, scrolls to the right.
 */
- (void)scrollToEnd:(BOOL)animated
{
  BOOL isHorizontal = [self isHorizontal:_scrollView];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  CGSize boundsSize = _scrollView.contentView.bounds.size;
#else
  CGSize boundsSize = _scrollView.bounds.size;
#endif // ]TODO(macOS ISS#2323203)
  CGPoint offset;
  if (isHorizontal) {
    CGFloat offsetX = _scrollView.contentSize.width - boundsSize.width + _scrollView.contentInset.right; // TODO(macOS ISS#2323203)
    offset = CGPointMake(fmax(offsetX, 0), 0);
  } else {
    CGFloat offsetY = _scrollView.contentSize.height - boundsSize.height + _scrollView.contentInset.bottom; // TODO(macOS ISS#2323203)
    offset = CGPointMake(0, fmax(offsetY, 0));
  }
  if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
    // Ensure at least one scroll event will fire
    _allowNextScrollNoMatterWhat = YES;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    (void) animated;
    _scrollView.contentOffset = offset;
#else // ]TODO(macOS ISS#2323203)
    [_scrollView setContentOffset:offset animated:animated];
#endif // TODO(macOS ISS#2323203)
  }
}

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
{
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  (void) animated;
  [_scrollView magnifyToFitRect:rect];
#else // ]TODO(macOS ISS#2323203)
  [_scrollView zoomToRect:rect animated:animated];
#endif // TODO(macOS ISS#2323203)
}

- (void)refreshContentInset
{
  [RCTView autoAdjustInsetsForView:self withScrollView:_scrollView updateOffset:YES];
}

// [TODO(macOS ISS#2323203)
- (void)flashScrollIndicators
{
#if TARGET_OS_OSX
  [_scrollView flashScrollers];
#else
	[_scrollView flashScrollIndicators];
#endif
}
// ]TODO(macOS ISS#2323203)
 
#pragma mark - ScrollView delegate

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (void)scrollViewDocumentViewBoundsDidChange:(__unused NSNotification *)notification
{
  if (_scrollView.centerContent) {
    _scrollView.contentOffset = _scrollView.contentOffset; // necessary for content centering when _centerContent == YES
  }

  // if scrollView is not ready, don't notify with scroll event
  if (_notifyDidScroll) {
    [self scrollViewDidScroll:_scrollView];
  }
}
#endif // ]TODO(macOS ISS#2323203)

#define RCT_SEND_SCROLL_EVENT(_eventName, _userData)                                    \
  {                                                                                     \
    NSString *eventName = NSStringFromSelector(@selector(_eventName));                  \
    [self sendScrollEventWithName:eventName scrollView:_scrollView userData:_userData]; \
  }

#define RCT_FORWARD_SCROLL_EVENT(call)                                            \
  for (NSObject<UIScrollViewDelegate> * scrollViewListener in _scrollListeners) { \
    if ([scrollViewListener respondsToSelector:_cmd]) {                           \
      [scrollViewListener call];                                                  \
    }                                                                             \
  }

#define RCT_SCROLL_EVENT_HANDLER(delegateMethod, eventName) \
  -(void)delegateMethod : (UIScrollView *)scrollView        \
  {                                                         \
    RCT_SEND_SCROLL_EVENT(eventName, nil);                  \
    RCT_FORWARD_SCROLL_EVENT(delegateMethod : scrollView);  \
  }

#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)

RCT_SCROLL_EVENT_HANDLER(scrollViewWillBeginDecelerating, onMomentumScrollBegin)
RCT_SCROLL_EVENT_HANDLER(scrollViewDidZoom, onScroll)
RCT_SCROLL_EVENT_HANDLER(scrollViewDidScrollToTop, onScrollToTop)

- (void)addScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener
{
  [_scrollListeners addObject:scrollListener];
}

- (void)removeScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener
{
  [_scrollListeners removeObject:scrollListener];
}

#endif // TODO(macOS ISS#2323203)

- (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // TODO(macOS ISS#2323203)
{
  NSTimeInterval now = CACurrentMediaTime();
  [self updateClippedSubviews];
  /**
   * TODO: this logic looks wrong, and it may be because it is. Currently, if _scrollEventThrottle
   * is set to zero (the default), the "didScroll" event is only sent once per scroll, instead of repeatedly
   * while scrolling as expected. However, if you "fix" that bug, ScrollView will generate repeated
   * warnings, and behave strangely (ListView works fine however), so don't fix it unless you fix that too!
   *
   * We limit the delta to 17ms so that small throttles intended to enable 60fps updates will not
   * inadvertently filter out any scroll events.
   */
  if (_allowNextScrollNoMatterWhat ||
      (_scrollEventThrottle > 0 && _scrollEventThrottle < MAX(0.017, now - _lastScrollDispatchTime))) {
    if (_DEPRECATED_sendUpdatedChildFrames) {
      // Calculate changed frames
      RCT_SEND_SCROLL_EVENT(onScroll, (@{@"updatedChildFrames" : [self calculateChildFramesData]}));
    } else {
      RCT_SEND_SCROLL_EVENT(onScroll, nil);
    }

    // Update dispatch time
    _lastScrollDispatchTime = now;
    _allowNextScrollNoMatterWhat = NO;
  }
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
  (void) scrollView;
#else // ]TODO(macOS ISS#2323203)
  RCT_FORWARD_SCROLL_EVENT(scrollViewDidScroll : scrollView);
#endif // TODO(macOS ISS#2323203)
}

- (NSArray<NSDictionary *> *)calculateChildFramesData
{
  NSMutableArray<NSDictionary *> *updatedChildFrames = [NSMutableArray new];
  [[self.contentView reactSubviews] enumerateObjectsUsingBlock: // TODO(OSS Candidate ISS#2710739) use p
    ^(RCTPlatformView *subview, NSUInteger idx, __unused BOOL *stop) { // TODO(macOS ISS#2323203)

    // Check if new or changed
    CGRect newFrame = subview.frame;
    BOOL frameChanged = NO;
    if (self->_cachedChildFrames.count <= idx) {
      frameChanged = YES;
      [self->_cachedChildFrames addObject:NSValueWithCGRect(newFrame)]; // TODO(macOS ISS#2323203)
    } else if (!CGRectEqualToRect(newFrame, CGRectValue(self->_cachedChildFrames[idx]))) { // TODO(macOS ISS#2323203)
      frameChanged = YES;
      self->_cachedChildFrames[idx] = NSValueWithCGRect(newFrame); // TODO(macOS ISS#2323203)
    }

    // Create JS frame object
    if (frameChanged) {
      [updatedChildFrames addObject:@{
        @"index" : @(idx),
        @"x" : @(newFrame.origin.x),
        @"y" : @(newFrame.origin.y),
        @"width" : @(newFrame.size.width),
        @"height" : @(newFrame.size.height),
      }];
    }
  }];

  return updatedChildFrames;
}

#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  _allowNextScrollNoMatterWhat = YES; // Ensure next scroll event is recorded, regardless of throttle
  RCT_SEND_SCROLL_EVENT(onScrollBeginDrag, nil);
  RCT_FORWARD_SCROLL_EVENT(scrollViewWillBeginDragging : scrollView);
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset
{
  if (self.snapToOffsets) {
    // An alternative to enablePaging and snapToInterval which allows setting custom
    // stopping points that don't have to be the same distance apart. Often seen in
    // apps which feature horizonally scrolling items. snapToInterval does not enforce
    // scrolling one interval at a time but guarantees that the scroll will stop at
    // a snap offset point.

    // Find which axis to snap
    BOOL isHorizontal = [self isHorizontal:scrollView];
    CGFloat velocityAlongAxis = isHorizontal ? velocity.x : velocity.y;
    CGFloat offsetAlongAxis = isHorizontal ? _scrollView.contentOffset.x : _scrollView.contentOffset.y;

    // Calculate maximum content offset
    CGSize viewportSize = [self _calculateViewportSize];
    CGFloat maximumOffset = isHorizontal ? MAX(0, _scrollView.contentSize.width - viewportSize.width)
                                         : MAX(0, _scrollView.contentSize.height - viewportSize.height);

    // Calculate the snap offsets adjacent to the initial offset target
    CGFloat targetOffset = isHorizontal ? targetContentOffset->x : targetContentOffset->y;
    CGFloat smallerOffset = 0.0;
    CGFloat largerOffset = maximumOffset;

    for (unsigned long i = 0; i < self.snapToOffsets.count; i++) {
      CGFloat offset = [[self.snapToOffsets objectAtIndex:i] floatValue];

      if (offset <= targetOffset) {
        if (targetOffset - offset < targetOffset - smallerOffset) {
          smallerOffset = offset;
        }
      }

      if (offset >= targetOffset) {
        if (offset - targetOffset < largerOffset - targetOffset) {
          largerOffset = offset;
        }
      }
    }

    // Calculate the nearest offset
    CGFloat nearestOffset = targetOffset - smallerOffset < largerOffset - targetOffset ? smallerOffset : largerOffset;

    CGFloat firstOffset = [[self.snapToOffsets firstObject] floatValue];
    CGFloat lastOffset = [[self.snapToOffsets lastObject] floatValue];

    // if scrolling after the last snap offset and snapping to the
    // end of the list is disabled, then we allow free scrolling
    if (!self.snapToEnd && targetOffset >= lastOffset) {
      if (offsetAlongAxis >= lastOffset) {
        // free scrolling
      } else {
        // snap to end
        targetOffset = lastOffset;
      }
    } else if (!self.snapToStart && targetOffset <= firstOffset) {
      if (offsetAlongAxis <= firstOffset) {
        // free scrolling
      } else {
        // snap to beginning
        targetOffset = firstOffset;
      }
    } else if (velocityAlongAxis > 0.0) {
      targetOffset = largerOffset;
    } else if (velocityAlongAxis < 0.0) {
      targetOffset = smallerOffset;
    } else {
      targetOffset = nearestOffset;
    }

    // Make sure the new offset isn't out of bounds
    targetOffset = MIN(MAX(0, targetOffset), maximumOffset);

    // Set new targetContentOffset
    if (isHorizontal) {
      targetContentOffset->x = targetOffset;
    } else {
      targetContentOffset->y = targetOffset;
    }
  } else if (self.snapToInterval) {
    // An alternative to enablePaging which allows setting custom stopping intervals,
    // smaller than a full page size. Often seen in apps which feature horizonally
    // scrolling items. snapToInterval does not enforce scrolling one interval at a time
    // but guarantees that the scroll will stop at an interval point.
    CGFloat snapToIntervalF = (CGFloat)self.snapToInterval;

    // Find which axis to snap
    BOOL isHorizontal = [self isHorizontal:scrollView];

    // What is the current offset?
    CGFloat velocityAlongAxis = isHorizontal ? velocity.x : velocity.y;
    CGFloat targetContentOffsetAlongAxis = targetContentOffset->y;
    if (isHorizontal) {
      // Use current scroll offset to determine the next index to snap to when momentum disabled
      targetContentOffsetAlongAxis = self.disableIntervalMomentum ? scrollView.contentOffset.x : targetContentOffset->x;
    } else {
      targetContentOffsetAlongAxis = self.disableIntervalMomentum ? scrollView.contentOffset.y : targetContentOffset->y;
    }

    // Offset based on desired alignment
    CGFloat frameLength = isHorizontal ? self.frame.size.width : self.frame.size.height;
    CGFloat alignmentOffset = 0.0f;
    if ([self.snapToAlignment isEqualToString:@"center"]) {
      alignmentOffset = (frameLength * 0.5f) + (snapToIntervalF * 0.5f);
    } else if ([self.snapToAlignment isEqualToString:@"end"]) {
      alignmentOffset = frameLength;
    }

    // Pick snap point based on direction and proximity
    CGFloat fractionalIndex = (targetContentOffsetAlongAxis + alignmentOffset) / snapToIntervalF;

    NSInteger snapIndex = velocityAlongAxis > 0.0
        ? ceil(fractionalIndex)
        : velocityAlongAxis < 0.0 ? floor(fractionalIndex) : round(fractionalIndex);
    CGFloat newTargetContentOffset = (snapIndex * snapToIntervalF) - alignmentOffset;

    // Set new targetContentOffset
    if (isHorizontal) {
      targetContentOffset->x = newTargetContentOffset;
    } else {
      targetContentOffset->y = newTargetContentOffset;
    }
  }

  NSDictionary *userData = @{
    @"velocity" : @{@"x" : @(velocity.x), @"y" : @(velocity.y)},
    @"targetContentOffset" : @{@"x" : @(targetContentOffset->x), @"y" : @(targetContentOffset->y)}
  };
  RCT_SEND_SCROLL_EVENT(onScrollEndDrag, userData);
  RCT_FORWARD_SCROLL_EVENT(scrollViewWillEndDragging
                           : scrollView withVelocity
                           : velocity targetContentOffset
                           : targetContentOffset);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
  RCT_FORWARD_SCROLL_EVENT(scrollViewDidEndDragging : scrollView willDecelerate : decelerate);
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
  RCT_SEND_SCROLL_EVENT(onScrollBeginDrag, nil);
  RCT_FORWARD_SCROLL_EVENT(scrollViewWillBeginZooming : scrollView withView : view);
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
  RCT_SEND_SCROLL_EVENT(onScrollEndDrag, nil);
  RCT_FORWARD_SCROLL_EVENT(scrollViewDidEndZooming : scrollView withView : view atScale : scale);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  // Fire a final scroll event
  _allowNextScrollNoMatterWhat = YES;
  [self scrollViewDidScroll:scrollView];

  // Fire the end deceleration event
  RCT_SEND_SCROLL_EVENT(onMomentumScrollEnd, nil);
  RCT_FORWARD_SCROLL_EVENT(scrollViewDidEndDecelerating : scrollView);
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
  // Fire a final scroll event
  _allowNextScrollNoMatterWhat = YES;
  [self scrollViewDidScroll:scrollView];

  // Fire the end deceleration event
  RCT_SEND_SCROLL_EVENT(onMomentumScrollEnd, nil);
  RCT_FORWARD_SCROLL_EVENT(scrollViewDidEndScrollingAnimation : scrollView);
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
  for (NSObject<UIScrollViewDelegate> *scrollListener in _scrollListeners) {
    if ([scrollListener respondsToSelector:_cmd] && ![scrollListener scrollViewShouldScrollToTop:scrollView]) {
      return NO;
    }
  }

  if (self.inverted) {
    [self scrollToEnd:YES];
    return NO;
  }

  return YES;
}

- (UIView *)viewForZoomingInScrollView:(__unused UIScrollView *)scrollView
{
  return _contentView;
}

#endif // TODO(macOS ISS#2323203)

#pragma mark - Setters

- (CGSize)_calculateViewportSize
{
  CGSize viewportSize = self.bounds.size;
  if (_automaticallyAdjustContentInsets) {
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
    contentInsets = [RCTView contentInsetsForView:self];
#endif // ]TODO(macOS ISS#2323203)
    viewportSize = CGSizeMake(
        self.bounds.size.width - contentInsets.left - contentInsets.right,
        self.bounds.size.height - contentInsets.top - contentInsets.bottom);
  }
  return viewportSize;
}

- (CGPoint)calculateOffsetForContentSize:(CGSize)newContentSize
{
  CGPoint oldOffset = _scrollView.contentOffset;
  CGPoint newOffset = oldOffset;

  CGSize oldContentSize = _scrollView.contentSize;
  CGSize viewportSize = [self _calculateViewportSize];

  BOOL fitsinViewportY = oldContentSize.height <= viewportSize.height && newContentSize.height <= viewportSize.height;
  if (newContentSize.height < oldContentSize.height && !fitsinViewportY) {
    CGFloat offsetHeight = oldOffset.y + viewportSize.height;
    if (oldOffset.y < 0) {
      // overscrolled on top, leave offset alone
    } else if (offsetHeight > oldContentSize.height) {
      // overscrolled on the bottom, preserve overscroll amount
      newOffset.y = MAX(0, oldOffset.y - (oldContentSize.height - newContentSize.height));
    } else if (offsetHeight > newContentSize.height) {
      // offset falls outside of bounds, scroll back to end of list
      newOffset.y = MAX(0, newContentSize.height - viewportSize.height);
    }
  }

  BOOL fitsinViewportX = oldContentSize.width <= viewportSize.width && newContentSize.width <= viewportSize.width;
  if (newContentSize.width < oldContentSize.width && !fitsinViewportX) {
    CGFloat offsetHeight = oldOffset.x + viewportSize.width;
    if (oldOffset.x < 0) {
      // overscrolled at the beginning, leave offset alone
    } else if (offsetHeight > oldContentSize.width && newContentSize.width > viewportSize.width) {
      // overscrolled at the end, preserve overscroll amount as much as possible
      newOffset.x = MAX(0, oldOffset.x - (oldContentSize.width - newContentSize.width));
    } else if (offsetHeight > newContentSize.width) {
      // offset falls outside of bounds, scroll back to end
      newOffset.x = MAX(0, newContentSize.width - viewportSize.width);
    }
  }

  // all other cases, offset doesn't change
  return newOffset;
}

/**
 * Once you set the `contentSize`, to a nonzero value, it is assumed to be
 * managed by you, and we'll never automatically compute the size for you,
 * unless you manually reset it back to {0, 0}
 */
- (CGSize)contentSize
{
  if (!CGSizeEqualToSize(_contentSize, CGSizeZero)) {
    return _contentSize;
  }

  return self.contentView.frame.size; // TODO(OSS Candidate ISS#2710739) use property instead of ivar for mac
}

- (void)updateContentOffsetIfNeeded
{
  CGSize contentSize = self.contentSize;
  if (!CGSizeEqualToSize(_scrollView.contentSize, contentSize)) {
    // When contentSize is set manually, ScrollView internals will reset
    // contentOffset to  {0, 0}. Since we potentially set contentSize whenever
    // anything in the ScrollView updates, we workaround this issue by manually
    // adjusting contentOffset whenever this happens
    CGPoint newOffset = [self calculateOffsetForContentSize:contentSize];
    _scrollView.contentSize = contentSize;
    _scrollView.contentOffset = newOffset;
  }
}

// maintainVisibleContentPosition is used to allow seamless loading of content from both ends of
// the scrollview without the visible content jumping in position.
- (void)setMaintainVisibleContentPosition:(NSDictionary *)maintainVisibleContentPosition
{
  if (maintainVisibleContentPosition != nil && _maintainVisibleContentPosition == nil) {
    [_eventDispatcher.bridge.uiManager.observerCoordinator addObserver:self];
  } else if (maintainVisibleContentPosition == nil && _maintainVisibleContentPosition != nil) {
    [_eventDispatcher.bridge.uiManager.observerCoordinator removeObserver:self];
  }
  _maintainVisibleContentPosition = maintainVisibleContentPosition;
}

#pragma mark - RCTUIManagerObserver

- (void)uiManagerWillPerformMounting:(RCTUIManager *)manager
{
  RCTAssertUIManagerQueue();
  [manager
      prependUIBlock:^(__unused RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, RCTUIView *> *viewRegistry) { // TODO(macOS ISS#3536887)
        BOOL horz = [self isHorizontal:self->_scrollView];
        NSUInteger minIdx = [self->_maintainVisibleContentPosition[@"minIndexForVisible"] integerValue];
        for (NSUInteger ii = minIdx; ii < self.contentView.subviews.count; ++ii) { // TODO(OSS Candidate ISS#2710739) use property instead of ivar for mac
          // Find the first entirely visible view. This must be done after we update the content offset
          // or it will tend to grab rows that were made visible by the shift in position
          RCTUIView *subview = self.contentView.subviews[ii]; // TODO(OSS Candidate ISS#2710739) use property instead of ivar for mac and TODO(macOS ISS#3536887)
          if ((horz ? subview.frame.origin.x >= self->_scrollView.contentOffset.x
                    : subview.frame.origin.y >= self->_scrollView.contentOffset.y) ||
              ii == self.contentView.subviews.count - 1) { // TODO(OSS Candidate ISS#2710739) use property instead of ivar for mac
            self->_prevFirstVisibleFrame = subview.frame;
            self->_firstVisibleView = subview;
            break;
          }
        }
      }];
  [manager addUIBlock:^(__unused RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, RCTUIView *> *viewRegistry) { // TODO(macOS ISS#3536887)
    if (self->_maintainVisibleContentPosition == nil) {
      return; // The prop might have changed in the previous UIBlocks, so need to abort here.
    }
    NSNumber *autoscrollThreshold = self->_maintainVisibleContentPosition[@"autoscrollToTopThreshold"];
    // TODO: detect and handle/ignore re-ordering
    if ([self isHorizontal:self->_scrollView]) {
      CGFloat deltaX = self->_firstVisibleView.frame.origin.x - self->_prevFirstVisibleFrame.origin.x;
      if (ABS(deltaX) > 0.1) {
        self->_scrollView.contentOffset =
            CGPointMake(self->_scrollView.contentOffset.x + deltaX, self->_scrollView.contentOffset.y);
        if (autoscrollThreshold != nil) {
          // If the offset WAS within the threshold of the start, animate to the start.
          if (self->_scrollView.contentOffset.x - deltaX <= [autoscrollThreshold integerValue]) {
            [self scrollToOffset:CGPointMake(0, self->_scrollView.contentOffset.y) animated:YES];
          }
        }
      }
    } else {
      CGRect newFrame = self->_firstVisibleView.frame;
      CGFloat deltaY = newFrame.origin.y - self->_prevFirstVisibleFrame.origin.y;
      if (ABS(deltaY) > 0.1) {
        self->_scrollView.contentOffset =
            CGPointMake(self->_scrollView.contentOffset.x, self->_scrollView.contentOffset.y + deltaY);
        if (autoscrollThreshold != nil) {
          // If the offset WAS within the threshold of the start, animate to the start.
          if (self->_scrollView.contentOffset.y - deltaY <= [autoscrollThreshold integerValue]) {
            [self scrollToOffset:CGPointMake(self->_scrollView.contentOffset.x, 0) animated:YES];
          }
        }
      }
    }
  }];
}

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)

- (NSString*)keyCommandFromKeyCode:(NSInteger)keyCode
{
  switch (keyCode)
  {
    case 36:
      return @"ENTER";
      
    case 116:
      return @"PAGE_UP";

    case 121:
      return @"PAGE_DOWN";
      
    case 123:
      return @"LEFT_ARROW";
      
    case 124:
      return @"RIGHT_ARROW";
      
    case 125:
      return @"DOWN_ARROW";
      
    case 126:
      return @"UP_ARROW";
  }
  return @"";
}

- (void)keyDown:(UIEvent*)theEvent
{
  // Don't emit a scroll event if tab was pressed while the scrollview is first responder
  if (self == [[self window] firstResponder] &&
      theEvent.keyCode != 48) {
    NSString *keyCommand = [self keyCommandFromKeyCode:theEvent.keyCode];
    RCT_SEND_SCROLL_EVENT(onScrollKeyDown, (@{ @"key": keyCommand}));
	} else {
    [super keyDown:theEvent];
		
    // AX: if a tab key was pressed and the first responder is currently clipped by the scroll view,
    // automatically scroll to make the view visible to make it navigable via keyboard.
    if ([theEvent keyCode] == 48) {  //tab key
      id firstResponder = [[self window] firstResponder];
      if ([firstResponder isKindOfClass:[NSView class]] &&
          [firstResponder isDescendantOf:[_scrollView documentView]]) {
        NSView *view = (NSView*)firstResponder;
        NSRect visibleRect = ([view superview] == [_scrollView documentView]) ? NSInsetRect(view.frame, -1, -1) :
                              [view convertRect:view.frame toView:_scrollView.documentView];
        [[_scrollView documentView] scrollRectToVisible:visibleRect];
      }
    }
  }
}

static NSString *RCTStringForScrollerStyle(NSScrollerStyle scrollerStyle) {
  switch (scrollerStyle) {
    case NSScrollerStyleLegacy:
      return @"legacy";
    case NSScrollerStyleOverlay:
      return @"overlay";
  }
}

- (void)preferredScrollerStyleDidChange:(__unused NSNotification *)notification {
  RCT_SEND_SCROLL_EVENT(onPreferredScrollerStyleDidChange, (@{ @"preferredScrollerStyle": RCTStringForScrollerStyle([NSScroller preferredScrollerStyle])}));
}
#endif // ]TODO(macOS ISS#2323203)

// Note: setting several properties of UIScrollView has the effect of
// resetting its contentOffset to {0, 0}. To prevent this, we generate
// setters here that will record the contentOffset beforehand, and
// restore it after the property has been set.

#define RCT_SET_AND_PRESERVE_OFFSET(setter, getter, type) \
  -(void)setter : (type)value                             \
  {                                                       \
    CGPoint contentOffset = _scrollView.contentOffset;    \
    [_scrollView setter:value];                           \
    _scrollView.contentOffset = contentOffset;            \
  }                                                       \
  -(type)getter                                           \
  {                                                       \
    return [_scrollView getter];                          \
  }

RCT_SET_AND_PRESERVE_OFFSET(setAlwaysBounceHorizontal, alwaysBounceHorizontal, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setAlwaysBounceVertical, alwaysBounceVertical, BOOL)
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
RCT_SET_AND_PRESERVE_OFFSET(setBounces, bounces, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setBouncesZoom, bouncesZoom, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setCanCancelContentTouches, canCancelContentTouches, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setDecelerationRate, decelerationRate, CGFloat)
RCT_SET_AND_PRESERVE_OFFSET(setDirectionalLockEnabled, isDirectionalLockEnabled, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setIndicatorStyle, indicatorStyle, UIScrollViewIndicatorStyle)
RCT_SET_AND_PRESERVE_OFFSET(setKeyboardDismissMode, keyboardDismissMode, UIScrollViewKeyboardDismissMode)
RCT_SET_AND_PRESERVE_OFFSET(setMaximumZoomScale, maximumZoomScale, CGFloat)
RCT_SET_AND_PRESERVE_OFFSET(setMinimumZoomScale, minimumZoomScale, CGFloat)
#endif // TODO(macOS ISS#2323203)
RCT_SET_AND_PRESERVE_OFFSET(setScrollEnabled, isScrollEnabled, BOOL)
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
#if !TARGET_OS_TV
RCT_SET_AND_PRESERVE_OFFSET(setPagingEnabled, isPagingEnabled, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setScrollsToTop, scrollsToTop, BOOL)
#endif
#endif // TODO(macOS ISS#2323203)
RCT_SET_AND_PRESERVE_OFFSET(setShowsHorizontalScrollIndicator, showsHorizontalScrollIndicator, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setShowsVerticalScrollIndicator, showsVerticalScrollIndicator, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setZoomScale, zoomScale, CGFloat);
RCT_SET_AND_PRESERVE_OFFSET(setScrollIndicatorInsets, scrollIndicatorInsets, UIEdgeInsets);

#pragma clang diagnostic push // TODO(OSS Candidate ISS#2710739)
#pragma clang diagnostic ignored "-Wunguarded-availability" // TODO(OSS Candidate ISS#2710739)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
- (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)behavior API_AVAILABLE(ios(11.0))
{
  // `contentInsetAdjustmentBehavior` is available since iOS 11.
  if ([_scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
    CGPoint contentOffset = _scrollView.contentOffset;
    if (@available(iOS 11.0, *)) {
      _scrollView.contentInsetAdjustmentBehavior = behavior;
    }
    _scrollView.contentOffset = contentOffset;
  }
}
#endif
#pragma clang diagnostic pop // TODO(OSS Candidate ISS#2710739)

- (void)sendScrollEventWithName:(NSString *)eventName
                     scrollView:(RCTCustomScrollView *)scrollView // TODO(macOS ISS#2323203)
                       userData:(NSDictionary *)userData
{
  if (![_lastEmittedEventName isEqualToString:eventName]) {
    _coalescingKey++;
    _lastEmittedEventName = [eventName copy];
  }
  RCTScrollEvent *scrollEvent = [[RCTScrollEvent alloc] initWithEventName:eventName
                                                                 reactTag:self.reactTag
                                                  scrollViewContentOffset:scrollView.contentOffset
                                                   scrollViewContentInset:scrollView.contentInset
                                                    scrollViewContentSize:scrollView.contentSize
                                                          scrollViewFrame:scrollView.frame
                                                      scrollViewZoomScale:scrollView.zoomScale
                                                                 userData:userData
                                                            coalescingKey:_coalescingKey];
  [_eventDispatcher sendEvent:scrollEvent];
}

@end

@implementation RCTEventDispatcher (RCTScrollView)

- (void)sendFakeScrollEvent:(NSNumber *)reactTag
{
  // Use the selector here in case the onScroll block property is ever renamed
  NSString *eventName = NSStringFromSelector(@selector(onScroll));
  RCTScrollEvent *fakeScrollEvent = [[RCTScrollEvent alloc] initWithEventName:eventName
                                                                     reactTag:reactTag
                                                      scrollViewContentOffset:CGPointZero
                                                       scrollViewContentInset:UIEdgeInsetsZero
                                                        scrollViewContentSize:CGSizeZero
                                                              scrollViewFrame:CGRectZero
                                                          scrollViewZoomScale:0
                                                                     userData:nil
                                                                coalescingKey:0];
  [self sendEvent:fakeScrollEvent];
}

@end
