"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 styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const CardContent_1 = tslib_1.__importDefault(require("@mui/material/CardContent"));
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const LinearProgress_1 = tslib_1.__importDefault(require("@mui/material/LinearProgress"));
const system_1 = require("@mui/system");
const Widget_1 = tslib_1.__importDefault(require("../Widget"));
const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder"));
const IncubatorSubscribeButton_1 = tslib_1.__importDefault(require("../IncubatorSubscribeButton"));
const UserDeletedSnackBar_1 = tslib_1.__importDefault(require("../../shared/UserDeletedSnackBar"));
const constants_1 = require("./constants");
const classes = {
    root: `${constants_1.PREFIX}-root`,
    name: `${constants_1.PREFIX}-name`,
    slogan: `${constants_1.PREFIX}-slogan`,
    progressBar: `${constants_1.PREFIX}-progress-bar`
};
const Root = (0, styles_1.styled)(Widget_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root'
})(() => ({}));
function LinearProgressWithLabel(_a) {
    var { className } = _a, props = tslib_1.__rest(_a, ["className"]);
    return ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: className }, { children: [(0, jsx_runtime_1.jsx)(LinearProgress_1.default, Object.assign({ variant: "determinate" }, props)), (0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ sx: { position: 'absolute', top: '3px', left: props.value + 2 + '%', transform: 'translateX(-50%)' } }, { children: props.subscribers !== 0 && ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: props.subscribers }))) })), (0, jsx_runtime_1.jsxs)(material_1.Grid, Object.assign({ container: true, spacing: 2, className: className }, { children: [(0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: true }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.incubator.progressBar.proposal", id: "ui.incubator.progressBar.proposal" }) }) })), (0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: true }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.incubator.progressBar.collectingSubscribers", id: "ui.incubator.progressBar.collectingSubscribers" }) })), (0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: true }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ align: "right" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.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
 */
function Incubator(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { incubatorId = null, incubator = null, className = null, autoHide = false, subscribeButtonProps = {}, ButtonProps = {}, detailView = false } = props, rest = tslib_1.__rest(props, ["incubatorId", "incubator", "className", "autoHide", "subscribeButtonProps", "ButtonProps", "detailView"]);
    // STATE
    const { scIncubator, setSCIncubator } = (0, react_core_1.useSCFetchIncubator)({ id: incubatorId, incubator });
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    const [openAlert, setOpenAlert] = (0, react_1.useState)(false);
    /**
     * Renders total votes
     */
    function renderVotes(voteCount, totalVotes) {
        if (totalVotes === 0) {
            return 0;
        }
        return (100 * voteCount) / totalVotes;
    }
    /**
     * Renders Incubator object
     */
    if (!scIncubator) {
        return (0, jsx_runtime_1.jsx)(Skeleton_1.default, { elevation: 0 });
    }
    /**
     * Renders root object (if not hidden by autoHide prop)
     */
    if (autoHide) {
        return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {});
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsxs)(CardContent_1.default, { children: [(0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", className: classes.name }, ButtonProps, { children: scIncubator.name })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ component: 'span' }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.incubator.proposedBy", id: "ui.incubator.proposedBy", values: {
                                        username: ((0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({}, (!scIncubator.user.deleted && { to: scRoutingContext.url(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, scIncubator.user) }), { onClick: scIncubator.user.deleted ? () => setOpenAlert(true) : null }, { children: scIncubator.user.username })))
                                    } }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ component: 'p', className: !detailView ? classes.slogan : null }, { children: scIncubator.slogan })), (0, jsx_runtime_1.jsx)(LinearProgressWithLabel, { className: classes.progressBar, value: renderVotes(scIncubator.subscribers_count, scIncubator.subscribers_threshold), subscribers: scIncubator.subscribers_count })] }), (0, jsx_runtime_1.jsx)(material_1.CardActions, { children: (0, jsx_runtime_1.jsx)(IncubatorSubscribeButton_1.default, Object.assign({ incubator: scIncubator }, subscribeButtonProps)) })] })), openAlert && (0, jsx_runtime_1.jsx)(UserDeletedSnackBar_1.default, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
exports.default = Incubator;
