/*
 * 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.
 */

package com.facebook.react.views.scroll;

import android.content.Context;
import androidx.core.view.ViewCompat;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.views.view.ReactViewGroup;

/** Container of Horizontal scrollViews that supports RTL scrolling. */
public class ReactHorizontalScrollContainerView extends ReactViewGroup {

  private int mLayoutDirection;
  private int mCurrentWidth;

  public ReactHorizontalScrollContainerView(Context context) {
    super(context);
    mLayoutDirection =
        I18nUtil.getInstance().isRTL(context)
            ? ViewCompat.LAYOUT_DIRECTION_RTL
            : ViewCompat.LAYOUT_DIRECTION_LTR;
    mCurrentWidth = 0;
  }

  @Override
  public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
    // Clipping doesn't work well for horizontal scroll views in RTL mode - in both
    // Fabric and non-Fabric - especially with TextInputs. The behavior you could see
    // is TextInputs being blurred immediately after being focused. So, for now,
    // it's easier to just disable this for these specific RTL views.
    // TODO T86027499: support `setRemoveClippedSubviews` in RTL mode
    if (mLayoutDirection == LAYOUT_DIRECTION_RTL) {
      super.setRemoveClippedSubviews(false);
      return;
    }

    super.setRemoveClippedSubviews(removeClippedSubviews);
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    /**
     * Note: in RTL mode, *when layout width changes*, we adjust the scroll position. Practically,
     * this means that on the first (meaningful) layout we will go from position 0 to position
     * (right - screenWidth). In theory this means if the width of the view ever changes during
     * layout again, scrolling could jump. Which shouldn't happen in theory, but... if you find a
     * weird product bug that looks related, keep this in mind.
     */
    if (mLayoutDirection == LAYOUT_DIRECTION_RTL) {
      // When the layout direction is RTL, we expect Yoga to give us a layout
      // that extends off the screen to the left so we re-center it with left=0
      int newLeft = 0;
      int width = right - left;
      int newRight = newLeft + width;
      setLeft(newLeft);
      setRight(newRight);

      // Call with the present values in order to re-layout if necessary
      ReactHorizontalScrollView parent = (ReactHorizontalScrollView) getParent();
      // Fix the ScrollX position when using RTL language
      int offsetX = parent.getScrollX() + getWidth() - mCurrentWidth;
      parent.reactScrollTo(offsetX, parent.getScrollY());
    }
    mCurrentWidth = getWidth();
  }
}

@@ -1637,24 +1637,380 @@
 t bottom) %7B%0A
+    if (mLayoutDirection == LAYOUT_DIRECTION_RTL) %7B%0A      // When the layout direction is RTL, we expect Yoga to give us a layout%0A      // that extends off the screen to the left so we re-center it with left=0%0A      int newLeft = 0;%0A      int width = right - left;%0A      int newRight = newLeft + width;%0A      setLeft(newLeft);%0A      setRight(newRight);%0A%0A  
     /**%0A    
@@ -2010,16 +2010,18 @@
 **%0A     
+  
 * Note: 
@@ -2111,16 +2111,18 @@
 y,%0A     
+  
 * this m
@@ -2203,16 +2203,18 @@
 osition%0A
+  
      * (
@@ -2306,16 +2306,18 @@
 ng%0A     
+  
 * layout
@@ -2406,16 +2406,18 @@
  a%0A     
+  
 * weird 
@@ -2467,16 +2467,18 @@
 n mind.%0A
+  
      */%0A
@@ -2485,358 +2485,47 @@
     
+  
 if (m
-LayoutDirection == LAYOUT_DIRECTION_RTL) %7B%0A      // When the layout direction is RTL, we expect Yoga to give us a layout%0A      // that extends off the screen to the left so we re-center it with left=0%0A      int newLeft = 0;%0A      int w
+CurrentW
 idth 
+!
 = 
-right - left;%0A      int newRight = newLeft + width;%0A      setLeft(newLeft);%0A      setRight(newRight);%0A%0A
+getWidth()) %7B%0A  
     
@@ -2599,16 +2599,18 @@
 y%0A      
+  
 ReactHor
@@ -2679,24 +2679,26 @@
 nt();%0A      
+  
 // Fix the S
@@ -2739,24 +2739,26 @@
 guage%0A      
+ 
+ 
 int offsetX 
@@ -2815,16 +2815,18 @@
 ;%0A      
+  
 parent.r
@@ -2869,16 +2869,24 @@
 llY());%0A
+      %7D%0A
     %7D%0A  

Fix RTL content jumping