"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _throttle = _interopRequireDefault(require("lodash/throttle"));
var _scrollAreaLogic = require("./scrollAreaLogic");
var _extractAttributes = require("../../utils/extractAttributes");
var _excluded = ["dataHook", "className", "style", "children", "onScrollAreaChanged", "onScrollChanged", "scrollThrottleWait"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/common/ScrollableContainer/ScrollableContainer.jsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var ScrollableContainer = /*#__PURE__*/(0, _react.forwardRef)(function ScrollableContainer(_ref, ref) {
  var dataHook = _ref.dataHook,
    className = _ref.className,
    style = _ref.style,
    children = _ref.children,
    onScrollAreaChanged = _ref.onScrollAreaChanged,
    onScrollChanged = _ref.onScrollChanged,
    _ref$scrollThrottleWa = _ref.scrollThrottleWait,
    scrollThrottleWait = _ref$scrollThrottleWa === void 0 ? 100 : _ref$scrollThrottleWa,
    restProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
  var elementRef = (0, _react.useRef)(null);
  // In case a ref was passed from outside we should use it so allow the parent access to the dom node as well.
  var scrollContainerElement = ref || elementRef;
  var scrollAreaY = (0, _react.useRef)('');

  // TODO: fix ESLint error
  // eslint-disable-next-line react-hooks/exhaustive-deps
  var handleScrollAreaChanged = (0, _react.useCallback)((0, _throttle["default"])(function (_ref2) {
    var target = _ref2.target;
    var newScrollAreaY = (0, _scrollAreaLogic.getScrollAreaY)(target);
    if (scrollAreaY.current !== newScrollAreaY) {
      scrollAreaY.current = newScrollAreaY;
      onScrollAreaChanged({
        target: target,
        area: {
          y: newScrollAreaY
        }
      });
    }
  }, scrollThrottleWait, {
    trailing: true
  }), [onScrollAreaChanged, scrollThrottleWait]);

  // TODO: fix ESLint error
  // eslint-disable-next-line react-hooks/exhaustive-deps
  var handleScrollChanged = (0, _react.useCallback)((0, _throttle["default"])(function (e) {
    return onScrollChanged(e);
  }, scrollThrottleWait, {
    trailing: true
  }), [onScrollChanged, scrollThrottleWait]);
  (0, _react.useEffect)(function () {
    // Registering to the scroll event only if the relevant handlers were provided
    var scrollableElement = scrollContainerElement.current;
    if (onScrollAreaChanged) {
      scrollableElement.addEventListener('scroll', handleScrollAreaChanged);
      // We trigger a call to this handler to expose the initial scroll area to registering consumers
      handleScrollAreaChanged({
        target: scrollableElement
      });
    }
    if (onScrollChanged) {
      scrollableElement.addEventListener('scroll', handleScrollChanged);
    }
    return function () {
      // Unregistering from relevant events on component unmount
      if (onScrollAreaChanged) {
        handleScrollAreaChanged.cancel();
        scrollableElement.removeEventListener('scroll', handleScrollAreaChanged);
      }
      if (onScrollChanged) {
        handleScrollChanged.cancel();
        scrollableElement.removeEventListener('scroll', handleScrollChanged);
      }
    };
  }, [handleScrollChanged, handleScrollAreaChanged, onScrollChanged, onScrollAreaChanged, scrollContainerElement]);
  var stl = _objectSpread({
    height: 'inherit',
    width: 'inherit',
    overflowY: 'auto'
  }, style);
  return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
    "data-hook": dataHook,
    className: className,
    style: stl,
    ref: scrollContainerElement
  }, (0, _extractAttributes.extractDataAttributes)(restProps), {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 92,
      columnNumber: 5
    }
  }), children);
});
var _default = exports["default"] = ScrollableContainer;