"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _style = require("./style");
var _react = require("react");
var _usePoll = require("./usePoll");
var _PollOptionList = _interopRequireDefault(require("./Components/PollOptionList"));
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const PollSection = ({
  pollId
}) => {
  const styles = (0, _style.useStyles)();
  const [shouldFetch, setShouldFetch] = (0, _react.useState)(false);
  const {
    data: pollData,
    endDays,
    totalVote,
    isPollClosed,
    isAlreadyVoted
  } = (0, _usePoll.usePoll)(pollId, shouldFetch);
  const onSubmit = (0, _react.useCallback)(async answerIds => {
    await _tsSdkReactNative.PollRepository.votePoll(pollId, answerIds);
    setShouldFetch(!shouldFetch);
  }, [pollId, shouldFetch]);
  if (pollData) {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.container
    }, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.rowContainer
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.pollEndDays
    }, isPollClosed ? 'Final result' : `Poll ends in ${endDays}`), /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.totalVote
    }, totalVote, " votes")), /*#__PURE__*/React.createElement(_PollOptionList.default, {
      onSubmit: onSubmit,
      options: pollData.answers,
      answerType: pollData.answerType,
      isAlreadyVoted: isAlreadyVoted,
      isPollClosed: isPollClosed,
      totalVote: totalVote
    }));
  }
  return null;
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(PollSection);
//# sourceMappingURL=PollSection.js.map