"use strict";

exports.__esModule = true;
exports.default = SharesDialog;

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"));

const _excluded = ["id", "feedObject", "feedObjectType", "open", "onClose"];

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

function SharesDialog(props) {
  // PROPS
  const {
    id = null,
    feedObject = null,
    feedObjectType = _core.SCFeedObjectTypologyType.POST,
    open = false,
    onClose = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const {
    obj,
    setObj
  } = (0, _core.useSCFetchFeedObject)({
    id,
    feedObject,
    feedObjectType
  });
  const [isLoading, setIsLoading] = (0, _react.useState)(Boolean(id && feedObjectType || feedObject));
  const [shares, setShares] = (0, _react.useState)([]);
  const [next, setNext] = (0, _react.useState)(id && feedObjectType ? `${_core.Endpoints.ShareUsersList.url({
    type: feedObjectType,
    id: id
  })}` : feedObject && feedObjectType ? `${_core.Endpoints.ShareUsersList.url({
    type: feedObjectType,
    id: feedObject.id
  })}` : null);
  /**
   * On mount, fetches shares
   */

  (0, _react.useEffect)(() => {
    if (obj && next) {
      fetchShares();
    }
  }, [`${obj}`]);
  /**
   * Fetches shares
   */

  function fetchShares() {
    setIsLoading(true);

    _core.http.request({
      url: next,
      method: _core.Endpoints.ShareUsersList.method
    }).then(res => {
      const data = res.data;
      setShares([...shares, ...data.results]);
      setIsLoading(false);
      setNext(data.next !== null ? data.next : null);
    }).catch(error => {
      _core.Logger.error(_Errors.SCOPE_SC_UI, error);
    });
  }
  /**
   * Renders shares dialog
   */


  return /*#__PURE__*/_react.default.createElement(_BaseDialog.default, _extends({
    title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      defaultMessage: "ui.feedObject.sharesDialog.title",
      id: "ui.feedObject.sharesDialog.title"
    }),
    onClose: onClose,
    open: open
  }, rest), isLoading ? /*#__PURE__*/_react.default.createElement(_CentralProgress.default, {
    size: 50
  }) : /*#__PURE__*/_react.default.createElement(_reactInfiniteScrollComponent.default, {
    dataLength: shares.length,
    next: fetchShares,
    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.feedObject.sharesDialog.noOtherLikes",
      defaultMessage: "ui.feedObject.sharesDialog.noOtherLikes"
    })))
  }, /*#__PURE__*/_react.default.createElement(_material.List, null, shares.slice(0, 4).map((user, index) => /*#__PURE__*/_react.default.createElement(_material.ListItem, {
    key: user.id
  }, /*#__PURE__*/_react.default.createElement(_User.default, {
    elevation: 0,
    user: user,
    key: index
  }))))));
}