"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const material_1 = require("@mui/material");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const constants_1 = require("./constants");
const styles_1 = require("@mui/material/styles");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const MediaChunkUploader_1 = tslib_1.__importDefault(require("../../MediaChunkUploader"));
const chunked_uploady_1 = tslib_1.__importDefault(require("@rpldy/chunked-uploady"));
const api_services_1 = require("@selfcommunity/api-services");
const asUploadButton_1 = tslib_1.__importDefault(require("./asUploadButton"));
const notistack_1 = require("notistack");
const classes = {
    triggerRoot: `${constants_1.PREFIX}-trigger-root`,
    triggerDrawerRoot: `${constants_1.PREFIX}-trigger-drawer-root`,
    triggerMenuRoot: `${constants_1.PREFIX}-trigger-menu-root`,
    paper: `${constants_1.PREFIX}-paper`,
    item: `${constants_1.PREFIX}-item`,
};
const Root = (0, styles_1.styled)(material_1.IconButton, {
    name: constants_1.PREFIX,
    slot: 'TriggerRoot'
})(() => ({}));
const SwipeableDrawerRoot = (0, styles_1.styled)(material_1.SwipeableDrawer, {
    name: constants_1.PREFIX,
    slot: 'TriggerDrawerRoot'
})(() => ({}));
const MenuRoot = (0, styles_1.styled)(material_1.Menu, {
    name: constants_1.PREFIX,
    slot: 'TriggerMenuRoot'
})(() => ({}));
const PhotoUploadListItemButton = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(material_1.ListItemButton, Object.assign({}, props, { "aria-label": "upload", ref: ref })))), { accept: 'image/*', capture: 'camera' });
const GalleryUploadListItemButton = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(material_1.ListItemButton, Object.assign({}, props, { "aria-label": "upload", ref: ref })))), { accept: 'image/*' });
const DocumentUploadListItemButton = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(material_1.ListItemButton, Object.assign({}, props, { "aria-label": "upload", ref: ref })))), { accept: 'application/pdf' });
const GalleryUploadMenuItem = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(material_1.MenuItem, Object.assign({}, props, { "aria-label": "upload", ref: ref })))), { accept: 'image/*' });
const DocumentUploadMenuItem = (0, asUploadButton_1.default)((0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(material_1.MenuItem, Object.assign({}, props, { "aria-label": "upload", ref: ref })))), { accept: 'application/pdf' });
exports.default = (_a) => {
    var { className, onAdd = null } = _a, rest = tslib_1.__rest(_a, ["className", "onAdd"]);
    // STATE
    const [anchorEl, setAnchorEl] = (0, react_1.useState)(null);
    const [isUploading, setIsUploading] = (0, react_1.useState)(false);
    // CONTEXT
    const scContext = (0, react_core_1.useSCContext)();
    // HOOKS
    const theme = (0, material_1.useTheme)();
    const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md'));
    const { enqueueSnackbar, closeSnackbar } = (0, notistack_1.useSnackbar)();
    // HANDLERS
    const handleOpen = (0, react_1.useCallback)((event) => {
        setAnchorEl(event.currentTarget);
    }, []);
    const handleClose = (0, react_1.useCallback)(() => {
        setAnchorEl(null);
    }, []);
    const handleFilterByMime = (0, react_1.useCallback)((file) => {
        return file.type.startsWith('image/') || file.type === 'application/pdf';
    }, []);
    const handleSuccess = (0, react_1.useCallback)((media) => {
        onAdd && onAdd(media);
    }, [onAdd]);
    const handleProgress = (0, react_1.useCallback)((chunks) => {
        //console.log(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]);
    const list = (0, react_1.useMemo)(() => {
        if (isMobile) {
            return [
                (0, jsx_runtime_1.jsx)(material_1.ListItem, Object.assign({ className: classes.item }, { children: (0, jsx_runtime_1.jsx)(PhotoUploadListItemButton, Object.assign({ inputFieldName: "image" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.media.file.photo", defaultMessage: "ui.composer.media.file.photo" }) })) }), "photo"),
                (0, jsx_runtime_1.jsx)(material_1.ListItem, Object.assign({ className: classes.item }, { children: (0, jsx_runtime_1.jsx)(GalleryUploadListItemButton, Object.assign({ inputFieldName: "image" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.media.file.gallery", defaultMessage: "ui.composer.media.file.gallery" }) })) }), "gallery"),
                (0, jsx_runtime_1.jsx)(material_1.ListItem, Object.assign({ className: classes.item }, { children: (0, jsx_runtime_1.jsx)(DocumentUploadListItemButton, Object.assign({ inputFieldName: "document" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.media.file.document", defaultMessage: "ui.composer.media.file.document" }) })) }), "document")
            ];
        }
        else {
            return [
                (0, jsx_runtime_1.jsx)(GalleryUploadMenuItem, Object.assign({ extraProps: { className: classes.item }, inputFieldName: "image" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.media.file.gallery", defaultMessage: "ui.composer.media.file.gallery" }) }), "gallery"),
                (0, jsx_runtime_1.jsx)(DocumentUploadMenuItem, Object.assign({ extraProps: { className: classes.item }, inputFieldName: "document" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.media.file.document", defaultMessage: "ui.composer.media.file.document" }) }), "document")
            ];
        }
    }, [isMobile, handleFilterByMime, handleSuccess, handleProgress, handleError]);
    return ((0, jsx_runtime_1.jsxs)(chunked_uploady_1.default, Object.assign({ destination: {
            url: `${scContext.settings.portal}${api_services_1.Endpoints.ComposerChunkUploadMedia.url()}`,
            method: api_services_1.Endpoints.ComposerChunkUploadMedia.method
        }, fileFilter: handleFilterByMime, chunkSize: 204800, multiple: true }, { children: [(0, jsx_runtime_1.jsx)(MediaChunkUploader_1.default, { onSuccess: handleSuccess, onProgress: handleProgress, onError: handleError }), (0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(className, classes.triggerRoot) }, rest, { "aria-label": "add media", disabled: isUploading, onClick: handleOpen }, { children: isUploading ? (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 20 }) : (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "photo_file" }) })), isMobile ? ((0, jsx_runtime_1.jsx)(SwipeableDrawerRoot, Object.assign({ onClick: () => setAnchorEl(null), className: classes.triggerDrawerRoot, anchor: "bottom", open: Boolean(anchorEl), onClose: handleClose, onOpen: handleOpen, PaperProps: { className: classes.paper }, disableSwipeToOpen: true }, { children: (0, jsx_runtime_1.jsx)(material_1.List, { children: list }) }))) : ((0, jsx_runtime_1.jsx)(MenuRoot, Object.assign({ onClick: () => setAnchorEl(null), className: classes.triggerMenuRoot, anchorEl: anchorEl, open: Boolean(anchorEl), onClose: handleClose, PaperProps: { className: classes.paper } }, { children: list })))] })));
};
