"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ScrollInto = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _color = require("./color");
var _configs = _interopRequireDefault(require("./configs"));
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; }
const CONTAINER = {
  flex: 1
};
/**
 * Describe your component here
 */
const ScrollInto = function ScrollInto(props) {
  const {
    style,
    dataLengthView,
    dataHeader,
    children
  } = props;
  const styles = Object.assign({}, CONTAINER, style);
  const ref = (0, _react.useRef)(null);
  const refHead = (0, _react.useRef)(null);
  const [indexCurrent, setIndexCurrent] = (0, _react.useState)(0);

  //scroll đến vị trí của title
  const scrollToIndex = indexCurrent => {
    setIndexCurrent(indexCurrent);
    refHead.current.scrollToIndex({
      animated: true,
      index: indexCurrent,
      viewPosition: 0.5
    });
  };
  const getItemLayout = (data, index) => {
    if (data) {}
    return {
      length: _configs.default.windowWidth / 3.5 - 20,
      offset: _configs.default.windowWidth / 3.5 * index,
      index
    };
  };

  // xử lý scroll đến từng vị trí
  const scrollHandler = index => {
    if (dataLengthView.length >= index) {
      ref.current.scrollTo({
        x: 0,
        y: dataLengthView[index - 1],
        animated: true
      });
    }
  };

  // xử lý scroll
  const handleScroll = event => {
    // console.log("event", event.nativeEvent.contentOffset.y)
    //tìm vị trí trùng
    const dtIndex = dataLengthView.findIndex(e => e <= event.nativeEvent.contentOffset.y);
    // console.log("data index ", dtIndex)
    // nếu vị trí bằng 0 thì thực hiện câu điều kiện if để scroll theo từng title, còn không bằngg thì
    if (dtIndex == 0) {
      for (let i = 0; i < dataLengthView.length; i++) {
        if (dataLengthView[i] <= event.nativeEvent.contentOffset.y + 20) {
          scrollToIndex(i);
        }
      }
    }
    //console.log("indesx", indexCurrent)
  };

  //console.log("indexcurrent", indexCurrent)

  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    ref: refHead,
    data: dataHeader,
    keyExtractor: (_, index) => `${index}`,
    horizontal: true
    // lâý ra kích thước của  item
    ,
    getItemLayout: getItemLayout,
    showsHorizontalScrollIndicator: false,
    renderItem: _ref => {
      let {
        item,
        index
      } = _ref;
      return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
        style: indexCurrent != index ? BTN_TYPE : [BTN_TYPE, {
          borderBottomWidth: 1,
          borderColor: _color.color.mainColor
        }],
        onPress: () => {
          scrollToIndex(index);
          scrollHandler(index + 1);
        }
      }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
        style: indexCurrent == index && {
          color: _color.color.mainColor
        }
      }, item.tile));
    }
  })), /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, {
    ref: ref
    // scroll
    ,
    onScroll: handleScroll,
    decelerationRate: 0.2,
    scrollEventThrottle: 16
  }, children, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      height: 100
    }
  })));
};
exports.ScrollInto = ScrollInto;
const BTN_TYPE = {
  marginHorizontal: 16,
  paddingVertical: 10
};
//# sourceMappingURL=scrollInto.js.map