import { __rest } from '../../_virtual/_tslib.js';
import React__default, { useRef, useEffect } from 'react';
import { UploaderWrapper, ActionButtonsWrapper, FileContainer } from './styles.js';
import { Button } from '../Button/index.js';
import { getImageUrlFromFile, openFileExplorer, isImageFile } from './utils.js';
import { FileInfoWrapper } from './FileInfoWrapper.js';
import { UploaderContainerWrapper } from './UploaderContainerWrapper.js';
import { getId } from '../../utils/index.js';

function Uploader(_a) {
    var { label, uploaderTitle, maxSizeDescription, uploadFileBtnText = 'Browse', removeFileBtnText = 'Remove', retryUploadBtnText = 'Retry', cancelBtnText = 'Cancel', assistText, optionalText, fileType = '*', successMessage, errorMessage, width, getFileCallback, removeFileCallback, cancelFileCallback, file, shouldDisplayThumbnail = false, loading } = _a, props = __rest(_a, ["label", "uploaderTitle", "maxSizeDescription", "uploadFileBtnText", "removeFileBtnText", "retryUploadBtnText", "cancelBtnText", "assistText", "optionalText", "fileType", "successMessage", "errorMessage", "width", "getFileCallback", "removeFileCallback", "cancelFileCallback", "file", "shouldDisplayThumbnail", "loading"]);
    const timestamp = Date.now();
    const inputRef = useRef(null);
    const inputId = `uploader_${timestamp}`;
    const fileId = `file_${timestamp}`;
    const appendTestId = getId(`${props['data-testid']}-uploader`);
    const appendTrackId = getId(`${props['data-trackid']}-uploader`);
    useEffect(() => {
        getImageUrlFromFile({ file, fileId });
    }, [JSON.stringify(file)]);
    return (React__default.createElement(UploaderContainerWrapper, { "data-testid": appendTestId('container'), "data-trackid": appendTrackId('container'), width: width, optionalText: optionalText, assistText: assistText, label: label },
        React__default.createElement("input", { id: inputId, "data-testid": appendTestId('input'), type: "file", ref: inputRef, onChange: getFileCallback, name: "uploader", multiple: false, accept: fileType, style: { display: 'none' }, onClick: () => {
                if (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) {
                    inputRef.current.value = '';
                }
            } }),
        React__default.createElement(UploaderWrapper, { hasFile: !!file, "data-testid": appendTestId('wrapper'), "data-trackid": appendTrackId('wrapper'), "aria-label": props['aria-label'], shouldDisplayThumbnail: shouldDisplayThumbnail },
            React__default.createElement(FileInfoWrapper, { file: file, uploaderTitle: uploaderTitle, maxSizeDescription: maxSizeDescription, loading: loading, successMessage: successMessage, errorMessage: errorMessage }),
            React__default.createElement(ActionButtonsWrapper, null,
                !errorMessage && !file && loading && (React__default.createElement(Button, { variant: "secondary", size: "small-expressive", onClick: cancelFileCallback, "aria-label": "Cancel" }, cancelBtnText)),
                !errorMessage && !file && !loading && (React__default.createElement(Button, { variant: "secondary", size: "small-expressive", "aria-label": "Upload", "data-testid": appendTestId('upload-button'), "data-trackid": appendTrackId('upload-button'), onClick: () => openFileExplorer({ inputId, file, errorMessage }) }, uploadFileBtnText)),
                (errorMessage || file) && !loading && (React__default.createElement(Button, { variant: "secondary", size: "small-expressive", "data-testid": appendTestId(errorMessage ? 'retry-button' : 'remove-button'), "data-trackid": appendTrackId(errorMessage ? 'retry-button' : 'remove-button'), "aria-label": errorMessage ? 'Retry' : 'Remove', onClick: (e) => {
                        e.stopPropagation();
                        removeFileCallback();
                        if (errorMessage) {
                            openFileExplorer({ inputId, file, errorMessage });
                        }
                    } }, errorMessage ? retryUploadBtnText : removeFileBtnText)))),
        shouldDisplayThumbnail && file && isImageFile(file) && !errorMessage && (React__default.createElement(FileContainer, null,
            React__default.createElement("img", { id: fileId, "data-testid": appendTestId('thumbnail'), src: "@jet-pie/react/src/components/Uploader/index", alt: file && typeof file !== 'string' ? file.name : '' })))));
}

export { Uploader };
