"use strict";

exports.__esModule = true;
exports.default = Choice;

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

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

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

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

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

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

var _LinearProgress = _interopRequireDefault(require("@mui/material/LinearProgress"));

var _lab = require("@mui/lab");

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

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

const _excluded = ["className", "choiceObj", "feedObject", "vote", "votes", "isVoting", "votable"];

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

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

const PREFIX = 'SCChoices';
const classes = {
  root: `${PREFIX}-root`,
  voteButton: `${PREFIX}-vote-button`,
  choice: `${PREFIX}-choice`,
  progress: `${PREFIX}-progress`
};
const Root = (0, _styles.styled)(_material.Card, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  background: theme.palette.grey['A200'],
  marginBottom: theme.spacing(2),
  padding: theme.spacing(1),
  [`& .${classes.voteButton}`]: {
    marginRight: theme.spacing(1)
  },
  [`& .${classes.choice}`]: {
    display: 'inline-flex',
    margin: theme.spacing(1)
  },
  [`& .${classes.progress}`]: {
    marginLeft: theme.spacing(1),
    height: '25px',
    backgroundColor: theme.palette.grey['A200']
  }
}));

function LinearProgressWithLabel(props) {
  return /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      display: 'flex',
      alignItems: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      width: '100%',
      mr: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_LinearProgress.default, _extends({
    variant: "determinate"
  }, props))), /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      minWidth: 35
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2",
    color: "text.secondary"
  }, `${Math.round(props.value)}%`)));
}

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

  const {
    className = null,
    choiceObj = null,
    feedObject = null,
    vote = null,
    votes = null,
    isVoting = null,
    votable = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded);

  const disabled = !feedObject; // CONTEXT

  const scContext = (0, _core.useSCContext)();
  const scUserContext = (0, _core.useSCUser)();

  const handleVoteAction = () => {
    if (!scUserContext.user) {
      scContext.settings.handleAnonymousAction();
    } else {
      vote(choiceObj);
    }
  };
  /**
   * Renders total votes in percentage
   */


  function renderVotes(voteCount, totalVotes) {
    if (totalVotes === 0) {
      return 0;
    }

    return 100 * voteCount / totalVotes;
  }
  /**
   * Renders choice obj
   */


  const c = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.choice
  }, /*#__PURE__*/_react.default.createElement(_lab.LoadingButton, {
    loading: isVoting === choiceObj.id,
    variant: choiceObj.voted ? 'contained' : 'outlined',
    size: "small",
    disabled: disabled || isVoting !== null || votable,
    className: classes.voteButton,
    onClick: handleVoteAction
  }, choiceObj.voted ? /*#__PURE__*/_react.default.createElement(_Icon.default, null, "check") : /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.feedObject.poll.choice.vote",
    defaultMessage: "ui.feedObject.poll.choice.vote"
  })), /*#__PURE__*/_react.default.createElement(_material.Typography, null, choiceObj.choice)), /*#__PURE__*/_react.default.createElement(LinearProgressWithLabel, {
    className: classes.progress,
    value: renderVotes(choiceObj.vote_count, votes)
  }));
  /**
   * Renders root element
   */


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    className: (0, _classnames.default)(classes.root, className)
  }, rest), c);
}