"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.FloatingScrollBar = FloatingScrollBar;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _reactScrollSync = require("react-scroll-sync");
var _FloatingScrollBarSt = require("./FloatingScrollBar.st.css.js");
var _FloatingScrollBarContext = require("./FloatingScrollBarContext");
var _useFloatingScrollBarState = require("./useFloatingScrollBarState");
var _PageScrollContext = require("../../Page/PageScrollContext");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Table/FloatingScrollBar/FloatingScrollBar.tsx"; // @ts-ignore
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; }
function FloatingScrollBarInner(props) {
  var {
    dataHook,
    viewportElement,
    scrollBarRef,
    zIndex,
    scrollContentSize,
    floatingPosition,
    scrollContentPosition
  } = props;
  var [height, setHeight] = (0, _react.useState)(0);
  (0, _react.useEffect)(() => {
    var scrollBarElement = scrollBarRef.current;
    if (!scrollBarElement) {
      return;
    }
    setHeight(scrollBarElement.getBoundingClientRect().height);
  }, [scrollBarRef]);
  return /*#__PURE__*/(0, _reactDom.createPortal)(/*#__PURE__*/_react.default.createElement("div", {
    "data-hook": dataHook,
    className: (0, _FloatingScrollBarSt.st)(_FloatingScrollBarSt.classes.root),
    ref: scrollBarRef,
    style: _objectSpread(_objectSpread({}, scrollContentPosition), {}, {
      zIndex
    }, floatingPosition ? {
      top: floatingPosition.top - height
    } : {
      visibility: 'hidden'
    }),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 46,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-hook": "floating-scroll-bar-content",
    className: (0, _FloatingScrollBarSt.st)(_FloatingScrollBarSt.classes.content),
    style: scrollContentSize,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 58,
      columnNumber: 7
    }
  })), viewportElement);
}
function FloatingScrollBar(props) {
  var {
    viewportRef
  } = (0, _FloatingScrollBarContext.useFloatingScrollBarContext)();
  var {
    scrollableContentRef: pageScrollableContentRef
  } = (0, _PageScrollContext.usePageScrollContext)();
  var [viewportElement, setViewportElement] = (0, _react.useState)(null);
  var scrollBarRef = (0, _react.useRef)(null);
  var {
    floatingScrollBarStateRef
  } = (0, _FloatingScrollBarContext.useFloatingScrollBarContext)();
  var {
    scrollContentPosition,
    scrollContentSize,
    floatingPosition,
    state
  } = (0, _useFloatingScrollBarState.useFloatingScrollBarState)();
  (0, _react.useEffect)(() => {
    floatingScrollBarStateRef.current = state;
    return () => {
      floatingScrollBarStateRef.current = null;
    };
  }, [floatingScrollBarStateRef, state]);
  (0, _react.useEffect)(() => {
    var _pageScrollableConten;
    var pageElement = pageScrollableContentRef == null || (_pageScrollableConten = pageScrollableContentRef.current) == null ? void 0 : _pageScrollableConten.parentElement;
    if (pageElement) {
      setViewportElement(pageElement);
    } else {
      var _viewportRef$current;
      setViewportElement((_viewportRef$current = viewportRef == null ? void 0 : viewportRef.current) !== null && _viewportRef$current !== void 0 ? _viewportRef$current : null);
    }
  }, [viewportRef, pageScrollableContentRef]);
  if (!viewportElement) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_reactScrollSync.ScrollSyncPane, {
    attachTo: scrollBarRef,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 103,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(FloatingScrollBarInner, (0, _extends2.default)({}, props, {
    viewportElement: viewportElement,
    scrollBarRef: scrollBarRef,
    scrollContentPosition: scrollContentPosition,
    scrollContentSize: scrollContentSize,
    floatingPosition: floatingPosition,
    state: state,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 104,
      columnNumber: 7
    }
  })));
}
//# sourceMappingURL=FloatingScrollBar.js.map