"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeImagePicker = require("react-native-image-picker");
var _utils = require("../utils");
var _usePermissions = require("./usePermissions");
var _file = require("../../core/legacy/file");
var _hooks = require("../../core/hooks");
const useImagePicker = () => {
  const {
    getCameraPermission
  } = (0, _usePermissions.useCameraPermission)();
  const [progress, setProgress] = (0, _react.useState)(0);
  const [imageUri, setImageUri] = (0, _react.useState)(null);
  const [uploadedImage, setUploadedImage] = (0, _react.useState)(null);
  const {
    uploadImage,
    isImageUploading
  } = (0, _hooks.useUpload)();
  const uploadFileToAmity = async path => {
    try {
      setImageUri(path);
      const {
        data
      } = await uploadImage({
        file: path,
        onProgress: setProgress
      });
      setUploadedImage(data[0]);
      setProgress(0);
      return data[0];
    } catch (error) {
      return null;
    } finally {
      setProgress(0);
    }
  };
  const openImageGallery = async options => {
    const result = await (0, _reactNativeImagePicker.launchImageLibrary)(options);
    if (!result.didCancel && result.assets && result.assets.length > 0) {
      var _result$assets$, _result$assets$2;
      if (!(0, _utils.isValidImageType)((_result$assets$ = result.assets[0]) === null || _result$assets$ === void 0 ? void 0 : _result$assets$.type)) {
        return _reactNative.Alert.alert('Unsupported image type', 'Please upload a PNG or JPG image.', [{
          text: 'OK'
        }]);
      }
      return uploadFileToAmity((_result$assets$2 = result.assets[0]) === null || _result$assets$2 === void 0 ? void 0 : _result$assets$2.uri);
    }
  };
  const openCamera = async options => {
    const cameraPermission = await getCameraPermission();
    if (!cameraPermission) return _reactNative.Linking.openSettings();
    const result = await (0, _reactNativeImagePicker.launchCamera)(options);
    if (!result.didCancel && result.assets && result.assets.length > 0) {
      var _result$assets$3, _result$assets$4;
      if (!(0, _utils.isValidImageType)((_result$assets$3 = result.assets[0]) === null || _result$assets$3 === void 0 ? void 0 : _result$assets$3.type)) {
        return _reactNative.Alert.alert('Unsupported image type', 'Please upload a PNG or JPG image.', [{
          text: 'OK'
        }]);
      }
      return uploadFileToAmity((_result$assets$4 = result.assets[0]) === null || _result$assets$4 === void 0 ? void 0 : _result$assets$4.uri);
    }
  };
  const removeSelectedImage = () => {
    setProgress(0);
    setImageUri(null);
    setUploadedImage(null);
    (0, _file.deleteAmityFile)(uploadedImage === null || uploadedImage === void 0 ? void 0 : uploadedImage.fileId);
  };
  return {
    isLoading: isImageUploading,
    imageUri,
    progress,
    uploadedImage,
    removeSelectedImage,
    openImageGallery,
    openCamera
  };
};
var _default = exports.default = useImagePicker;
//# sourceMappingURL=useImagePicker.js.map