"use strict";

exports.__esModule = true;
exports.default = Votes;

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

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

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

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

var _Icon = _interopRequireDefault(require("@mui/material/Icon"));

var _VotesDialog = _interopRequireDefault(require("./VotesDialog"));

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

const _excluded = ["commentObjectId", "commentObject"];

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 _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 = 'SCCommentObjectVotes';
const classes = {
  root: `${PREFIX}-root`,
  btnViewVotes: `${PREFIX}-btnViewVotes`,
  votes: `${PREFIX}-votes`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.btnViewVotes}`]: {
    minWidth: 42,
    marginTop: -14,
    right: 0,
    position: 'absolute'
  }
}));

function Votes(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    commentObjectId,
    commentObject
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // RETRIEVE OBJECTS


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

  const [openVotesDialog, setOpenVotesDialog] = (0, _react.useState)(false);
  /**
   * Open/Close dialog shares
   */

  function handleToggleSharesDialog() {
    setOpenVotesDialog(!openVotesDialog);
  }
  /**
   * Render votes
   * @return {JSX.Element}
   */


  function renderVotes() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Button, {
      variant: "text",
      size: "small",
      disabled: obj.vote_count <= 0,
      className: classes.btnViewVotes,
      onClick: handleToggleSharesDialog
    }, obj.voted ? /*#__PURE__*/_react.default.createElement(_Icon.default, {
      fontSize: "medium",
      color: "primary",
      className: classes.votes
    }, "thumb_up_alt") : /*#__PURE__*/_react.default.createElement(_Icon.default, {
      fontSize: "medium",
      sx: {
        marginTop: '-1px'
      }
    }, "thumb_up_off_alt"), /*#__PURE__*/_react.default.createElement(_material.Typography, {
      variant: 'body2',
      sx: {
        marginLeft: '5px'
      }
    }, obj.vote_count)), openVotesDialog && obj.vote_count > 0 && /*#__PURE__*/_react.default.createElement(_VotesDialog.default, {
      commentObject: obj,
      open: openVotesDialog,
      onClose: handleToggleSharesDialog
    }));
  }

  return /*#__PURE__*/_react.default.createElement(Root, rest, renderVotes());
}