import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext, useRef, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Alert } from '@mui/material';
import { SCUserContext } from '@selfcommunity/react-core';
import Icon from '@mui/material/Icon';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { PREFIX } from './constants';
import { LoadingButton } from '@mui/lab';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { EventService } from '@selfcommunity/api-services';
import { Logger } from '@selfcommunity/utils';
import { defineMessages, useIntl } from 'react-intl';
const messages = defineMessages({
    errorLoadImage: {
        id: 'ui.changeGroupCover.button.change.alertErrorImage',
        defaultMessage: 'ui.changeGroupCover.button.change.alertErrorImage'
    }
});
const classes = {
    root: `${PREFIX}-upload-event-cover-root`
};
const Root = styled(LoadingButton, {
    name: PREFIX,
    slot: 'UploadEventCoverRoot'
})(() => ({}));
/**
 * > API documentation for the Community-JS Change Group Cover component. Learn about the available props and the CSS API.
 *
 *
 * This component renders a button that allows admins to edit the group cover and a popover that specifies format and sizes allowed.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/UploadEventCover)

 #### Import
 ```jsx
 import {UploadEventCover} from '@selfcommunity/react-ui';
 ```

 #### Component Name
 The name `SCUploadEventCover` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUploadEventCover-root|Styles applied to the root element.|

 * @param inProps
 */
export default function UploadEventCover(inProps) {
    //PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { eventId, onChange, className = false, isCreationMode = false } = props, rest = __rest(props, ["eventId", "onChange", "className", "isCreationMode"]);
    //CONTEXT
    const scUserContext = useContext(SCUserContext);
    //STATE
    let fileInput = useRef(null);
    const [loading, setLoading] = useState(false);
    const [alert, setAlert] = useState(null);
    // INTL
    const intl = useIntl();
    // Anonymous
    if (!scUserContext.user) {
        return null;
    }
    /**
     * Handles file upload
     * @param event
     */
    const handleUpload = (event) => {
        fileInput = event.target.files[0];
        if (fileInput) {
            const reader = new FileReader();
            reader.onload = (e) => {
                const img = new Image();
                img.onload = () => {
                    isCreationMode ? onChange && onChange(fileInput) : handleSave();
                };
                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                // @ts-ignore
                img.src = e.target.result;
            };
            // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
            // @ts-ignore
            reader.readAsDataURL(fileInput);
        }
    };
    /**
     * Handles cover saving after upload action
     */
    function handleSave() {
        setLoading(true);
        const formData = new FormData();
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        formData.append('image_original', fileInput);
        EventService.changeEventCover(eventId, formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then((data) => {
            onChange && onChange(data.image_medium);
            setLoading(false);
        })
            .catch((error) => {
            Logger.error(SCOPE_SC_UI, error);
            setLoading(false);
            setAlert(intl.formatMessage(messages.errorLoadImage));
        });
    }
    /**
     * If there is an error
     */
    if (alert) {
        return (_jsx(Alert, Object.assign({ color: "error", onClose: () => setAlert(null) }, { children: alert })));
    }
    return (_jsxs(_Fragment, { children: [_jsx("input", { type: "file", onChange: handleUpload, ref: fileInput, hidden: true, accept: ".gif,.png,.jpg,.jpeg" }), _jsx(Root, Object.assign({ className: classNames(classes.root, className), size: "medium", variant: "contained", disabled: loading, onClick: () => fileInput.current.click(), loading: loading }, rest, { children: _jsx(Icon, { children: "image" }) }))] }));
}
