"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PollResults = PollResults;
var _style = require("./style");
var _reactNative = require("react-native");
var _hooks = require("../../hooks");
var _enums = require("../../enums");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _Typography = require("../../../core/components/Typography/Typography");
var _AvatarElement = _interopRequireDefault(require("../../elements/CommonElements/AvatarElement"));
var _time = require("../../../core/utils/time");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function PollResults({
  options,
  totalVotes
}) {
  const {
    styles
  } = (0, _style.useStyles)();
  const {
    client
  } = (0, _useAuth.default)();
  const user = (0, _hooks.useUser)(client.userId);
  const maxVoteCount = Math.max(...options.map(option => option.voteCount));
  const percentage = voteCount => +Math.min(totalVotes === 0 ? 0 : voteCount / totalVotes * 100, 100).toFixed(2);
  const voteBy = option => {
    const {
      voteCount,
      isVotedByUser
    } = option;
    if (voteCount === 0) return 'No votes';
    if (isVotedByUser) {
      const otherVotes = voteCount - 1;
      if (otherVotes === 0) return 'Voted by you';
      const formattedCount = (0, _time.formatCount)(otherVotes);
      const plural = otherVotes > 1 ? 's' : '';
      return `Voted by ${formattedCount} participant${plural} and you`;
    }
    const formattedCount = (0, _time.formatCount)(voteCount);
    const plural = voteCount > 1 ? 's' : '';
    return `Voted by ${formattedCount} participant${plural}`;
  };
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.pollResults
  }, options.map(option => {
    const isHighestVote = option.voteCount === maxVoteCount && maxVoteCount > 0;
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      key: option.id,
      style: [styles.optionResult, isHighestVote && styles.topOptionResult]
    }, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.optionResultHeader
    }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
      style: styles.optionResultLabel
    }, option.data), /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
      style: [styles.optionResultPercentage, isHighestVote && styles.topOptionResultPercentage]
    }, percentage(option.voteCount), "%")), /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.optionResultVoters
    }, /*#__PURE__*/React.createElement(_Typography.Typography.Caption, {
      style: styles.baseShade2
    }, voteBy(option)), option.isVotedByUser && /*#__PURE__*/React.createElement(_AvatarElement.default, {
      targetType: "user",
      style: styles.optionAvatar,
      avatarId: user === null || user === void 0 ? void 0 : user.avatarFileId,
      avatarCustomUrl: user === null || user === void 0 ? void 0 : user.avatarCustomUrl,
      elementID: _enums.ElementID.WildCardElement,
      displayName: user === null || user === void 0 ? void 0 : user.displayName
    })), /*#__PURE__*/React.createElement(_reactNative.View, {
      style: [styles.optionResultProgressBar, isHighestVote && styles.topOptionResultProgressBar]
    }, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: [styles.optionResultProgressBarLength, isHighestVote && styles.topOptionResultProgressBarLength, {
        width: `${percentage(option.voteCount)}%`
      }]
    })));
  }));
}
//# sourceMappingURL=PollResults.js.map