"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.deleteAmityFile = deleteAmityFile;
exports.uploadFile = uploadFile;
exports.uploadImageFile = uploadImageFile;
exports.uploadVideoFile = uploadVideoFile;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _reactNative = require("react-native");
async function uploadFile(filePath, perCentCallback) {
  return await new Promise(async (resolve, reject) => {
    const formData = new FormData();
    const parts = filePath.split('/');
    const fileName = parts[parts.length - 1];
    const fileType = _reactNative.Platform.OS === 'ios' ? 'image/jpeg' : 'image/jpg';
    const uri = _reactNative.Platform.OS === 'android' ? filePath : filePath.replace('file://', '');
    formData.append('files', {
      name: fileName,
      type: fileType,
      uri: uri
    });
    const {
      data: file
    } = await _tsSdkReactNative.FileRepository.uploadFile(formData, percent => {
      perCentCallback && perCentCallback(percent);
    });
    if (file) {
      resolve(file);
    } else {
      reject('Upload error');
    }
  });
}
async function uploadImageFile(filePath, perCentCallback) {
  return await new Promise(async (resolve, reject) => {
    try {
      const formData = new FormData();
      const parts = filePath.split('/');
      const fileName = parts[parts.length - 1];
      const fileType = _reactNative.Platform.OS === 'ios' ? 'image/jpeg' : 'image/jpg';
      const uri = _reactNative.Platform.OS === 'android' ? filePath : filePath.replace('file://', '');
      formData.append('files', {
        name: fileName,
        type: fileType,
        uri: uri
      });
      const {
        data: file
      } = await _tsSdkReactNative.FileRepository.uploadImage(formData, percent => {
        perCentCallback && perCentCallback(percent);
      });
      if (file) {
        resolve(file);
      } else {
        reject({
          message: 'Upload failed - no file data returned'
        });
      }
    } catch (error) {
      var _error$message, _error$message2;
      if (error !== null && error !== void 0 && (_error$message = error.message) !== null && _error$message !== void 0 && _error$message.includes('INVALID_IMAGE') || error !== null && error !== void 0 && (_error$message2 = error.message) !== null && _error$message2 !== void 0 && _error$message2.includes('Inappropriate')) {
        reject({
          message: 'Inappropriate image',
          details: 'Please choose a different image to upload.',
          code: 'INVALID_IMAGE'
        });
      } else {
        reject({
          message: 'Upload failed',
          details: (error === null || error === void 0 ? void 0 : error.message) || "We couldn't complete your upload. Please try again.",
          originalError: error
        });
      }
    }
  });
}
async function uploadVideoFile(filePath, perCentCallback) {
  return await new Promise(async (resolve, reject) => {
    const formData = new FormData();
    const parts = filePath.split('/');
    const fileName = parts[parts.length - 1];
    // const fileType = Platform.OS === 'ios' ? 'image/jpeg' : 'image/jpg';
    const uri = _reactNative.Platform.OS === 'android' ? filePath : filePath.replace('file://', '');
    formData.append('files', {
      name: fileName,
      type: 'video/*',
      uri: uri
    });
    const {
      data: file
    } = await _tsSdkReactNative.FileRepository.uploadVideo(formData, _tsSdkReactNative.ContentFeedType.POST, percent => {
      perCentCallback && perCentCallback(percent);
    });
    if (file) {
      resolve(file);
    } else {
      reject('Upload error');
    }
  });
}
async function deleteAmityFile(fileId) {
  const reactionObject = new Promise(async (resolve, reject) => {
    try {
      const isFileDeleted = await _tsSdkReactNative.FileRepository.deleteFile(fileId);
      resolve(isFileDeleted);
    } catch (error) {
      reject(error);
    }
  });
  return reactionObject;
}
//# sourceMappingURL=file.js.map