import { useEffect, useMemo, useState } from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { PollRepository } from '@amityco/ts-sdk-react-native';
dayjs.extend(utc);
export const usePoll = (pollId, shouldFetch) => {
  const [pollData, setPollData] = useState(undefined);
  const endDays = useMemo(() => {
    const now = dayjs().utc();
    if (pollData !== null && pollData !== void 0 && pollData.closedAt) {
      const endDate = dayjs(pollData.closedAt);
      const hoursRemaining = endDate.diff(now, 'hour');
      const daysRemaining = Math.ceil(hoursRemaining / 24);
      const dayText = daysRemaining > 1 ? 'days' : 'day';
      return `${daysRemaining} ${dayText}`;
    }
    return null;
  }, [pollData]);
  const isPollClosed = useMemo(() => {
    if (pollData !== null && pollData !== void 0 && pollData.status) {
      return pollData.status === 'closed';
    }
    return false;
  }, [pollData]);
  const totalVote = useMemo(() => {
    var _pollData$answers;
    if ((pollData === null || pollData === void 0 || (_pollData$answers = pollData.answers) === null || _pollData$answers === void 0 ? void 0 : _pollData$answers.length) > 0) {
      const total = pollData.answers.reduce((acc, poll) => {
        return acc + poll.voteCount;
      }, 0);
      return total;
    } else {
      return 0;
    }
  }, [pollData]);
  const isAlreadyVoted = useMemo(() => {
    if (pollData !== null && pollData !== void 0 && pollData.answers) {
      return pollData.answers.some(answer => answer.isVotedByUser);
    }
    return false;
  }, [pollData]);
  useEffect(() => {
    (async () => {
      try {
        PollRepository.getPoll(pollId, ({
          data
        }) => setPollData(data));
      } catch (error) {
        console.log(error);
      }
    })();
  }, [pollId, shouldFetch]);
  return {
    data: pollData,
    endDays,
    totalVote,
    isPollClosed,
    isAlreadyVoted
  };
};
//# sourceMappingURL=usePoll.js.map