"use strict";

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

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

var _utilDOM = _interopRequireDefault(require("@enact/ui/useScroll/utilDOM"));

var _react = require("react");

var _SharedStateDecorator = require("../Panels/SharedStateDecorator");

var _ScrollbarModule = _interopRequireDefault(require("./Scrollbar.module.css"));

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

var navigableFilter = function navigableFilter(elem) {
  if (!_spotlight["default"].getPointerMode() && // ignore containers passed as their id
  typeof elem !== 'string' && _utilDOM["default"].containsDangerously(elem.classList, _ScrollbarModule["default"].scrollButton)) {
    return false;
  }
};

var useSpotlightConfig = function useSpotlightConfig(props) {
  // Hooks
  (0, _react.useLayoutEffect)(function () {
    function configureSpotlight() {
      _spotlight["default"].set(props['data-spotlight-id'], {
        navigableFilter: props.focusableScrollbar ? null : navigableFilter
      });
    }

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

exports.useSpotlightConfig = useSpotlightConfig;

var useSpotlightRestore = function useSpotlightRestore(props, instances) {
  var scrollContainerHandle = instances.scrollContainerHandle;
  var context = (0, _react.useContext)(_SharedStateDecorator.SharedState); // Hooks

  (0, _react.useEffect)(function () {
    // Only intended to be used within componentDidMount, this method will fetch the last stored
    // scroll position from SharedState and scroll (without animation) to that position
    function restoreScrollPosition() {
      var id = props.id;

      if (id && context && context.get) {
        var scrollPosition = context.get("".concat(id, ".scrollPosition"));

        if (scrollPosition) {
          scrollContainerHandle.current.scrollTo({
            position: scrollPosition,
            animate: false
          });
        }
      }
    }

    restoreScrollPosition();
  }, []); // eslint-disable-line react-hooks/exhaustive-deps
};

exports.useSpotlightRestore = useSpotlightRestore;