"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var Progress = _interopRequireWildcard(require("react-native-progress"));
var _reactNativeSvg = require("react-native-svg");
var _file = require("../../../core/legacy/file");
var _xml = require("../../../core/assets/icons/xml");
var _styles = require("./styles");
var _reactNativeVideo = _interopRequireDefault(require("react-native-video"));
var _native = require("@react-navigation/native");
var _uiSlice = _interopRequireDefault(require("../../../core/stores/slices/uiSlice"));
var _reactNativeCompressor = require("react-native-compressor");
var _store = require("../../../core/stores/store");
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); }
const LoadingVideo = ({
  source,
  onClose,
  index,
  onLoadFinish,
  onUploadError,
  isUploaded = false,
  thumbNail,
  onPlay,
  fileId,
  isEditMode = false,
  fileCount,
  postId,
  setIsUploading
}) => {
  const dispatch = (0, _store.useUIKitDispatch)();
  const {
    showToastMessage
  } = _uiSlice.default.actions;
  const [loading, setLoading] = (0, _react.useState)(true);
  const [progress, setProgress] = (0, _react.useState)(0);
  const [isProcess, setIsProcess] = (0, _react.useState)(false);
  const [isUploadError, setIsUploadError] = (0, _react.useState)(false);
  const [thumbNailImage, setThumbNailImage] = (0, _react.useState)(thumbNail ?? '');
  const styles = (0, _styles.useStyles)();
  const [playingUri, setPlayingUri] = (0, _react.useState)('');
  const [isPause, setIsPause] = (0, _react.useState)(true);
  const navigation = (0, _native.useNavigation)();
  const playVideoFullScreen = fileUrl => {
    if (_reactNative.Platform.OS === 'ios') {
      setPlayingUri(fileUrl);
    } else {
      setIsPause(true);
      navigation.navigate('VideoPlayer', {
        source: source
      });
    }
  };
  const onClosePlayer = () => {
    setIsPause(true);
    setPlayingUri('');
  };
  const handleLoadEnd = () => {
    setLoading(false);
    setIsUploading(false);
  };
  const processThumbNail = async () => {
    const generatedThumbNail = await (0, _reactNativeCompressor.createVideoThumbnail)(source);
    setThumbNailImage(generatedThumbNail.path);
  };
  (0, _react.useEffect)(() => {
    processThumbNail();
  }, [thumbNail]);
  (0, _react.useEffect)(() => {
    if (progress === 100) {
      setIsProcess(true);
    }
  }, [progress]);
  const uploadFileToAmity = (0, _react.useCallback)(async () => {
    setIsUploading(true);
    setIsUploadError(false);
    try {
      const file = await (0, _file.uploadVideoFile)(source, percent => {
        setProgress(percent);
      });
      if (file) {
        var _file$, _file$2, _file$3;
        setIsProcess(false);
        handleLoadEnd();
        onLoadFinish && onLoadFinish((_file$ = file[0]) === null || _file$ === void 0 ? void 0 : _file$.fileId, (_file$2 = file[0]) === null || _file$2 === void 0 ? void 0 : _file$2.fileUrl, (_file$3 = file[0]) === null || _file$3 === void 0 ? void 0 : _file$3.attributes.name, index, source, thumbNail);
      } else {
        handleLoadEnd();
        dispatch(showToastMessage({
          toastMessage: 'Failed to upload file'
        }));
        setIsProcess(false);
        setIsUploadError(true);
        onUploadError === null || onUploadError === void 0 || onUploadError(true, source);
      }
    } catch (error) {
      handleLoadEnd();
      dispatch(showToastMessage({
        toastMessage: 'Failed to upload file'
      }));
      setIsProcess(false);
      setIsUploadError(true);
      onUploadError === null || onUploadError === void 0 || onUploadError(true, source);
    }
  }, [source]);
  const handleDelete = async () => {
    if (fileId && !isEditMode) {
      await (0, _file.deleteAmityFile)(fileId);
    }
    onClose && onClose(source, fileId, postId);
  };
  (0, _react.useEffect)(() => {
    setIsUploadError(false);
    onUploadError === null || onUploadError === void 0 || onUploadError(false, source);
    setProgress(0);
    setIsProcess(false);
    if (isUploaded) {
      setLoading(false);
    } else {
      uploadFileToAmity();
    }
  }, [fileId, isUploaded, source]);
  const handleOnPlay = () => {
    setIsPause(!isPause);
    playVideoFullScreen(source);
    onPlay && onPlay(source);
  };
  const onRetryUpload = () => {
    uploadFileToAmity();
  };
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: fileCount >= 3 ? styles.image3XContainer : styles.container
  }, !loading && !isUploadError && isPause && /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.playButton,
    onPress: handleOnPlay
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    xml: _xml.playBtn,
    width: "50",
    height: "50"
  })), playingUri && !isPause ? /*#__PURE__*/React.createElement(_reactNativeVideo.default, {
    controls: true,
    style: styles.image,
    source: {
      uri: playingUri
    },
    onFullscreenPlayerWillDismiss: onClosePlayer,
    paused: isPause
  }) : thumbNailImage ? /*#__PURE__*/React.createElement(_reactNative.Image, {
    resizeMode: "cover",
    source: {
      uri: thumbNailImage
    },
    style: [styles.image, loading ? styles.loadingImage : styles.loadedImage]
  }) : /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.image
  }), loading ? /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.overlay
  }, isProcess ? /*#__PURE__*/React.createElement(Progress.CircleSnail, {
    size: 24,
    borderColor: "transparent",
    thickness: 2
  }) : /*#__PURE__*/React.createElement(Progress.Circle, {
    progress: progress / 100,
    size: 24,
    borderColor: "transparent",
    unfilledColor: "#ffffff",
    thickness: 2
  })) : isUploadError ? /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.overlay,
    onPress: onRetryUpload
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    xml: (0, _xml.toastIcon)(),
    width: "24",
    height: "24"
  })) : /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.closeButton,
    disabled: (loading || isProcess) && !isUploadError,
    onPress: handleDelete
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    xml: (0, _xml.closeIcon)('white'),
    width: "12",
    height: "12"
  })));
};
var _default = exports.default = LoadingVideo;
//# sourceMappingURL=index.js.map