"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const lab_1 = require("@mui/lab");
const material_1 = require("@mui/material");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const styles_1 = require("@mui/material/styles");
const chunked_uploady_1 = tslib_1.__importDefault(require("@rpldy/chunked-uploady"));
const api_services_1 = require("@selfcommunity/api-services");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const notistack_1 = require("notistack");
const react_1 = require("react");
const react_intl_1 = require("react-intl");
const MediaChunkUploader_1 = tslib_1.__importDefault(require("../../shared/MediaChunkUploader"));
const asUploadButton_1 = tslib_1.__importDefault(require("./asUploadButton"));
const constants_1 = require("./constants");
const classes = {
    triggerRoot: `${constants_1.PREFIX}-trigger-root`,
    triggerContent: `${constants_1.PREFIX}-trigger-content`,
    triggerIcon: `${constants_1.PREFIX}-trigger-icon`
};
const Root = (0, styles_1.styled)(lab_1.LoadingButton, {
    name: constants_1.PREFIX,
    slot: 'TriggerRoot',
    overridesResolver: (_props, styles) => styles.triggerRoot,
    shouldForwardProp: (prop) => prop !== 'isSquare'
})(() => ({}));
const GalleryUploadStack = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => (0, jsx_runtime_1.jsx)(material_1.Stack, Object.assign({}, props, { "aria-label": "upload", ref: ref }))), { accept: 'image/*' });
function TriggerButton(props, ref) {
    // PROPS
    const { className, onAdd = null, isSquare = false } = props, rest = tslib_1.__rest(props, ["className", "onAdd", "isSquare"]);
    // STATE
    const [isUploading, setIsUploading] = (0, react_1.useState)(false);
    // HOOKS
    const { enqueueSnackbar, closeSnackbar } = (0, notistack_1.useSnackbar)();
    const handleFilterByMime = (0, react_1.useCallback)((file) => {
        return file.type.startsWith('image/');
    }, []);
    const handleSuccess = (0, react_1.useCallback)((media) => {
        onAdd === null || onAdd === void 0 ? void 0 : onAdd(media);
    }, []);
    const handleProgress = (0, react_1.useCallback)((chunks) => {
        setIsUploading(Object.keys(chunks).length > 0);
    }, []);
    const handleError = (0, react_1.useCallback)((chunk, error) => {
        const _snackBar = enqueueSnackbar(`${chunk.name}: ${error}`, {
            variant: 'error',
            anchorOrigin: { horizontal: 'center', vertical: 'top' },
            autoHideDuration: 2000,
            SnackbarProps: {
                onClick: () => {
                    closeSnackbar(_snackBar);
                }
            }
        });
    }, [enqueueSnackbar]);
    return ((0, jsx_runtime_1.jsxs)(chunked_uploady_1.default, Object.assign({ destination: {
            url: api_services_1.Endpoints.ComposerChunkUploadMedia.url(),
            method: api_services_1.Endpoints.ComposerChunkUploadMedia.method
        }, fileFilter: handleFilterByMime, chunkSize: 204800 }, { children: [(0, jsx_runtime_1.jsx)(MediaChunkUploader_1.default, { onSuccess: handleSuccess, onProgress: handleProgress, onError: handleError, type: "image" }), (0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(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: (0, jsx_runtime_1.jsxs)(GalleryUploadStack, Object.assign({ className: classes.triggerContent }, { children: [!isSquare && ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "caption", component: "p" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.eventMediaWidget.add", defaultMessage: "ui.eventMediaWidget.add" }) }))), (0, jsx_runtime_1.jsx)(Icon_1.default, Object.assign({ className: classes.triggerIcon }, { children: "add" }))] })) }))] })));
}
exports.default = (0, react_1.forwardRef)(TriggerButton);
