content: package com.google.android.material.tabs;
  
  import androidx.recyclerview.widget.RecyclerView;
  import androidx.viewpager2.widget.ViewPager2;
  import com.google.android.material.tabs.TabLayout;
  import java.lang.ref.WeakReference;
  
  public final class TabLayoutMediator {
      private RecyclerView.Adapter<?> adapter;
      private boolean attached;
      private final boolean autoRefresh;
      private TabLayoutOnPageChangeCallback onPageChangeCallback;
      private TabLayout.OnTabSelectedListener onTabSelectedListener;
      private RecyclerView.AdapterDataObserver pagerAdapterObserver;
      private final boolean smoothScroll;
      private final TabConfigurationStrategy tabConfigurationStrategy;
      private final TabLayout tabLayout;
      private final ViewPager2 viewPager;
  
      private class PagerAdapterObserver extends RecyclerView.AdapterDataObserver {
          PagerAdapterObserver() {
          }
  
          public void onChanged() {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
  
          public void onItemRangeChanged(int positionStart, int itemCount) {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
  
          public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
  
          public void onItemRangeInserted(int positionStart, int itemCount) {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
  
          public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
  
          public void onItemRangeRemoved(int positionStart, int itemCount) {
              TabLayoutMediator.this.populateTabsFromPagerAdapter();
          }
      }
  
      public interface TabConfigurationStrategy {
          void onConfigureTab(TabLayout.Tab tab, int i);
      }
  
      private static class TabLayoutOnPageChangeCallback extends ViewPager2.OnPageChangeCallback {
          private int previousScrollState;
          private int scrollState;
          private final WeakReference<TabLayout> tabLayoutRef;
  
          TabLayoutOnPageChangeCallback(TabLayout tabLayout) {
              this.tabLayoutRef = new WeakReference<>(tabLayout);
              reset();
          }
  
          public void onPageScrollStateChanged(int state) {
              this.previousScrollState = this.scrollState;
              this.scrollState = state;
          }
  
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
              TabLayout tabLayout = (TabLayout) this.tabLayoutRef.get();
              if (tabLayout != null) {
                  int i = this.scrollState;
                  boolean z = false;
                  boolean z2 = i != 2 || this.previousScrollState == 1;
                  if (!(i == 2 && this.previousScrollState == 0)) {
                      z = true;
                  }
                  tabLayout.setScrollPosition(position, positionOffset, z2, z);
              }
          }
  
          public void onPageSelected(int position) {
              TabLayout tabLayout = (TabLayout) this.tabLayoutRef.get();
              if (tabLayout != null && tabLayout.getSelectedTabPosition() != position && position < tabLayout.getTabCount()) {
                  int i = this.scrollState;
                  tabLayout.selectTab(tabLayout.getTabAt(position), i == 0 || (i == 2 && this.previousScrollState == 0));
              }
          }
  
          /* access modifiers changed from: package-private */
          public void reset() {
              this.scrollState = 0;
              this.previousScrollState = 0;
          }
      }
  
      private static class ViewPagerOnTabSelectedListener implements TabLayout.OnTabSelectedListener {
          private final boolean smoothScroll;
          private final ViewPager2 viewPager;
  
          ViewPagerOnTabSelectedListener(ViewPager2 viewPager2, boolean smoothScroll2) {
              this.viewPager = viewPager2;
              this.smoothScroll = smoothScroll2;
          }
  
          public void onTabReselected(TabLayout.Tab tab) {
          }
  
          public void onTabSelected(TabLayout.Tab tab) {
              this.viewPager.setCurrentItem(tab.getPosition(), this.smoothScroll);
          }
  
          public void onTabUnselected(TabLayout.Tab tab) {
          }
      }
  
      public TabLayoutMediator(TabLayout tabLayout2, ViewPager2 viewPager2, TabConfigurationStrategy tabConfigurationStrategy2) {
          this(tabLayout2, viewPager2, true, tabConfigurationStrategy2);
      }
  
      public TabLayoutMediator(TabLayout tabLayout2, ViewPager2 viewPager2, boolean autoRefresh2, TabConfigurationStrategy tabConfigurationStrategy2) {
          this(tabLayout2, viewPager2, autoRefresh2, true, tabConfigurationStrategy2);
      }
  
      public TabLayoutMediator(TabLayout tabLayout2, ViewPager2 viewPager2, boolean autoRefresh2, boolean smoothScroll2, TabConfigurationStrategy tabConfigurationStrategy2) {
          this.tabLayout = tabLayout2;
          this.viewPager = viewPager2;
          this.autoRefresh = autoRefresh2;
          this.smoothScroll = smoothScroll2;
          this.tabConfigurationStrategy = tabConfigurationStrategy2;
      }
  
      public void attach() {
          if (!this.attached) {
              RecyclerView.Adapter<?> adapter2 = this.viewPager.getAdapter();
              this.adapter = adapter2;
              if (adapter2 != null) {
                  this.attached = true;
                  TabLayoutOnPageChangeCallback tabLayoutOnPageChangeCallback = new TabLayoutOnPageChangeCallback(this.tabLayout);
                  this.onPageChangeCallback = tabLayoutOnPageChangeCallback;
                  this.viewPager.registerOnPageChangeCallback(tabLayoutOnPageChangeCallback);
                  ViewPagerOnTabSelectedListener viewPagerOnTabSelectedListener = new ViewPagerOnTabSelectedListener(this.viewPager, this.smoothScroll);
                  this.onTabSelectedListener = viewPagerOnTabSelectedListener;
                  this.tabLayout.addOnTabSelectedListener((TabLayout.OnTabSelectedListener) viewPagerOnTabSelectedListener);
                  if (this.autoRefresh) {
                      PagerAdapterObserver pagerAdapterObserver2 = new PagerAdapterObserver();
                      this.pagerAdapterObserver = pagerAdapterObserver2;
                      this.adapter.registerAdapterDataObserver(pagerAdapterObserver2);
                  }
                  populateTabsFromPagerAdapter();
                  this.tabLayout.setScrollPosition(this.viewPager.getCurrentItem(), 0.0f, true);
                  return;
              }
              throw new IllegalStateException("TabLayoutMediator attached before ViewPager2 has an adapter");
          }
          throw new IllegalStateException("TabLayoutMediator is already attached");
      }
  
      public void detach() {
          RecyclerView.Adapter<?> adapter2;
          if (this.autoRefresh && (adapter2 = this.adapter) != null) {
              adapter2.unregisterAdapterDataObserver(this.pagerAdapterObserver);
              this.pagerAdapterObserver = null;
          }
          this.tabLayout.removeOnTabSelectedListener(this.onTabSelectedListener);
          this.viewPager.unregisterOnPageChangeCallback(this.onPageChangeCallback);
          this.onTabSelectedListener = null;
          this.onPageChangeCallback = null;
          this.adapter = null;
          this.attached = false;
      }
  
      public boolean isAttached() {
          return this.attached;
      }
  
      /* access modifiers changed from: package-private */
      public void populateTabsFromPagerAdapter() {
          int min;
          this.tabLayout.removeAllTabs();
          RecyclerView.Adapter<?> adapter2 = this.adapter;
          if (adapter2 != null) {
              int itemCount = adapter2.getItemCount();
              for (int i = 0; i < itemCount; i++) {
                  TabLayout.Tab newTab = this.tabLayout.newTab();
                  this.tabConfigurationStrategy.onConfigureTab(newTab, i);
                  this.tabLayout.addTab(newTab, false);
              }
              if (itemCount > 0 && (min = Math.min(this.viewPager.getCurrentItem(), this.tabLayout.getTabCount() - 1)) != this.tabLayout.getSelectedTabPosition()) {
                  TabLayout tabLayout2 = this.tabLayout;
                  tabLayout2.selectTab(tabLayout2.getTabAt(min));
              }
          }
      }
  }
  