import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useEffect, useState } from 'react';
import { COMMAND_PRIORITY_EDITOR, createCommand } from 'lexical';
import { $insertNodeToNearestRoot } from '@lexical/utils';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CircularProgress, Icon, IconButton } from '@mui/material';
import { styled } from '@mui/material/styles';
import ChunkedUploady from '@rpldy/chunked-uploady';
import { Endpoints } from '@selfcommunity/api-services';
import { useSCContext, useSCUser } from '@selfcommunity/react-core';
import MediaChunkUploader from '../../../shared/MediaChunkUploader';
import { asUploadButton } from '@rpldy/upload-button';
import { useSnackbar } from 'notistack';
import { $createImageNode, ImageNode } from '../nodes/ImageNode';
import { PREFIX } from '../constants';
export const INSERT_IMAGE_COMMAND = createCommand();
const UploadButton = asUploadButton(forwardRef((_a, ref) => {
    var { progress = null } = _a, rest = __rest(_a, ["progress"]);
    return (_jsx(IconButton, Object.assign({}, rest, { "aria-label": "upload image", ref: ref }, { children: progress ? _jsx(CircularProgress, { variant: "determinate", value: progress, size: "1rem" }) : _jsx(Icon, Object.assign({ color: "inherit" }, { children: "image" })) })));
}));
function Image({ editor, className = '' }) {
    // CONTEXT
    const scContext = useSCContext();
    const scUserContext = useSCUser();
    // STATE
    const [uploading, setUploading] = useState({});
    // HOOKS
    const { enqueueSnackbar } = useSnackbar();
    // HANDLERS
    const handleFileUploadFilter = (file, index, a) => {
        return file.type.startsWith('image/');
    };
    const handleUploadSuccess = (media) => {
        const data = {
            altText: media.title,
            src: media.image,
            width: media.image_width,
            height: media.image_height
        };
        editor.focus();
        editor.dispatchCommand(INSERT_IMAGE_COMMAND, data);
    };
    const handleUploadProgress = (chunks) => {
        setUploading(Object.assign({}, chunks));
    };
    const handleUploadError = (chunk, error) => {
        enqueueSnackbar(error, {
            variant: 'error',
            autoHideDuration: 3000
        });
    };
    if (!scUserContext.user) {
        return null;
    }
    return (_jsxs(ChunkedUploady, Object.assign({ destination: {
            url: `${scContext.settings.portal}${Endpoints.ComposerChunkUploadMedia.url()}`,
            headers: Object.assign({}, (scContext.settings.session &&
                scContext.settings.session.authToken && {
                Authorization: `Bearer ${scContext.settings.session.authToken.accessToken}`
            })),
            method: Endpoints.ComposerChunkUploadMedia.method
        }, chunkSize: 204800, multiple: true, accept: "image/*", fileFilter: handleFileUploadFilter }, { children: [_jsx(MediaChunkUploader, { type: "eimage", onSuccess: handleUploadSuccess, onProgress: handleUploadProgress, onError: handleUploadError }), _jsx(UploadButton, { className: className, extraProps: {
                    disabled: Object.keys(uploading).length !== 0,
                    progress: Object.keys(uploading).length !== 0 ? Object.values(uploading)[0].completed : null
                } })] })));
}
const classes = {
    root: `${PREFIX}-image-plugin-root`
};
const Root = styled(Image, {
    name: PREFIX,
    slot: 'ImagePluginRoot'
})(() => ({}));
export default function ImagePlugin() {
    const [editor] = useLexicalComposerContext();
    useEffect(() => {
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        if (!editor.hasNodes([ImageNode])) {
            return;
        }
        return editor.registerCommand(INSERT_IMAGE_COMMAND, (payload) => {
            const imageNode = $createImageNode({
                src: payload.src,
                altText: payload.altText,
                maxWidth: '100%',
                width: payload.width,
                height: payload.height
            });
            // The image is not editable so it is better to position it near the root element
            $insertNodeToNearestRoot(imageNode);
            return true;
        }, COMMAND_PRIORITY_EDITOR);
    }, [editor]);
    // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
    // @ts-ignore
    if (!editor.hasNodes([ImageNode])) {
        return null;
    }
    return _jsx(Root, { editor: editor, className: classes.root });
}
