text: // This file was generated by Mendix Studio Pro.
  //
  // WARNING: Only the following code will be retained when actions are regenerated:
  // - the import list
  // - the code between BEGIN USER CODE and END USER CODE
  // - the code between BEGIN EXTRA CODE and END EXTRA CODE
  // Other code you write will be lost the next time you deploy the project.
  import { Big } from "big.js";
  import { Alert, Linking, NativeModules } from "react-native";
  import ImagePicker from "react-native-image-picker";
  
  // BEGIN EXTRA CODE
  // END EXTRA CODE
  
  /**
   * @param {MxObject} picture - This field is required.
   * @param {"NativeMobileResources.PictureSource.camera"|"NativeMobileResources.PictureSource.imageLibrary"|"NativeMobileResources.PictureSource.either"} pictureSource - Select a picture from the library or the camera. The default is to let the user decide.
   * @param {"NativeMobileResources.PictureQuality.original"|"NativeMobileResources.PictureQuality.low"|"NativeMobileResources.PictureQuality.medium"|"NativeMobileResources.PictureQuality.high"|"NativeMobileResources.PictureQuality.custom"} pictureQuality - Set to empty to use default value 'medium'.
   * @param {Big} maximumWidth - The picture will be scaled to this maximum pixel width, while maintaing the aspect ratio.
   * @param {Big} maximumHeight - The picture will be scaled to this maximum pixel height, while maintaing the aspect ratio.
   * @returns {Promise.<boolean>}
   */
  export async function TakePicture(picture, pictureSource, pictureQuality, maximumWidth, maximumHeight) {
  	// BEGIN USER CODE
      // Documentation https://github.com/react-native-community/react-native-image-picker/blob/master/docs/Reference.md
      if (!picture) {
          return Promise.reject(new Error("Input parameter 'Picture' is required"));
      }
      if (!picture.inheritsFrom("System.FileDocument")) {
          const entity = picture.getEntity();
          return Promise.reject(new Error(`Entity ${entity} does not inherit from 'System.FileDocument'`));
      }
      if (pictureQuality === "custom" && !maximumHeight && !maximumWidth) {
          return Promise.reject(new Error("Picture quality is set to 'Custom', but no maximum width or height was provided"));
      }
      return takePicture()
          .then(uri => {
          if (!uri) {
              return false;
          }
          return storeFile(picture, uri);
      })
          .catch(error => {
          if (error === "canceled") {
              return false;
          }
          throw new Error(error);
      });
      function takePicture() {
          return new Promise((resolve, reject) => {
              const options = getOptions();
              const method = getPictureMethod();
              method(options, response => {
                  if (response.didCancel) {
                      return resolve();
                  }
                  if (response.error) {
                      const unhandledError = handleImagePickerError(response.error);
                      if (!unhandledError) {
                          return resolve();
                      }
                      return reject(new Error(response.error));
                  }
                  return resolve(response.uri);
              });
          });
      }
      function storeFile(imageObject, uri) {
          return new Promise((resolve, reject) => {
              fetch(uri)
                  .then(res => res.blob())
                  .then(blob => {
                  const guid = imageObject.getGuid();
                  // eslint-disable-next-line no-useless-escape
                  const filename = /[^\/]*$/.exec(uri)[0];
                  const onSuccess = () => {
                      NativeModules.NativeFsModule.remove(uri).then(() => {
                          imageObject.set("Name", filename);
                          mx.data.commit({
                              mxobj: imageObject,
                              callback: () => resolve(true),
                              error: (error) => reject(error)
                          });
                      });
                  };
                  const onError = (error) => {
                      NativeModules.NativeFsModule.remove(uri).then(undefined);
                      reject(error);
                  };
                  mx.data.saveDocument(guid, filename, {}, blob, onSuccess, onError);
              });
          });
      }
      // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
      function getPictureMethod() {
          const source = pictureSource ? pictureSource : "either";
          switch (source) {
              case "imageLibrary":
                  return ImagePicker.launchImageLibrary;
              case "camera":
                  return ImagePicker.launchCamera;
              case "either":
              default:
                  return ImagePicker.showImagePicker;
          }
      }
      // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
      function getOptions() {
          const { maxWidth, maxHeight } = getPictureQuality();
          return {
              mediaType: "photo",
              maxWidth,
              maxHeight,
              noData: true,
              permissionDenied: {
                  title: "This app does not have access to your camera or photos",
                  text: "To enable access, tap Settings > Permissions and turn on Camera and Storage.",
                  reTryTitle: "Settings",
                  okTitle: "Cancel"
              },
              storageOptions: {
                  skipBackup: true,
                  cameraRoll: false,
                  privateDirectory: true
              }
          };
      }
      function getPictureQuality() {
          switch (pictureQuality) {
              case "low":
                  return {
                      maxWidth: 1024,
                      maxHeight: 1024
                  };
              case "medium":
              default:
                  return {
                      maxWidth: 2048,
                      maxHeight: 2048
                  };
              case "high":
                  return {
                      maxWidth: 4096,
                      maxHeight: 4096
                  };
              case "custom":
                  return {
                      maxWidth: Number(maximumWidth),
                      maxHeight: Number(maximumHeight)
                  };
          }
      }
      function handleImagePickerError(error) {
          const ERRORS = {
              AndroidPermissionDenied: "Permissions weren't granted",
              iOSPhotoLibraryPermissionDenied: "Photo library permissions not granted",
              iOSCameraPermissionDenied: "Camera permissions not granted"
          };
          switch (error) {
              case ERRORS.iOSPhotoLibraryPermissionDenied:
                  showiOSPermissionAlert("This app does not have access to your photos or videos", "To enable access, tap Settings and turn on Photos.");
                  return;
              case ERRORS.iOSCameraPermissionDenied:
                  showiOSPermissionAlert("This app does not have access to your camera", "To enable access, tap Settings and turn on Camera.");
                  return;
              case ERRORS.AndroidPermissionDenied:
                  // Ignore this error because the image picker plugin already shows an alert in this case.
                  return;
              default:
                  return error;
          }
      }
      function showiOSPermissionAlert(title, message) {
          Alert.alert(title, message, [
              { text: "Cancel", style: "cancel" },
              { text: "Settings", onPress: () => Linking.openURL("app-settings:") }
          ], { cancelable: false });
      }
  	// END USER CODE
  }
  