import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { LoadingButton } from '@mui/lab';
import { Stack, Typography } from '@mui/material';
import Icon from '@mui/material/Icon';
import { styled } from '@mui/material/styles';
import ChunkedUploady from '@rpldy/chunked-uploady';
import { Endpoints } from '@selfcommunity/api-services';
import classNames from 'classnames';
import { useSnackbar } from 'notistack';
import { forwardRef, useCallback, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import MediaChunkUploader from '../../shared/MediaChunkUploader';
import asUploadButton from './asUploadButton';
import { PREFIX } from './constants';
const classes = {
    triggerRoot: `${PREFIX}-trigger-root`,
    triggerContent: `${PREFIX}-trigger-content`,
    triggerIcon: `${PREFIX}-trigger-icon`
};
const Root = styled(LoadingButton, {
    name: PREFIX,
    slot: 'TriggerRoot',
    overridesResolver: (_props, styles) => styles.triggerRoot,
    shouldForwardProp: (prop) => prop !== 'isSquare'
})(() => ({}));
const GalleryUploadStack = asUploadButton(forwardRef((props, ref) => _jsx(Stack, Object.assign({}, props, { "aria-label": "upload", ref: ref }))), { accept: 'image/*' });
function TriggerButton(props, ref) {
    // PROPS
    const { className, onAdd = null, isSquare = false } = props, rest = __rest(props, ["className", "onAdd", "isSquare"]);
    // STATE
    const [isUploading, setIsUploading] = useState(false);
    // HOOKS
    const { enqueueSnackbar, closeSnackbar } = useSnackbar();
    const handleFilterByMime = useCallback((file) => {
        return file.type.startsWith('image/');
    }, []);
    const handleSuccess = useCallback((media) => {
        onAdd === null || onAdd === void 0 ? void 0 : onAdd(media);
    }, []);
    const handleProgress = useCallback((chunks) => {
        setIsUploading(Object.keys(chunks).length > 0);
    }, []);
    const handleError = useCallback((chunk, error) => {
        const _snackBar = enqueueSnackbar(`${chunk.name}: ${error}`, {
            variant: 'error',
            anchorOrigin: { horizontal: 'center', vertical: 'top' },
            autoHideDuration: 2000,
            SnackbarProps: {
                onClick: () => {
                    closeSnackbar(_snackBar);
                }
            }
        });
    }, [enqueueSnackbar]);
    return (_jsxs(ChunkedUploady, Object.assign({ destination: {
            url: Endpoints.ComposerChunkUploadMedia.url(),
            method: Endpoints.ComposerChunkUploadMedia.method
        }, fileFilter: handleFilterByMime, chunkSize: 204800 }, { children: [_jsx(MediaChunkUploader, { onSuccess: handleSuccess, onProgress: handleProgress, onError: handleError, type: "image" }), _jsx(Root, Object.assign({ className: classNames(className, classes.triggerRoot), ref: ref }, rest, { "aria-label": "add media", loading: isUploading, 
                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                // @ts-ignore
                isSquare: isSquare }, { children: _jsxs(GalleryUploadStack, Object.assign({ className: classes.triggerContent }, { children: [!isSquare && (_jsx(Typography, Object.assign({ variant: "caption", component: "p" }, { children: _jsx(FormattedMessage, { id: "ui.eventMediaWidget.add", defaultMessage: "ui.eventMediaWidget.add" }) }))), _jsx(Icon, Object.assign({ className: classes.triggerIcon }, { children: "add" }))] })) }))] })));
}
export default forwardRef(TriggerButton);
