import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import { Typography, Grid, Box, Button, CardActions } from '@mui/material';
import { Link, SCRoutes, useSCFetchIncubator, useSCRouting } from '@selfcommunity/react-core';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import CardContent from '@mui/material/CardContent';
import Skeleton from './Skeleton';
import LinearProgress from '@mui/material/LinearProgress';
import { useThemeProps } from '@mui/system';
import Widget from '../Widget';
import HiddenPlaceholder from '../../shared/HiddenPlaceholder';
import IncubatorSubscribeButton from '../IncubatorSubscribeButton';
import UserDeletedSnackBar from '../../shared/UserDeletedSnackBar';
import { PREFIX } from './constants';
const classes = {
    root: `${PREFIX}-root`,
    name: `${PREFIX}-name`,
    slogan: `${PREFIX}-slogan`,
    progressBar: `${PREFIX}-progress-bar`
};
const Root = styled(Widget, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
function LinearProgressWithLabel(_a) {
    var { className } = _a, props = __rest(_a, ["className"]);
    return (_jsxs(Box, Object.assign({ className: className }, { children: [_jsx(LinearProgress, Object.assign({ variant: "determinate" }, props)), _jsx(Box, Object.assign({ sx: { position: 'absolute', top: '3px', left: props.value + 2 + '%', transform: 'translateX(-50%)' } }, { children: props.subscribers !== 0 && (_jsx(Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: props.subscribers }))) })), _jsxs(Grid, Object.assign({ container: true, spacing: 2, className: className }, { children: [_jsx(Grid, Object.assign({ item: true, xs: true }, { children: _jsx(Typography, { children: _jsx(FormattedMessage, { defaultMessage: "ui.incubator.progressBar.proposal", id: "ui.incubator.progressBar.proposal" }) }) })), _jsx(Grid, Object.assign({ item: true, xs: true }, { children: _jsx(FormattedMessage, { defaultMessage: "ui.incubator.progressBar.collectingSubscribers", id: "ui.incubator.progressBar.collectingSubscribers" }) })), _jsx(Grid, Object.assign({ item: true, xs: true }, { children: _jsx(Typography, Object.assign({ align: "right" }, { children: _jsx(FormattedMessage, { defaultMessage: "ui.incubator.progressBar.approved", id: "ui.incubator.progressBar.approved" }) })) }))] }))] })));
}
/**
 * > API documentation for the Community-JS Incubator component. Learn about the available props and the CSS API.
 *
 *
 * This component renders an incubator item.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/Incubator)

 #### Import
 ```jsx
 import {Incubator} from '@selfcommunity/react-ui';
 ```
 #### Component Name
 The name `SCIncubator` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCIncubator-root|Styles applied to the root element.|
 |name|.SCIncubator-name|Styles applied to the name section.|
 |slogan|.SCIncubator-slogan|Styles applied to the slogan section.|
 |progressBar|.SCIncubator-progress-bar|Styles applied to the progress bar element.|

 * @param inProps
 */
export default function Incubator(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { incubatorId = null, incubator = null, className = null, autoHide = false, subscribeButtonProps = {}, ButtonProps = {}, detailView = false } = props, rest = __rest(props, ["incubatorId", "incubator", "className", "autoHide", "subscribeButtonProps", "ButtonProps", "detailView"]);
    // STATE
    const { scIncubator, setSCIncubator } = useSCFetchIncubator({ id: incubatorId, incubator });
    // CONTEXT
    const scRoutingContext = useSCRouting();
    const [openAlert, setOpenAlert] = useState(false);
    /**
     * Renders total votes
     */
    function renderVotes(voteCount, totalVotes) {
        if (totalVotes === 0) {
            return 0;
        }
        return (100 * voteCount) / totalVotes;
    }
    /**
     * Renders Incubator object
     */
    if (!scIncubator) {
        return _jsx(Skeleton, { elevation: 0 });
    }
    /**
     * Renders root object (if not hidden by autoHide prop)
     */
    if (autoHide) {
        return _jsx(HiddenPlaceholder, {});
    }
    return (_jsxs(_Fragment, { children: [_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsxs(CardContent, { children: [_jsx(Button, Object.assign({ variant: "text", className: classes.name }, ButtonProps, { children: scIncubator.name })), _jsx(Typography, Object.assign({ component: 'span' }, { children: _jsx(FormattedMessage, { defaultMessage: "ui.incubator.proposedBy", id: "ui.incubator.proposedBy", values: {
                                        username: (_jsx(Link, Object.assign({}, (!scIncubator.user.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, scIncubator.user) }), { onClick: scIncubator.user.deleted ? () => setOpenAlert(true) : null }, { children: scIncubator.user.username })))
                                    } }) })), _jsx(Typography, Object.assign({ component: 'p', className: !detailView ? classes.slogan : null }, { children: scIncubator.slogan })), _jsx(LinearProgressWithLabel, { className: classes.progressBar, value: renderVotes(scIncubator.subscribers_count, scIncubator.subscribers_threshold), subscribers: scIncubator.subscribers_count })] }), _jsx(CardActions, { children: _jsx(IncubatorSubscribeButton, Object.assign({ incubator: scIncubator }, subscribeButtonProps)) })] })), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
