"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _style = require("../style");
var _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../../../core/assets/icons/xml");
var _PollBar = _interopRequireDefault(require("./PollBar"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const PollOptionList = ({
  options: data = [],
  answerType,
  isAlreadyVoted,
  isPollClosed,
  totalVote,
  onSubmit
}) => {
  var _ref;
  const styles = (0, _style.useStyles)();
  const [options, setOptions] = (0, _react.useState)(data.slice(0, 2) || []);
  const [selectedOption, setSelectedOption] = (0, _react.useState)([]);
  const hiddenOptions = (data === null || data === void 0 ? void 0 : data.length) - ((_ref = [...options]) === null || _ref === void 0 ? void 0 : _ref.length) || 0;
  const showedAllOptions = hiddenOptions === 0;
  const isSingleOption = answerType === 'single';
  (0, _react.useEffect)(() => {
    setOptions(data.slice(0, 2));
  }, [data]);
  const onPressMoreOption = (0, _react.useCallback)(() => {
    setOptions([...data]);
  }, [data]);
  const onPressOption = (0, _react.useCallback)(optionId => {
    if (isSingleOption) return setSelectedOption([optionId]);
    const currentSelectedOptions = [...selectedOption];
    const index = currentSelectedOptions.findIndex(id => id === optionId);
    if (index !== -1) {
      currentSelectedOptions.splice(index, 1);
    } else {
      currentSelectedOptions.push(optionId);
    }
    setSelectedOption([...currentSelectedOptions]);
  }, [isSingleOption, selectedOption]);
  const renderSubmitButton = (0, _react.useMemo)(() => {
    if (isPollClosed || isAlreadyVoted) return null;
    return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
      disabled: selectedOption.length === 0,
      onPress: () => onSubmit(selectedOption),
      style: styles.submitBtn
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.submit
    }, "Submit"));
  }, [isPollClosed, isAlreadyVoted, styles, onSubmit, selectedOption]);
  const renderViewAllOptions = (0, _react.useMemo)(() => {
    const resultText = isPollClosed || isAlreadyVoted ? `View full results` : `${hiddenOptions} more options`;
    return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
      onPress: onPressMoreOption,
      style: styles.moreOptionsBtn
    }, /*#__PURE__*/React.createElement(_reactNative.Text, null, resultText));
  }, [hiddenOptions, isPollClosed, isAlreadyVoted, onPressMoreOption, styles.moreOptionsBtn]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, options.map(option => {
    const selectedItem = selectedOption.includes(option.id);
    const selectedIcon = selectedItem ? (0, _xml.radioOn)() : (0, _xml.radioOff)();
    const selectedOptionContainerStyle = selectedItem && styles.selectedOptionContainer;
    const selectedVoteCountStyle = (isAlreadyVoted || isPollClosed) && option.isVotedByUser && styles.selectedVoteCount;
    const lengthPercentage = option.voteCount / totalVote * 100;
    const length = isNaN(lengthPercentage) ? 0 : lengthPercentage;
    const onResultOptionStyle = (isAlreadyVoted || isPollClosed) && option.isVotedByUser && styles.onResultOption;
    return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
      disabled: isPollClosed || isAlreadyVoted,
      key: `${option.id}${option.isVotedByUser}`,
      style: [styles.pollOptionContainer, selectedOptionContainerStyle, onResultOptionStyle],
      onPress: () => onPressOption(option.id)
    }, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.pollOptionContainerRow
    }, !isPollClosed && !isAlreadyVoted && /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
      xml: selectedIcon,
      width: "20",
      height: "20"
    }), /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.optionText
    }, option.data)), (isPollClosed || isAlreadyVoted) && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_PollBar.default, {
      myVote: option.isVotedByUser,
      length: length
    }), /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: [styles.voteCount, selectedVoteCountStyle]
    }, option.voteCount, " votes")));
  }), showedAllOptions ? renderSubmitButton : renderViewAllOptions);
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(PollOptionList);
//# sourceMappingURL=PollOptionList.js.map