"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 pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
const react_intl_1 = require("react-intl");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const Widget_1 = tslib_1.__importDefault(require("../Widget"));
const system_1 = require("@mui/system");
const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder"));
const constants_1 = require("./constants");
const classes = {
    root: `${constants_1.PREFIX}-root`,
    buttonLoadMore: `${constants_1.PREFIX}-button-load-more`
};
const Root = (0, styles_1.styled)(Widget_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS Feed Updates Widget component. Learn about the available props and the CSS API.
 *
 *
 * This component allows the subscription to updates from PubSub channel (e.g. websocket) and tell the feed to updates if necessary.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/FeedUpdates)

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCFeedUpdatesWidget-root|Styles applied to the root element.|
 |buttonLoadMore|.SCFeedUpdatesWidget-button-load-more|Styles applied to the to load more button.|
 *
 * @param inProps
 */
function FeedUpdatesWidget(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { id = 'feed_updates', className = null, message = (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedUpdatesWidget.message", defaultMessage: "ui.feedUpdatesWidget.message" }), subscriptionChannel, subscriptionChannelUpdatesCallback = (msg, data) => true, publicationChannel = null, onHeightChange } = props, rest = tslib_1.__rest(props, ["id", "className", "message", "subscriptionChannel", "subscriptionChannelUpdatesCallback", "publicationChannel", "onHeightChange"]);
    // STATE
    const [updates, setUpdates] = (0, react_1.useState)(false);
    // REFS
    const updatesSubscription = (0, react_1.useRef)(null);
    // Subscriber for pubsub callback
    const subscriber = (msg, data) => {
        if (subscriptionChannelUpdatesCallback(msg, data)) {
            setUpdates(true);
        }
    };
    /**
     * On mount, fetches first page of notifications
     * On mount, subscribe to receive notification updates
     */
    (0, react_1.useEffect)(() => {
        updatesSubscription.current = pubsub_js_1.default.subscribe(subscriptionChannel, subscriber);
        return () => {
            pubsub_js_1.default.unsubscribe(updatesSubscription.current);
        };
    }, []);
    /**
     * Virtual Feed update
     */
    (0, react_1.useEffect)(() => {
        onHeightChange && onHeightChange();
    }, [updates]);
    if (!updates) {
        return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {});
    }
    // HANDLERS
    const handleClick = () => {
        pubsub_js_1.default.publishSync(publicationChannel, { refresh: true });
        setUpdates(false);
    };
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: (0, jsx_runtime_1.jsxs)(material_1.CardContent, { children: [message, publicationChannel && ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "text", color: "inherit", onClick: handleClick, classes: { root: classes.buttonLoadMore } }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedUpdatesWidget.reload", defaultMessage: "ui.feedUpdatesWidget.reload" }) })))] }) })));
}
exports.default = FeedUpdatesWidget;
