import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { styled } from '@mui/material/styles';
import { Alert, Button, CardMedia, Icon, Typography } from '@mui/material';
import Box from '@mui/material/Box';
import { useThemeProps } from '@mui/system';
import classNames from 'classnames';
import { useEffect, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { PREFIX } from '../../constants';
import ProgressBar from '../../../../shared/ProgressBar';
import { SCOnBoardingStepStatusType } from '@selfcommunity/types';
import { Player } from '@lottiefiles/react-lottie-player';
import CategoryAPlaceholder from '../../../../assets/onBoarding/categoryA';
import CategoryBPlaceholder from '../../../../assets/onBoarding/categoryB';
import animatedProgress from '../../../../assets/onBoarding/progress/category_progress.json';
const classes = {
    root: `${PREFIX}-category-root`,
    title: `${PREFIX}-category-title`,
    summary: `${PREFIX}-category-summary`,
    image: `${PREFIX}-category-image`,
    action: `${PREFIX}-category-action`,
    button: `${PREFIX}-category-button`,
    success: `${PREFIX}-success`,
    progress: `${PREFIX}-category-progress`,
    animationProgress: `${PREFIX}-category-animation-progress`
};
function CircledLetter({ letter, statement }) {
    return (_jsxs(Box, Object.assign({ component: "span", sx: { display: 'flex', alignItems: 'center', gap: 1, mb: 1.5 } }, { children: [_jsx(Box, Object.assign({ component: "span", sx: {
                    width: 22,
                    height: 22,
                    borderRadius: '50%',
                    backgroundColor: 'black',
                    display: 'flex',
                    justifyContent: 'center',
                    alignItems: 'center'
                } }, { children: _jsx(Typography, Object.assign({ component: "span", sx: {
                        color: 'white',
                        textTransform: 'uppercase',
                        fontWeight: 'bold'
                    } }, { children: letter })) })), statement] })));
}
const Root = styled(Box, {
    name: PREFIX,
    slot: 'CategoryRoot'
})(() => ({}));
export default function Category(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, step, handleCategoriesCreation } = props;
    // STATE
    const [hover, setHover] = useState(false);
    const [progress, setProgress] = useState(step.completion_percentage);
    useEffect(() => {
        if (step.status === SCOnBoardingStepStatusType.IN_PROGRESS) {
            const intervalId = setInterval(() => {
                setProgress((prev) => {
                    if (prev < step.completion_percentage) {
                        return prev + 1;
                    }
                    else {
                        clearInterval(intervalId);
                        return prev;
                    }
                });
            }, 1000);
            return () => clearInterval(intervalId);
        }
    }, [step.status, step.completion_percentage]);
    const getLoadingMessage = useMemo(() => {
        let message;
        if (progress <= 10) {
            message = (_jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.loading.a", defaultMessage: "ui.onBoardingWidget.step.categories.loading.a" }));
        }
        else if (progress <= 20) {
            message = (_jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.loading.b", defaultMessage: "ui.onBoardingWidget.step.categories.loading.b" }));
        }
        else if (progress <= 40) {
            message = (_jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.loading.c", defaultMessage: "ui.onBoardingWidget.step.categories.loading.c" }));
        }
        else if (progress <= 60) {
            message = (_jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.loading.d", defaultMessage: "ui.onBoardingWidget.step.categories.loading.d" }));
        }
        else {
            message = (_jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.loading.e", defaultMessage: "ui.onBoardingWidget.step.categories.loading.e" }));
        }
        return message;
    }, [progress]);
    return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, { children: [_jsx(Typography, Object.assign({ variant: "h4", className: classes.title }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.categories", defaultMessage: "ui.onBoardingWidget.categories" }) })), _jsx(Typography, Object.assign({ className: classes.summary }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.category.summary", defaultMessage: "ui.onBoardingWidget.step.category.summary" }) })), (step === null || step === void 0 ? void 0 : step.status) !== SCOnBoardingStepStatusType.IN_PROGRESS && (_jsxs(_Fragment, { children: [_jsx(Typography, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.category.generation.steps", defaultMessage: "ui.onBoardingWidget.step.category.generation.steps", values: {
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                iconA: (...chunks) => _jsx(CircledLetter, { letter: "a", statement: chunks }),
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                iconB: (...chunks) => _jsx(CircledLetter, { letter: "b", statement: chunks }),
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                iconC: (...chunks) => _jsx(CircledLetter, { letter: "c", statement: chunks })
                            } }) }), _jsx(CardMedia, { className: classes.image, component: "img", src: CategoryAPlaceholder }), _jsx(CardMedia, { className: classes.image, component: "img", src: CategoryBPlaceholder })] })), _jsx(Box, Object.assign({ component: "span", className: classes.action }, { children: (step === null || step === void 0 ? void 0 : step.status) === SCOnBoardingStepStatusType.COMPLETED ? (_jsx(Alert, Object.assign({ severity: "success" }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.categories.success", defaultMessage: "ui.onBoardingWidget.step.categories.success" }) }))) : (step === null || step === void 0 ? void 0 : step.status) === SCOnBoardingStepStatusType.IN_PROGRESS ? (_jsxs(Box, Object.assign({ className: classes.progress }, { children: [_jsx(Player, { autoplay: true, loop: true, src: animatedProgress, className: classes.animationProgress, controls: false }), _jsx(ProgressBar, { value: progress, hideBar: true, loadingMessage: _jsx(Typography, Object.assign({ variant: "h4" }, { children: getLoadingMessage })) })] }))) : (_jsx(Button, Object.assign({ size: "small", variant: "contained", color: "secondary", onClick: handleCategoriesCreation, endIcon: _jsx(Icon, { children: hover ? 'ai_stars' : 'magic_wand' }), onMouseEnter: () => setHover(true), onMouseLeave: () => setHover(false) }, { children: _jsx(FormattedMessage, { defaultMessage: "ui.onBoardingWidget.step.category.button", id: "ui.onBoardingWidget.step.category.button" }) }))) }))] })));
}
