'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../_virtual/_tslib.js');
var React = require('react');
var styles = require('./styles.js');
var index$1 = require('../Button/index.js');
var utils = require('./utils.js');
var FileInfoWrapper = require('./FileInfoWrapper.js');
var UploaderContainerWrapper = require('./UploaderContainerWrapper.js');
var index = require('../../utils/index.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

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 = _tslib.__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 = React.useRef(null);
    const inputId = `uploader_${timestamp}`;
    const fileId = `file_${timestamp}`;
    const appendTestId = index.getId(`${props['data-testid']}-uploader`);
    const appendTrackId = index.getId(`${props['data-trackid']}-uploader`);
    React.useEffect(() => {
        utils.getImageUrlFromFile({ file, fileId });
    }, [JSON.stringify(file)]);
    return (React__default['default'].createElement(UploaderContainerWrapper.UploaderContainerWrapper, { "data-testid": appendTestId('container'), "data-trackid": appendTrackId('container'), width: width, optionalText: optionalText, assistText: assistText, label: label },
        React__default['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['default'].createElement(styles.UploaderWrapper, { hasFile: !!file, "data-testid": appendTestId('wrapper'), "data-trackid": appendTrackId('wrapper'), "aria-label": props['aria-label'], shouldDisplayThumbnail: shouldDisplayThumbnail },
            React__default['default'].createElement(FileInfoWrapper.FileInfoWrapper, { file: file, uploaderTitle: uploaderTitle, maxSizeDescription: maxSizeDescription, loading: loading, successMessage: successMessage, errorMessage: errorMessage }),
            React__default['default'].createElement(styles.ActionButtonsWrapper, null,
                !errorMessage && !file && loading && (React__default['default'].createElement(index$1.Button, { variant: "secondary", size: "small-expressive", onClick: cancelFileCallback, "aria-label": "Cancel" }, cancelBtnText)),
                !errorMessage && !file && !loading && (React__default['default'].createElement(index$1.Button, { variant: "secondary", size: "small-expressive", "aria-label": "Upload", "data-testid": appendTestId('upload-button'), "data-trackid": appendTrackId('upload-button'), onClick: () => utils.openFileExplorer({ inputId, file, errorMessage }) }, uploadFileBtnText)),
                (errorMessage || file) && !loading && (React__default['default'].createElement(index$1.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) {
                            utils.openFileExplorer({ inputId, file, errorMessage });
                        }
                    } }, errorMessage ? retryUploadBtnText : removeFileBtnText)))),
        shouldDisplayThumbnail && file && utils.isImageFile(file) && !errorMessage && (React__default['default'].createElement(styles.FileContainer, null,
            React__default['default'].createElement("img", { id: fileId, "data-testid": appendTestId('thumbnail'), src: "@jet-pie/react/src/components/Uploader/index", alt: file && typeof file !== 'string' ? file.name : '' })))));
}

exports.Uploader = Uploader;
