"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _HeaderContext = require("../Header/HeaderContext");
var _TableCell = _interopRequireDefault(require("./TableCell"));
var _TableRow = _interopRequireDefault(require("./TableRow"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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); }
const DataTable = _ref => {
  let {
    children,
    style,
    renderItem,
    selectable,
    onSelect,
    ...rest
  } = _ref;
  const {
    options,
    setOptions
  } = (0, _HeaderContext.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 = _reactNative.Platform.OS === "ios" ? y + _HeaderContext.HEADER_HEIGHT : y;
    setTimeout(() => {
      if (0 < y && y < _HeaderContext.HEADER_HEIGHT / 2) {
        if (flatListRef.current) {
          var _flatListRef$current;
          (_flatListRef$current = flatListRef.current) === null || _flatListRef$current === void 0 ? void 0 : _flatListRef$current.scrollToOffset({
            offset: _reactNative.Platform.OS === "ios" ? -_HeaderContext.HEADER_HEIGHT : 0,
            animation: _reactNative.Platform.OS === "ios" ? true : false
          });
        }
      } else if (_HeaderContext.HEADER_HEIGHT / 2 <= y && y < _HeaderContext.HEADER_HEIGHT) {
        if (flatListRef.current) {
          var _flatListRef$current2;
          (_flatListRef$current2 = flatListRef.current) === null || _flatListRef$current2 === void 0 ? void 0 : _flatListRef$current2.scrollToOffset({
            offset: _reactNative.Platform.OS === "ios" ? 0 : _HeaderContext.HEADER_HEIGHT,
            animation: _reactNative.Platform.OS === "ios" ? true : false
          });
        }
      }
    }, _reactNative.Platform.OS === "ios" ? 16 : 0);
  };
  const decelerationRate = options.scrollY.interpolate({
    inputRange: [0, _HeaderContext.HEADER_HEIGHT],
    outputRange: [0, 0.98],
    extrapolate: "clamp"
  });
  return /*#__PURE__*/React.createElement(_reactNative.Animated.FlatList, _extends({
    decelerationRate: decelerationRate,
    ref: flatListRef,
    scrollEventThrottle: 16,
    contentContainerStyle: {
      paddingTop: _reactNative.Platform.OS === "android" ? _HeaderContext.HEADER_HEIGHT : 0
    },
    contentInset: {
      top: _HeaderContext.HEADER_HEIGHT
    },
    contentOffset: {
      y: _reactNative.Platform.OS === "ios" ? -_HeaderContext.HEADER_HEIGHT : 0,
      x: 0
    },
    onScrollBeginDrag: event => {
      _reactNative.Platform.OS === "ios" && options.scrollY.setOffset(_HeaderContext.HEADER_HEIGHT);
    },
    progressViewOffset: _HeaderContext.HEADER_HEIGHT,
    onScroll: _reactNative.Animated.event([{
      nativeEvent: {
        contentOffset: {
          y: options.scrollY
        }
      }
    }], {
      useNativeDriver: true
    }),
    style: [styles.container, style],
    scrollToOverflowEnabled: true,
    keyExtractor: (item, index) => index.toString(),
    onScrollEndDrag: onScrollEndSnapToEdge,
    onMomentumScrollEnd: e => _reactNative.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.default;

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