"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 utils_1 = require("@selfcommunity/utils");
const react_core_1 = require("@selfcommunity/react-core");
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const BaseDialog_1 = tslib_1.__importDefault(require("../../../shared/BaseDialog"));
const CentralProgress_1 = tslib_1.__importDefault(require("../../../shared/CentralProgress"));
const InfiniteScroll_1 = tslib_1.__importDefault(require("../../../shared/InfiniteScroll"));
const User_1 = tslib_1.__importDefault(require("../../User"));
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const constants_1 = require("../constants");
const classes = {
    root: `${constants_1.PREFIX}-contributors-root`,
    avatarGroup: `${constants_1.PREFIX}-contributors-avatarGroup`,
    btnParticipants: `${constants_1.PREFIX}-contributors-btn-participants`
};
const Root = (0, styles_1.styled)(material_1.Box, {
    name: constants_1.PREFIX,
    slot: 'ContributorsRoot'
})(() => ({}));
function ContributorsFeedObject(props) {
    // PROPS
    const { className = null, feedObjectId = null, feedObject = null, feedObjectType = null, ContributorsSkeletonProps = {}, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST } = props, rest = tslib_1.__rest(props, ["className", "feedObjectId", "feedObject", "feedObjectType", "ContributorsSkeletonProps", "cacheStrategy"]);
    const contributorsObject = (0, react_core_1.useSCFetchContributors)({
        id: feedObjectId,
        feedObject,
        feedObjectType,
        pageSize: 10,
        cacheStrategy
    });
    // STATE
    const [openContributorsDialog, setOpenContributorsDialog] = (0, react_1.useState)(false);
    /**
     * On mount fetches contributors
     */
    (0, react_1.useEffect)(() => {
        if (contributorsObject.feedObject) {
            contributorsObject.getNextPage();
        }
    }, [contributorsObject.feedObject]);
    /**
     * Renders root object (if obj)
     */
    if (!contributorsObject.feedObject) {
        return null;
    }
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: (0, jsx_runtime_1.jsx)(material_1.Box, { children: contributorsObject.isLoadingNext && !openContributorsDialog ? ((0, jsx_runtime_1.jsx)(Skeleton_1.default, Object.assign({}, ContributorsSkeletonProps))) : ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: contributorsObject.contributors.length > 0 ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(material_1.Button, Object.assign({ variant: "text", onClick: () => setOpenContributorsDialog(true), classes: { root: classes.btnParticipants }, color: "inherit" }, { children: [(0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: 'ui.feedObject.contributors.participants', defaultMessage: 'ui.feedObject.contributors.participants' }), ":", (0, jsx_runtime_1.jsxs)(material_1.AvatarGroup, Object.assign({}, rest, { children: [contributorsObject.contributors.map((c, i) => ((0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: c.username, src: c.avatar }, i))), [...Array(Math.max(contributorsObject.total - contributorsObject.contributors.length, 0))].map((x, i) => ((0, jsx_runtime_1.jsx)(material_1.Avatar, {}, i)))] }))] })), openContributorsDialog && ((0, jsx_runtime_1.jsx)(BaseDialog_1.default, Object.assign({ title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.feedObject.contributors.title", id: "ui.feedObject.contributors.title", values: { total: contributorsObject.total } }), onClose: () => setOpenContributorsDialog(false), open: openContributorsDialog }, { children: contributorsObject.isLoadingNext ? ((0, jsx_runtime_1.jsx)(CentralProgress_1.default, { size: 50 })) : ((0, jsx_runtime_1.jsx)(InfiniteScroll_1.default, Object.assign({ dataLength: contributorsObject.contributors.length, next: contributorsObject.getNextPage(), hasMoreNext: Boolean(contributorsObject.next), loaderNext: (0, jsx_runtime_1.jsx)(CentralProgress_1.default, { size: 30 }), height: 400, endMessage: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", align: "center", fontWeight: "bold" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedObject.contributors.noOtherContributors", defaultMessage: "ui.feedObject.contributors.noOtherContributors" }) })) }, { children: (0, jsx_runtime_1.jsx)(material_1.List, { children: contributorsObject.contributors.map((c, i) => ((0, jsx_runtime_1.jsx)(material_1.ListItem, { children: (0, jsx_runtime_1.jsx)(User_1.default, { elevation: 0, user: c, sx: { m: 0 }, onClick: () => setOpenContributorsDialog(false) }, c.id) }, i))) }) }))) })))] })) : null })) }) })));
}
exports.default = ContributorsFeedObject;
