"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 lab_1 = require("@mui/lab");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const material_1 = require("@mui/material");
const react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const notistack_1 = require("notistack");
const errors_1 = require("../../utils/errors");
const PREFIX = 'SCVoteButton';
const classes = {
    root: `${PREFIX}-root`,
    voted: `${PREFIX}-voted`,
    popperRoot: `${PREFIX}-popper-root`,
    reactionList: `${PREFIX}-reaction-list`,
    reaction: `${PREFIX}-reaction`
};
const Root = (0, styles_1.styled)(lab_1.LoadingButton, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => [styles.root, styles.voted]
})(({ theme }) => ({}));
const PopperRoot = (0, styles_1.styled)(material_1.Popper, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.popperRoot
})(() => ({}));
/**
 * > API documentation for the Community-JS Vote Button component. Learn about the available props and the CSS API.

 #### Import

 ```jsx
 import {VoteAudienceButton} from '@selfcommunity/react-ui';
 ```

 #### Component Name

 The name `SCVoteButton` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCVoteButton-root|Styles applied to the root element.|
 |voted|.SCVoteButton-voted|Styles applied to the root element when the user has vote the contribution.|

 * @param inProps
 */
function VoteButton(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, contributionId, contributionType, contribution = null, onVote } = props, rest = tslib_1.__rest(props, ["className", "contributionId", "contributionType", "contribution", "onVote"]);
    // STATE
    const [anchorEl, setAnchorEl] = (0, react_1.useState)(null);
    // REF
    const timeoutRef = (0, react_1.useRef)(null);
    // CONTEXT
    const scContext = (0, react_core_1.useSCContext)();
    const scUserContext = (0, react_core_1.useSCUser)();
    const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
    // HANDLERS
    const handleMouseEnter = (event) => {
        handleClearTimeout();
        timeoutRef.current = setTimeout(() => setAnchorEl(event.target), 1000);
    };
    const handleMouseLeave = (event) => {
        handleClearTimeout();
        timeoutRef.current = setTimeout(() => setAnchorEl(null), 500);
    };
    const handleClearTimeout = () => {
        timeoutRef.current && clearTimeout(timeoutRef.current);
        timeoutRef.current = null;
    };
    /**
     * Perform vote action
     * @param reaction
     */
    const handleVoteAction = (reaction) => {
        if (!scUserContext.user) {
            scContext.settings.handleAnonymousAction();
        }
        else if (react_core_1.UserUtils.isBlocked(scUserContext.user)) {
            enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.userBlocked", defaultMessage: "ui.common.userBlocked" }), {
                variant: 'warning',
                autoHideDuration: 3000
            });
        }
        else {
            handleVote(reaction);
        }
    };
    /**
     * Handle callback onVote
     * @param contribution
     * @param error
     */
    const handleOnVote = (contribution, error) => {
        setAnchorEl(null);
        if (error) {
            if (!(0, errors_1.catchUnauthorizedActionByBlockedUser)(error, scUserContext.managers.blockedUsers.isBlocked(contribution.author), enqueueSnackbar)) {
                enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.error.action", defaultMessage: "ui.common.error.action" }), {
                    variant: 'error',
                    autoHideDuration: 3000
                });
            }
        }
        else {
            onVote && onVote(contribution);
        }
    };
    // HOOKS
    const { isLoading, isVoting, contributionVoted, contributionReaction, reactions, handleVote, error } = (0, react_core_1.useSCFetchVote)({
        id: contributionId,
        contributionType,
        contribution,
        onVote: handleOnVote
    });
    const theme = (0, material_1.useTheme)();
    const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md'));
    // MEMO
    const rootProps = (0, react_1.useMemo)(() => {
        if (!reactions.default) {
            return {};
        }
        return {
            onMouseEnter: handleMouseEnter,
            onMouseLeave: handleMouseLeave
        };
    }, [reactions]);
    // RENDER
    const button = ((0, jsx_runtime_1.jsx)(Root, Object.assign({ onClick: isMobile && reactions.reactions
            ? handleMouseEnter
            : () => handleVoteAction(contributionReaction ? contributionReaction : reactions.default ? reactions.default : null), disabled: isLoading || Boolean(error), loading: isVoting, className: (0, classnames_1.default)(classes.root, className, {
            [classes.voted]: scUserContext.user && contributionVoted
        }) }, rest, rootProps, { children: scUserContext.user && contributionVoted ? (contributionReaction ? ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: (0, jsx_runtime_1.jsx)("img", { alt: contributionReaction.label, src: contributionReaction.image, width: "100%", height: "100%" }) })) : ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: "thumb_up" }))) : ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: "thumb_up_off_alt" })) })));
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [reactions.default ? (button) : ((0, jsx_runtime_1.jsx)(material_1.Tooltip, Object.assign({ title: contributionVoted ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteButton.voteDown", defaultMessage: "ui.voteButton.voteDown" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteButton.voteUp", defaultMessage: "ui.voteButton.voteUp" })) }, { children: (0, jsx_runtime_1.jsx)("span", { children: button }) }))), reactions.default && ((0, jsx_runtime_1.jsx)(PopperRoot, Object.assign({ id: `vote_${contributionId}_${contributionType}_popper`, className: classes.popperRoot, open: Boolean(anchorEl) && !isVoting && !isLoading, anchorEl: anchorEl, placement: "top", keepMounted: true }, { children: (0, jsx_runtime_1.jsx)(material_1.Paper, Object.assign({ className: classes.reactionList, onMouseEnter: handleClearTimeout, onMouseLeave: handleMouseLeave }, { children: reactions.reactions
                        .filter((reaction) => !contributionVoted || (contributionVoted && contributionReaction.id !== reaction.id) ? reaction.active : reaction)
                        .map((reaction) => ((0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ className: classes.reaction, onClick: () => handleVoteAction(reaction) }, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: (0, jsx_runtime_1.jsx)("img", { alt: reaction.label, src: reaction.image, width: "100%", height: "100%" }) }) }), reaction.id))) })) })))] }));
}
exports.default = VoteButton;
