function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from "react";
import { StyleSheet, Animated, Platform } from "react-native";
import { HEADER_HEIGHT, useHeader } from "../Header/HeaderContext";
import TableCell from "./TableCell";
import TableRow from "./TableRow";
const DataTable = _ref => {
  let {
    children,
    style,
    renderItem,
    selectable,
    onSelect,
    ...rest
  } = _ref;
  const {
    options,
    setOptions
  } = useHeader();
  const [selected, setSelected] = React.useState([]);
  const flatListRef = React.useRef(null);
  React.useEffect(() => {
    setOptions(options => ({
      ...options,
      animate: true
    }));
  }, []);
  React.useEffect(() => {
    typeof onSelect === "function" && onSelect(selected);
  }, [selected]);
  React.useEffect(() => {
    if (!selectable && selected.length > 0) {
      setSelected([]);
    }
  }, [selectable]);
  const onScrollEndSnapToEdge = event => {
    let y = event.nativeEvent.contentOffset.y;
    console.log(y);
    y = Platform.OS === "ios" ? y + HEADER_HEIGHT : y;
    setTimeout(() => {
      if (0 < y && y < HEADER_HEIGHT / 2) {
        if (flatListRef.current) {
          var _flatListRef$current;
          (_flatListRef$current = flatListRef.current) === null || _flatListRef$current === void 0 ? void 0 : _flatListRef$current.scrollToOffset({
            offset: Platform.OS === "ios" ? -HEADER_HEIGHT : 0,
            animation: Platform.OS === "ios" ? true : false
          });
        }
      } else if (HEADER_HEIGHT / 2 <= y && y < HEADER_HEIGHT) {
        if (flatListRef.current) {
          var _flatListRef$current2;
          (_flatListRef$current2 = flatListRef.current) === null || _flatListRef$current2 === void 0 ? void 0 : _flatListRef$current2.scrollToOffset({
            offset: Platform.OS === "ios" ? 0 : HEADER_HEIGHT,
            animation: Platform.OS === "ios" ? true : false
          });
        }
      }
    }, Platform.OS === "ios" ? 16 : 0);
  };
  const decelerationRate = options.scrollY.interpolate({
    inputRange: [0, HEADER_HEIGHT],
    outputRange: [0, 0.98],
    extrapolate: "clamp"
  });
  return /*#__PURE__*/React.createElement(Animated.FlatList, _extends({
    decelerationRate: decelerationRate,
    ref: flatListRef,
    scrollEventThrottle: 16,
    contentContainerStyle: {
      paddingTop: Platform.OS === "android" ? HEADER_HEIGHT : 0
    },
    contentInset: {
      top: HEADER_HEIGHT
    },
    contentOffset: {
      y: Platform.OS === "ios" ? -HEADER_HEIGHT : 0,
      x: 0
    },
    onScrollBeginDrag: event => {
      Platform.OS === "ios" && options.scrollY.setOffset(HEADER_HEIGHT);
    },
    progressViewOffset: HEADER_HEIGHT,
    onScroll: Animated.event([{
      nativeEvent: {
        contentOffset: {
          y: options.scrollY
        }
      }
    }], {
      useNativeDriver: true
    }),
    style: [styles.container, style],
    scrollToOverflowEnabled: true,
    keyExtractor: (item, index) => index.toString(),
    onScrollEndDrag: onScrollEndSnapToEdge,
    onMomentumScrollEnd: e => Platform.OS === "android" && onScrollEndSnapToEdge(e),
    renderItem: data => {
      return /*#__PURE__*/React.cloneElement(
      //TODO do this only for Table.Row elements
      renderItem(data), selectable && {
        onSelect: selected => {
          selected //@ts-ignore callbacks are allowed with state hooks idk why it errors
          ? setSelected(prev => [...prev, {
            ...data.item,
            _index: data.index
          }]) : setSelected(prev => prev.filter(item => item._index !== data.index));
        }
      });
    }
  }, rest));
};

// @component ./TableRow.tsx
DataTable.Row = TableRow;

// @component ./TableRow.tsx
DataTable.Cell = TableCell;
const styles = StyleSheet.create({
  container: {
    width: "100%",
    paddingHorizontal: 8,
    paddingBottom: 56
  }
});
export default DataTable;
//# sourceMappingURL=DataTable.js.map