import { Text, View } from 'react-native';
import { useStyles } from './style';
import { memo, useCallback, useState } from 'react';
import { usePoll } from './usePoll';
import PollOptionList from './Components/PollOptionList';
import { PollRepository } from '@amityco/ts-sdk-react-native';
const PollSection = ({
  pollId
}) => {
  const styles = useStyles();
  const [shouldFetch, setShouldFetch] = useState(false);
  const {
    data: pollData,
    endDays,
    totalVote,
    isPollClosed,
    isAlreadyVoted
  } = usePoll(pollId, shouldFetch);
  const onSubmit = useCallback(async answerIds => {
    await PollRepository.votePoll(pollId, answerIds);
    setShouldFetch(!shouldFetch);
  }, [pollId, shouldFetch]);
  if (pollData) {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.container
    }, /*#__PURE__*/React.createElement(View, {
      style: styles.rowContainer
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.pollEndDays
    }, isPollClosed ? 'Final result' : `Poll ends in ${endDays}`), /*#__PURE__*/React.createElement(Text, {
      style: styles.totalVote
    }, totalVote, " votes")), /*#__PURE__*/React.createElement(PollOptionList, {
      onSubmit: onSubmit,
      options: pollData.answers,
      answerType: pollData.answerType,
      isAlreadyVoted: isAlreadyVoted,
      isPollClosed: isPollClosed,
      totalVote: totalVote
    }));
  }
  return null;
};
export default /*#__PURE__*/memo(PollSection);
//# sourceMappingURL=PollSection.js.map