"use strict";

exports.__esModule = true;
exports.default = FeedUpdates;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@mui/material/styles");

var _material = require("@mui/material");

var _pubsubJs = _interopRequireDefault(require("pubsub-js"));

var _reactIntl = require("react-intl");

var _classnames = _interopRequireDefault(require("classnames"));

var _Widget = _interopRequireDefault(require("../Widget"));

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

const _excluded = ["id", "className", "message", "subscriptionChannel", "subscriptionChannelUpdatesCallback", "publicationChannel"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const PREFIX = 'SCFeedUpdates';
const classes = {
  root: `${PREFIX}-root`,
  image: `${PREFIX}-image`
};
const Root = (0, _styles.styled)(_Widget.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  marginBottom: theme.spacing(2),
  '& div:last-child': {
    paddingBottom: theme.spacing(2)
  }
}));

/**
 * > API documentation for the Community-UI FeedUpdates component. Learn about the available props and the CSS API.
 * This Component subscribe to updates from PubSub channel (eg. websocket) and tell the feed to updates if necessary.

 #### Import

 ```jsx
 import {FeedUpdates} from '@selfcommunity/ui';
 ```

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCFeedUpdates-root|Styles applied to the root element.|
 |image|.SCFeedUpdates-image|Styles applied to the image element.|
 *
 * @param inProps
 */
function FeedUpdates(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    id = 'feed_updates',
    className = null,
    message = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.feedUpdates.message",
      defaultMessage: "ui.feedUpdates.message"
    }),
    subscriptionChannel,
    subscriptionChannelUpdatesCallback = (msg, data) => true,
    publicationChannel = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [updates, setUpdates] = (0, _react.useState)(false); // REFS

  const updatesSubscription = (0, _react.useRef)(null); // Subscripber 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.useEffect)(() => {
    updatesSubscription.current = _pubsubJs.default.subscribe(subscriptionChannel, subscriber);
    return () => {
      _pubsubJs.default.unsubscribe(updatesSubscription.current);
    };
  }, []);

  if (!updates) {
    return null;
  } // HANDLERS


  const handleClick = () => {
    _pubsubJs.default.publishSync(publicationChannel, {
      refresh: true
    });

    setUpdates(false);
  };

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: (0, _classnames.default)(classes.root, className)
  }, rest), /*#__PURE__*/_react.default.createElement(_material.CardContent, null, message, publicationChannel && /*#__PURE__*/_react.default.createElement(_material.Button, {
    onClick: handleClick
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.feedUpdates.reload",
    defaultMessage: "ui.feedUpdates.reload"
  }))));
}