"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSpotlightConfig = void 0;

var _spotlight = _interopRequireDefault(require("@enact/spotlight"));

var _react = require("react");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var useSpotlightConfig = function useSpotlightConfig(props, instances) {
  var scrollContentHandle = instances.scrollContentHandle; // Hooks

  (0, _react.useEffect)(function () {
    function handleLeaveContainer(_ref) {
      var direction = _ref.direction,
          target = _ref.target;

      // ensure we only scroll to boundary from the contents and not a scroll button which
      // lie outside of scrollContentRef but within the spotlight container
      if (props.scrollContainerContainsDangerously(target)) {
        var _scrollContentHandle$ = scrollContentHandle.current,
            _scrollContentHandle$2 = _scrollContentHandle$.scrollBounds,
            maxLeft = _scrollContentHandle$2.maxLeft,
            maxTop = _scrollContentHandle$2.maxTop,
            _scrollContentHandle$3 = _scrollContentHandle$.scrollPos,
            left = _scrollContentHandle$3.left,
            top = _scrollContentHandle$3.top,
            isVerticalDirection = direction === 'up' || direction === 'down',
            pos = isVerticalDirection ? top : left,
            max = isVerticalDirection ? maxTop : maxLeft; // If max is equal to 0, it means scroller can not scroll to the direction.

        if (pos >= 0 && pos <= max && max !== 0) {
          props.scrollAndFocusScrollbarButton(direction);
        }
      }
    }

    function configureSpotlight() {
      _spotlight["default"].set(props.spotlightId, {
        onLeaveContainer: handleLeaveContainer,
        onLeaveContainerFail: handleLeaveContainer
      });
    }

    configureSpotlight();
  }, [props, scrollContentHandle]);
};

exports.useSpotlightConfig = useSpotlightConfig;