"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(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, 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.
 *
 */
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;
const ImageItem = ({
  imageSrc,
  onZoom,
  onRequestClose,
  onLongPress,
  delayLongPress,
  swipeToCloseEnabled = true,
  doubleTapToZoomEnabled = true
}) => {
  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 => {
    var _a;
    onZoom(isZoomed);
    if ((_a = imageContainer) === null || _a === void 0 ? void 0 : _a.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 = ({
    nativeEvent
  }) => {
    var _a, _b, _c, _d, _e, _f;
    const velocityY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.velocity) === null || _b === void 0 ? void 0 : _b.y, _c !== null && _c !== void 0 ? _c : 0);
    const offsetY = (_f = (_e = (_d = nativeEvent) === null || _d === void 0 ? void 0 : _d.contentOffset) === null || _e === void 0 ? void 0 : _e.y, _f !== null && _f !== void 0 ? _f : 0);
    if (Math.abs(velocityY) > SWIPE_CLOSE_VELOCITY && offsetY > SWIPE_CLOSE_OFFSET || offsetY > SCREEN_HEIGHT / 2) {
      onRequestClose();
    }
  };
  const onScroll = ({
    nativeEvent
  }) => {
    var _a, _b, _c;
    const offsetY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.contentOffset) === null || _b === void 0 ? void 0 : _b.y, _c !== null && _c !== void 0 ? _c : 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 = exports.default = /*#__PURE__*/_react.default.memo(ImageItem);
//# sourceMappingURL=ImageItem.android.js.map