"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUpload = useUpload;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _reactNative = require("react-native");
var _reactQuery = require("@tanstack/react-query");
var _constants = require("../constants");
function useUpload() {
  const {
    mutateAsync,
    isPending
  } = (0, _reactQuery.useMutation)({
    mutationFn: ({
      file,
      onProgress,
      altText
    }) => _tsSdkReactNative.FileRepository.uploadImage(file, onProgress, altText)
  });
  const uploadImage = async ({
    file,
    onProgress,
    altText
  }) => {
    const formData = new FormData();
    const parts = file.split('/');
    const fileName = parts[parts.length - 1];
    const fileType = _reactNative.Platform.OS === 'ios' ? 'image/jpeg' : 'image/jpg';
    const uri = _reactNative.Platform.OS === 'android' ? file : file.replace('file://', '');
    formData.append('files', {
      name: fileName,
      type: fileType,
      uri: uri
    });
    return await mutateAsync({
      file: formData,
      onProgress,
      altText
    }, {
      onError: error => {
        if (error.message.includes(_constants.ERROR_CODE.INVALID_IMAGE) || error.message.includes(_constants.ERROR_CODE.VIOLENCE)) {
          _reactNative.Alert.alert('Inappropriate image', 'Please choose a different image to upload.', [{
            text: 'OK'
          }]);
        } else {
          _reactNative.Alert.alert('Upload failed', 'Please try again.', [{
            text: 'OK'
          }]);
        }
      }
    });
  };
  return {
    uploadImage,
    isImageUploading: isPending
  };
}
//# sourceMappingURL=useUpload.js.map