"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PollOptions = PollOptions;
var _Radio = require("../../../core/components/Radio");
var _style = require("./style");
var _CheckBox = require("../../../core/components/CheckBox");
var _PollResults = require("./PollResults");
var _Typography = require("../../../core/components/Typography/Typography");
var _reactNative = require("react-native");
var _react = require("react");
var _Button = _interopRequireWildcard(require("../Button/Button"));
var _native = require("@react-navigation/native");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function PollOptions({
  post,
  options,
  votePoll,
  totalVotes,
  answerType,
  isPollClosed,
  isAlreadyVoted,
  showedAllOptions,
  disabledPoll,
  isAuthorSeeingResults
}) {
  const DEFAULT_DISPLAY_OPTIONS = 4;
  const isSingleOption = answerType === 'single';
  const hiddenOptions = options.length - DEFAULT_DISPLAY_OPTIONS;
  const navigation = (0, _native.useNavigation)();
  const {
    styles
  } = (0, _style.useStyles)();
  const [selectedOption, setSelectedOption] = (0, _react.useState)([]);
  const btnDisabled = selectedOption.length === 0;
  const optionDisabled = !!disabledPoll;
  return /*#__PURE__*/React.createElement(_reactNative.View, null, isPollClosed || isAlreadyVoted || isAuthorSeeingResults ? /*#__PURE__*/React.createElement(_PollResults.PollResults, {
    options: [...options].sort((a, b) => b.voteCount - a.voteCount).slice(0, showedAllOptions ? options.length : 4),
    totalVotes: totalVotes
  }) : isSingleOption ? /*#__PURE__*/React.createElement(_Radio.Radio.Group, {
    value: selectedOption[0],
    style: styles.optionGroup,
    disabled: optionDisabled,
    onChange: value => setSelectedOption([value]),
    select: (value, option) => value.id === option.id
  }, options.slice(0, showedAllOptions ? options.length : DEFAULT_DISPLAY_OPTIONS).map(option => /*#__PURE__*/React.createElement(_Radio.Radio.Option, {
    value: option,
    key: option.id,
    style: ({
      selected
    }) => [styles.option, selected && styles.optionSelected]
  }, /*#__PURE__*/React.createElement(_Radio.Radio.Label, {
    style: styles.optionLabel
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    style: [optionDisabled && styles.optionLabelDisabled]
  }, option.data)), /*#__PURE__*/React.createElement(_Radio.Radio.Icon, null)))) : /*#__PURE__*/React.createElement(_CheckBox.CheckBox.Group, {
    value: selectedOption,
    style: styles.optionGroup,
    disabled: optionDisabled,
    onChange: value => setSelectedOption(value),
    select: (value, option) => !!value.find(item => item.id === option.id),
    extractKey: value => value.id
  }, options.slice(0, showedAllOptions ? options.length : DEFAULT_DISPLAY_OPTIONS).map(option => /*#__PURE__*/React.createElement(_CheckBox.CheckBox.Option, {
    value: option,
    key: option.id,
    style: ({
      selected
    }) => [styles.option, selected && styles.optionSelected]
  }, /*#__PURE__*/React.createElement(_CheckBox.CheckBox.Label, {
    style: styles.optionLabel
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    style: [optionDisabled && styles.optionLabelDisabled]
  }, option.data)), /*#__PURE__*/React.createElement(_CheckBox.CheckBox.Icon, null)))), !showedAllOptions && hiddenOptions > 0 && /*#__PURE__*/React.createElement(_Button.default, {
    type: "secondary",
    size: _Button.BUTTON_SIZE.LARGE,
    style: styles.seeMoreOptionsBtn,
    onPress: () => navigation.navigate('PostDetail', {
      postId: post.postId
    })
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, null, isPollClosed || isAlreadyVoted ? `See full results` : `See ${hiddenOptions} more options`)), !isPollClosed && !isAlreadyVoted && /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    disabled: btnDisabled || optionDisabled,
    style: [styles.voteBtn, (btnDisabled || optionDisabled) && styles.voteBtnDisabled],
    onPress: () => votePoll(selectedOption.map(option => option.id))
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    style: styles.voteBtnLabel
  }, "Vote")));
}
//# sourceMappingURL=PollOption.js.map