"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useScrollableInternal = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeReanimated = require("react-native-reanimated");
var _reactNativeRedash = require("react-native-redash");
var _useBottomSheetInternal = require("./useBottomSheetInternal");
// @ts-nocheck

const useScrollableInternal = type => {
  // refs
  const scrollableContentHeightRef = (0, _react.useRef)(0);
  const scrollableContentOffsetYRef = (0, _react.useRef)(0);
  const scrollableContentOffsetY = (0, _reactNativeRedash.useValue)(0);
  const scrollableRef = (0, _react.useRef)(null);

  // hooks
  const {
    scrollableContentOffsetY: _scrollableContentOffsetY,
    setScrollableRef,
    removeScrollableRef
  } = (0, _useBottomSheetInternal.useBottomSheetInternal)();

  // callbacks
  /**
   * Reset the scrollable offset y when its size get smaller,
   * this fixes #286.
   */
  const handleOnContentSizeChange = (0, _react.useCallback)((_, height) => {
    if (scrollableContentHeightRef.current > height) {
      scrollableContentOffsetY.setValue(0);
    }
    scrollableContentHeightRef.current = height;
  }, [scrollableContentOffsetY]);
  const handleOnBeginDragEvent = (0, _react.useMemo)(() => (0, _reactNativeReanimated.event)([{
    nativeEvent: {
      contentOffset: {
        y: scrollableContentOffsetY
      }
    }
  }]), [scrollableContentOffsetY]);
  const handleSettingScrollable = (0, _react.useCallback)(() => {
    _scrollableContentOffsetY.setValue(scrollableContentOffsetYRef.current);
    const id = (0, _reactNative.findNodeHandle)(scrollableRef.current);
    if (id) {
      setScrollableRef({
        id: id,
        type,
        // @ts-ignore
        node: scrollableRef.current.getNode()
      });
    } else {
      console.warn(`Couldn't find the scrollable node handle id!`);
    }
    return () => {
      removeScrollableRef(scrollableRef);
    };
  }, [type, _scrollableContentOffsetY, removeScrollableRef, setScrollableRef]);

  // effects
  (0, _reactNativeReanimated.useCode)(() => (0, _reactNativeReanimated.onChange)(scrollableContentOffsetY, [(0, _reactNativeReanimated.set)(_scrollableContentOffsetY, scrollableContentOffsetY), (0, _reactNativeReanimated.call)([scrollableContentOffsetY], args => {
    scrollableContentOffsetYRef.current = args[0];
  })]), []);
  return {
    scrollableRef,
    handleOnBeginDragEvent,
    handleOnContentSizeChange,
    handleSettingScrollable
  };
};
exports.useScrollableInternal = useScrollableInternal;
//# sourceMappingURL=useScrollableInternal.js.map