"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useImageDimensions = _interopRequireDefault(require("../../hooks/useImageDimensions"));
var _usePanResponder = _interopRequireDefault(require("../../hooks/usePanResponder"));
var _utils = require("../../utils");
var _ImageLoading = require("./ImageLoading");
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); } /**
                                                                                                                                                                                                                                                                                                                                                     * Copyright (c) JOB TODAY S.A. and its affiliates.
                                                                                                                                                                                                                                                                                                                                                     *
                                                                                                                                                                                                                                                                                                                                                     * This source code is licensed under the MIT license found in the
                                                                                                                                                                                                                                                                                                                                                     * LICENSE file in the root directory of this source tree.
                                                                                                                                                                                                                                                                                                                                                     *
                                                                                                                                                                                                                                                                                                                                                     */ // @ts-nocheck
const SWIPE_CLOSE_OFFSET = 75;
const SWIPE_CLOSE_VELOCITY = 1.75;
const SCREEN = _reactNative.Dimensions.get("window");
const SCREEN_WIDTH = SCREEN.width;
const SCREEN_HEIGHT = SCREEN.height;

// @ts-ignore
const ImageItem = _ref => {
  let {
    imageSrc,
    onZoom,
    onRequestClose,
    onLongPress,
    delayLongPress,
    swipeToCloseEnabled = true,
    doubleTapToZoomEnabled = true
  } = _ref;
  const imageContainer = (0, _react.useRef)(null);
  const imageDimensions = (0, _useImageDimensions.default)(imageSrc);
  const [translate, scale] = (0, _utils.getImageTransform)(imageDimensions, SCREEN);
  const scrollValueY = new _reactNative.Animated.Value(0);
  const [isLoaded, setLoadEnd] = (0, _react.useState)(false);
  const onLoaded = (0, _react.useCallback)(() => setLoadEnd(true), []);
  const onZoomPerformed = (0, _react.useCallback)(isZoomed => {
    onZoom(isZoomed);
    if (imageContainer !== null && imageContainer !== void 0 && imageContainer.current) {
      imageContainer.current.setNativeProps({
        scrollEnabled: !isZoomed
      });
    }
  }, [imageContainer]);
  const onLongPressHandler = (0, _react.useCallback)(() => {
    onLongPress(imageSrc);
  }, [imageSrc, onLongPress]);
  const [panHandlers, scaleValue, translateValue] = (0, _usePanResponder.default)({
    initialScale: scale || 1,
    initialTranslate: translate || {
      x: 0,
      y: 0
    },
    onZoom: onZoomPerformed,
    doubleTapToZoomEnabled,
    onLongPress: onLongPressHandler,
    delayLongPress
  });
  const imagesStyles = (0, _utils.getImageStyles)(imageDimensions, translateValue, scaleValue);
  const imageOpacity = scrollValueY.interpolate({
    inputRange: [-SWIPE_CLOSE_OFFSET, 0, SWIPE_CLOSE_OFFSET],
    outputRange: [0.7, 1, 0.7]
  });
  const imageStylesWithOpacity = {
    ...imagesStyles,
    opacity: imageOpacity
  };
  const onScrollEndDrag = _ref2 => {
    var _nativeEvent$velocity, _nativeEvent$velocity2, _nativeEvent$contentO, _nativeEvent$contentO2;
    let {
      nativeEvent
    } = _ref2;
    const velocityY = (_nativeEvent$velocity = nativeEvent === null || nativeEvent === void 0 ? void 0 : (_nativeEvent$velocity2 = nativeEvent.velocity) === null || _nativeEvent$velocity2 === void 0 ? void 0 : _nativeEvent$velocity2.y) !== null && _nativeEvent$velocity !== void 0 ? _nativeEvent$velocity : 0;
    const offsetY = (_nativeEvent$contentO = nativeEvent === null || nativeEvent === void 0 ? void 0 : (_nativeEvent$contentO2 = nativeEvent.contentOffset) === null || _nativeEvent$contentO2 === void 0 ? void 0 : _nativeEvent$contentO2.y) !== null && _nativeEvent$contentO !== void 0 ? _nativeEvent$contentO : 0;
    if (Math.abs(velocityY) > SWIPE_CLOSE_VELOCITY && offsetY > SWIPE_CLOSE_OFFSET || offsetY > SCREEN_HEIGHT / 2) {
      onRequestClose();
    }
  };
  const onScroll = _ref3 => {
    var _nativeEvent$contentO3, _nativeEvent$contentO4;
    let {
      nativeEvent
    } = _ref3;
    const offsetY = (_nativeEvent$contentO3 = nativeEvent === null || nativeEvent === void 0 ? void 0 : (_nativeEvent$contentO4 = nativeEvent.contentOffset) === null || _nativeEvent$contentO4 === void 0 ? void 0 : _nativeEvent$contentO4.y) !== null && _nativeEvent$contentO3 !== void 0 ? _nativeEvent$contentO3 : 0;
    scrollValueY.setValue(offsetY);
  };
  return /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, _extends({
    ref: imageContainer,
    style: styles.listItem,
    pagingEnabled: true,
    nestedScrollEnabled: true,
    showsHorizontalScrollIndicator: false,
    showsVerticalScrollIndicator: false,
    contentContainerStyle: styles.imageScrollContainer,
    scrollEnabled: swipeToCloseEnabled
  }, swipeToCloseEnabled && {
    onScroll,
    onScrollEndDrag
  }), /*#__PURE__*/_react.default.createElement(_reactNative.Animated.Image, _extends({}, panHandlers, {
    source: imageSrc,
    style: imageStylesWithOpacity,
    onLoad: onLoaded
  })), (!isLoaded || !imageDimensions) && /*#__PURE__*/_react.default.createElement(_ImageLoading.ImageLoading, null));
};
const styles = _reactNative.StyleSheet.create({
  listItem: {
    width: SCREEN_WIDTH,
    height: SCREEN_HEIGHT
  },
  imageScrollContainer: {
    height: SCREEN_HEIGHT * 2
  }
});
var _default = /*#__PURE__*/_react.default.memo(ImageItem);
exports.default = _default;
//# sourceMappingURL=ImageItem.android.js.map