"use strict";

exports.__esModule = true;
exports.default = CommentObjectVotesDialog;

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

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

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

var _reactInfiniteScrollComponent = _interopRequireDefault(require("react-infinite-scroll-component"));

var _Typography = _interopRequireDefault(require("@mui/material/Typography"));

var _core = require("@selfcommunity/core");

var _Errors = require("../../../../constants/Errors");

var _BaseDialog = _interopRequireDefault(require("../../../../shared/BaseDialog"));

var _CentralProgress = _interopRequireDefault(require("../../../../shared/CentralProgress"));

var _User = _interopRequireDefault(require("../../../User"));

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

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

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; }

const PREFIX = 'SCCommentObjectVotesDialog';
const classes = {
  root: `${PREFIX}-root`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})({});

function CommentObjectVotesDialog(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });
  const {
    commentObjectId,
    commentObject,
    open = false,
    onClose
  } = props; // RETRIEVE OBJECTS

  const {
    obj,
    setObj
  } = (0, _core.useSCFetchCommentObject)({
    id: commentObjectId,
    commentObject
  }); // STATE

  const [isLoading, setIsLoading] = (0, _react.useState)(true);
  const [votes, setVotes] = (0, _react.useState)([]);
  const [next, setNext] = (0, _react.useState)(null);
  (0, _react.useEffect)(() => {
    if (obj) {
      fetchVotes();
    }
  }, [obj.id]);

  function fetchVotes() {
    setIsLoading(true);

    _core.http.request({
      url: next ? next : `${_core.Endpoints.VotesList.url({
        type: _core.SCCommentTypologyType,
        id: obj.id
      })}`,
      method: _core.Endpoints.VotesList.method
    }).then(res => {
      const data = res.data;
      setVotes([...data.results, ...votes]);
      setIsLoading(false);
      setNext(data.next !== null ? data.next : null);
    }).catch(error => {
      _core.Logger.error(_Errors.SCOPE_SC_UI, error);
    });
  }

  return /*#__PURE__*/_react.default.createElement(_BaseDialog.default, {
    title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      defaultMessage: "ui.commentObjectVotesDialog.title",
      id: "ui.commentObjectVotesDialog.title"
    }),
    onClose: onClose,
    open: open
  }, isLoading ? /*#__PURE__*/_react.default.createElement(_CentralProgress.default, {
    size: 50
  }) : /*#__PURE__*/_react.default.createElement(_reactInfiniteScrollComponent.default, {
    dataLength: votes.length,
    next: fetchVotes,
    hasMore: next !== null,
    loader: /*#__PURE__*/_react.default.createElement(_CentralProgress.default, {
      size: 30
    }),
    height: 400,
    endMessage: /*#__PURE__*/_react.default.createElement(_Typography.default, {
      variant: "body2",
      align: "center"
    }, /*#__PURE__*/_react.default.createElement("b", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.commentObjectVotesDialog.noOtherVotes",
      defaultMessage: "ui.commentObjectVotesDialog.noOtherVotes"
    })))
  }, /*#__PURE__*/_react.default.createElement(_material.List, null, votes.slice(0, 4).map((like, index) => /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    key: index
  }, /*#__PURE__*/_react.default.createElement(_User.default, {
    elevation: 0,
    user: like.user,
    key: index
  }))))));
}