"use strict";

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

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

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

var _Card = _interopRequireDefault(require("@mui/material/Card"));

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

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

var _Check = _interopRequireDefault(require("@mui/icons-material/Check"));

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

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

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`,
  vote: `${PREFIX}-vote`,
  voted: `${PREFIX}-voted`,
  choice: `${PREFIX}-choice`,
  result: `${PREFIX}-result`,
  progress: `${PREFIX}-progress`
};
const Root = (0, _styles.styled)(_Card.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  background: theme.palette.grey['A200'],
  marginBottom: theme.spacing(2),
  padding: theme.spacing(1),
  [`& .${classes.vote}`]: {
    backgroundColor: theme.palette.common.white,
    marginRight: theme.spacing(1)
  },
  [`& .${classes.voted}`]: {
    backgroundColor: theme.palette.common.black,
    '&:hover': {
      backgroundColor: theme.palette.common.black
    },
    marginRight: theme.spacing(1)
  },
  [`& .${classes.choice}`]: {
    display: 'inline-flex',
    margin: theme.spacing(1)
  },
  [`& .${classes.result}`]: {
    marginLeft: theme.spacing(2)
  },
  '& .MuiSvgIcon-root': {
    color: theme.palette.common.white
  },
  '& .MuiLinearProgress-bar': {
    backgroundColor: theme.palette.common.black
  },
  [`& .${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(props) {
  //PROPS
  const {
    className = null,
    choiceObj = null,
    feedObject = null,
    vote = null,
    votes = null,
    isVoting = null,
    votable = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded);

  const disabled = !feedObject;
  /**
   * 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: "outlined",
    size: "small",
    disabled: disabled || isVoting !== null || votable,
    className: choiceObj.voted ? classes.voted : classes.vote,
    onClick: () => vote(choiceObj)
  }, choiceObj.voted ? /*#__PURE__*/_react.default.createElement(_Check.default, null) : /*#__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: className
  }, rest), c);
}