"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useScrollbar = exports["default"] = void 0;

var _useScroll = require("@enact/ui/useScroll");

var paginationPageMultiplier = _useScroll.constants.paginationPageMultiplier;

var useScrollbar = function useScrollbar(props, instances) {
  var horizontalScrollbarHandle = instances.horizontalScrollbarHandle,
      scrollContainerHandle = instances.scrollContainerHandle,
      verticalScrollbarHandle = instances.verticalScrollbarHandle;
  var scrollbarProps = {
    cbAlertScrollbarTrack: alertScrollbarTrack,
    onNextScroll: onScrollbarButtonClick,
    onPrevScroll: onScrollbarButtonClick
  }; // Functions

  function isScrollButtonFocused() {
    return horizontalScrollbarHandle.current && horizontalScrollbarHandle.current.isOneOfScrollButtonsFocused() || verticalScrollbarHandle.current && verticalScrollbarHandle.current.isOneOfScrollButtonsFocused();
  }

  function onScrollbarButtonClick(_ref) {
    var isPreviousScrollButton = _ref.isPreviousScrollButton,
        isVerticalScrollBar = _ref.isVerticalScrollBar;

    if (scrollContainerHandle.current) {
      var wheelDirection = scrollContainerHandle.current.wheelDirection,
          bounds = scrollContainerHandle.current.getScrollBounds(),
          direction = isPreviousScrollButton ? -1 : 1,
          pageDistance = direction * (isVerticalScrollBar ? bounds.clientHeight : bounds.clientWidth) * paginationPageMultiplier;
      scrollContainerHandle.current.lastInputType = 'scrollbarButton';

      if (direction !== wheelDirection) {
        scrollContainerHandle.current.isScrollAnimationTargetAccumulated = false;
        scrollContainerHandle.current.wheelDirection = direction;
      }

      scrollContainerHandle.current.scrollToAccumulatedTarget(pageDistance, isVerticalScrollBar);
    }
  }

  function focusOnScrollButton(scrollbarHandle, isPreviousScrollButton) {
    if (scrollbarHandle.current) {
      scrollbarHandle.current.focusOnButton(isPreviousScrollButton);
    }
  }

  function scrollAndFocusScrollbarButton(direction) {
    if (scrollContainerHandle.current) {
      var rtl = scrollContainerHandle.current.rtl,
          isPreviousScrollButton = direction === 'up' || (rtl ? direction === 'right' : direction === 'left'),
          isHorizontalDirection = direction === 'left' || direction === 'right',
          isVerticalDirection = direction === 'up' || direction === 'down',
          canScrollHorizontally = isHorizontalDirection && (props.direction === 'horizontal' || props.direction === 'both'),
          canScrollingVertically = isVerticalDirection && (props.direction === 'vertical' || props.direction === 'both');

      if (canScrollHorizontally || canScrollingVertically) {
        onScrollbarButtonClick({
          isPreviousScrollButton: isPreviousScrollButton,
          isVerticalScrollBar: canScrollingVertically
        });

        if (props.focusableScrollbar) {
          focusOnScrollButton(canScrollingVertically ? verticalScrollbarHandle : horizontalScrollbarHandle, isPreviousScrollButton);
        }
      }
    }
  }

  function alertScrollbarTrack() {
    if (scrollContainerHandle.current) {
      var bounds = scrollContainerHandle.current.getScrollBounds();
      scrollContainerHandle.current.showScrollbarTrack(bounds);
      scrollContainerHandle.current.startHidingScrollbarTrack();
    }
  } // Return


  return {
    alertScrollbarTrack: alertScrollbarTrack,
    isScrollButtonFocused: isScrollButtonFocused,
    scrollAndFocusScrollbarButton: scrollAndFocusScrollbarButton,
    scrollbarProps: scrollbarProps
  };
};

exports.useScrollbar = useScrollbar;
var _default = useScrollbar;
exports["default"] = _default;