"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const react_intl_1 = require("react-intl");
const material_1 = require("@mui/material");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const react_core_1 = require("@selfcommunity/react-core");
const types_1 = require("@selfcommunity/types");
const styles_1 = require("@mui/material/styles");
const feedObject_1 = require("../../../../types/feedObject");
const contribution_1 = require("../../../../utils/contribution");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const Skeleton_1 = tslib_1.__importDefault(require("@mui/material/Skeleton"));
const constants_1 = require("../../constants");
const messages = (0, react_intl_1.defineMessages)({
    comments: {
        id: 'ui.feedObject.comment.comments',
        defaultMessage: 'ui.feedObject.comment.comments'
    },
    comment: {
        id: 'ui.feedObject.comment.comment',
        defaultMessage: 'ui.feedObject.comment.comment'
    }
});
const classes = {
    root: `${constants_1.PREFIX}-action-comment-root`,
    divider: `${constants_1.PREFIX}-action-comment-divider`,
    inline: `${constants_1.PREFIX}-action-comment-inline`,
    button: `${constants_1.PREFIX}-action-comment-button`,
    viewAudienceButton: `${constants_1.PREFIX}-action-comment-view-audience-button`
};
const Root = (0, styles_1.styled)(material_1.Box, {
    name: constants_1.PREFIX,
    slot: 'ActionCommentRoot'
})(() => ({}));
function Comment(props) {
    // PROPS
    const { className, feedObjectId, feedObject, feedObjectType = types_1.SCContributionType.POST, feedObjectTemplate = feedObject_1.SCFeedObjectTemplateType, withAction = true, withAudience = true, inlineAction = false, onViewCommentsAction, onCommentAction } = props, rest = tslib_1.__rest(props, ["className", "feedObjectId", "feedObject", "feedObjectType", "feedObjectTemplate", "withAction", "withAudience", "inlineAction", "onViewCommentsAction", "onCommentAction"]);
    // STATE
    const { obj, setObj } = (0, react_core_1.useSCFetchFeedObject)({ id: feedObjectId, feedObject, feedObjectType });
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    // INTL
    const intl = (0, react_intl_1.useIntl)();
    /**
     * Renders comments counter
     * @return {JSX.Element}
     */
    function renderAudience() {
        let audience;
        if (withAudience) {
            if (!obj) {
                audience = ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", size: "small", disabled: true, color: "inherit" }, { children: (0, jsx_runtime_1.jsx)(Skeleton_1.default, { animation: "wave", height: 18, width: 50 }) })));
            }
            else {
                audience = ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: onViewCommentsAction ? ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", size: "small", onClick: onViewCommentsAction, color: "inherit" }, { children: `${intl.formatMessage(messages.comments, { total: obj.comment_count })}` }))) : ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: feedObjectTemplate === feedObject_1.SCFeedObjectTemplateType.DETAIL ? ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: 'body2' }, { children: `${intl.formatMessage(messages.comments, { total: obj.comment_count })}` }))) : ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", size: "small", component: react_core_1.Link, to: scRoutingContext.url((0, contribution_1.getContributionRouteName)(obj), (0, contribution_1.getRouteData)(obj)), classes: { root: classes.viewAudienceButton } }, { children: `${intl.formatMessage(messages.comments, { total: obj.comment_count })}` }))) })) }));
            }
        }
        return audience;
    }
    /**
     * Renders commentsCounter
     * @return {JSX.Element}
     */
    function renderCommentButton() {
        let ButtonProps = {};
        if (!onCommentAction) {
            ButtonProps = {
                component: react_core_1.Link,
                to: scRoutingContext.url((0, contribution_1.getContributionRouteName)(obj), (0, contribution_1.getRouteData)(obj))
            };
        }
        return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: withAction && ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [!inlineAction && withAudience && (0, jsx_runtime_1.jsx)(material_1.Divider, { className: classes.divider }), (0, jsx_runtime_1.jsx)(material_1.Tooltip, Object.assign({ title: `${intl.formatMessage(messages.comment)}` }, { children: (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ onClick: onCommentAction, className: classes.button }, ButtonProps, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "chat_bubble_outline" }) })) }))] })) }));
    }
    /**
     * Renders comment action
     */
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className, { [classes.inline]: inlineAction }) }, rest, { children: [renderAudience(), renderCommentButton()] })));
}
exports.default = Comment;
