"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const react_native_camera_1 = require("react-native-camera");
const react_native_fast_image_1 = tslib_1.__importDefault(require("react-native-fast-image"));
const react_native_image_picker_1 = require("react-native-image-picker");
const types_1 = require("../../types");
const styles_1 = require("./styles");
const UploadPhoto = react_1.default.forwardRef(({ image, id, accessibility, accessibilityLabel, testID, uploadText = 'Adicionar Foto', uploadIcon = 'image', deleteIcon = 'trash', imageQuality = 0.5, uploadIconSize = 36, onPress, onUpload, onClearUpload, displayCamera = false, iconType = types_1.IconFonts.FontAwesome, ...rest }, ref) => {
    const [uploadedImage, setUploadedImage] = (0, react_1.useState)('');
    const cameraRef = (0, react_1.useRef)();
    (0, react_1.useEffect)(() => {
        if (image) {
            setUploadedImage(image);
        }
    }, [image]);
    const openPicker = () => {
        const options = {
            title: 'Selecionar foto',
            cancelButtonTitle: 'Cancelar',
            takePhotoButtonTitle: 'Tirar foto',
            chooseFromLibraryButtonTitle: 'Escolher da galeria',
            includeBase64: true,
            quality: imageQuality,
            storageOptions: {
                skipBackup: true,
                path: 'images',
            },
        };
        return new Promise((resolve) => {
            (0, react_native_image_picker_1.launchImageLibrary)(options, (response) => {
                if (!response.didCancel && response.assets) {
                    setUploadedImage(response?.assets[0]?.uri);
                    if (onUpload) {
                        onUpload(response);
                    }
                }
                resolve();
            });
        });
    };
    const takePicture = async () => {
        if (cameraRef.current) {
            const options = { quality: 0.5, base64: true };
            const data = await cameraRef.current.takePictureAsync(options);
            setUploadedImage(data);
            return data;
        }
    };
    const getUploadImage = () => {
        return uploadedImage;
    };
    const clearUploadImage = () => {
        setUploadedImage('');
    };
    const clearUpload = () => {
        setUploadedImage('');
        if (onClearUpload) {
            onClearUpload();
        }
    };
    const getCurrentPhoto = () => {
        if (uploadedImage) {
            return { uri: uploadedImage };
        }
        return '';
    };
    (0, react_1.useImperativeHandle)(ref, () => ({
        getUploadImage,
        clearUploadImage,
        takePicture,
        openPicker,
    }));
    return (<styles_1.Wrapper id={id || accessibility} accessibility={accessibility} accessibilityLabel={accessibilityLabel || accessibility} testID={testID || id} onPress={onPress || (onUpload && openPicker)} disabled={!onPress && !onUpload} {...rest}>
        {displayCamera && !uploadedImage && (<styles_1.CameraView ref={cameraRef} type={react_native_camera_1.RNCamera.Constants.Type.front} flashMode={react_native_camera_1.RNCamera.Constants.FlashMode.auto} androidCameraPermissionOptions={{
                title: 'Câmera',
                message: 'Precisamos da sua permissão para usar a câmera',
                buttonPositive: 'Ok',
                buttonNegative: 'Cancelar',
            }}/>)}
        {uploadedImage && (<react_native_fast_image_1.default source={getCurrentPhoto()} resizeMode={react_native_fast_image_1.default.resizeMode.cover} style={{ width: '101%', height: '101%' }}/>)}
        {!displayCamera && !uploadedImage && (<styles_1.UploadIconWrapper>
            <styles_1.UploadIcon disabled size={uploadIconSize} name={uploadIcon} id="" accessibility="" type={iconType}/>
            {uploadText && <styles_1.UploadText>{uploadText}</styles_1.UploadText>}
          </styles_1.UploadIconWrapper>)}
        {uploadedImage && (<styles_1.DeleteIconWrapper accessibility="Remover imagem" onPress={clearUpload}>
            <styles_1.DeleteIcon disabled name={deleteIcon} id="remove_image_button" accessibility="" type={iconType}/>
          </styles_1.DeleteIconWrapper>)}
      </styles_1.Wrapper>);
});
UploadPhoto.displayName = 'UploadPhotoComponent';
exports.default = UploadPhoto;
//# sourceMappingURL=index.js.map