import { fromPollServer } from "./from-poll-server.js";
import { toCreatePollPayload, toPollServer } from "./to-poll-server.js";
const BASE_URL = "/_serverless/polls-client/v1";
class PollsService {
  constructor(getWixClientPromise) {
    this.getWixClientPromise = getWixClientPromise;
    this.fetchPoll = async (pollId) => {
      try {
        const wixClient = await this.getWixClientPromise();
        const response = await wixClient.fetchWithAuth(`${BASE_URL}/questions/${pollId}`);
        const question = await response.json();
        return fromPollServer(question.question);
      } catch (e) {
        if (e?.response?.status === 404) {
          throw new Error("POLL_NOT_FOUND");
        }
        throw e;
      }
    };
    this.createPoll = async (question) => {
      const wixClient = await this.getWixClientPromise();
      const response = await wixClient.fetchWithAuth(`${BASE_URL}/questions`, {
        method: "POST",
        body: JSON.stringify({
          question: toCreatePollPayload(question)
        }),
        headers: {
          "Content-Type": "application/json"
        }
      });
      const responseQuestion = await response.json();
      return fromPollServer(responseQuestion.question);
    };
    this.updatePoll = async (question) => {
      try {
        const wixClient = await this.getWixClientPromise();
        const response = await wixClient.fetchWithAuth(`${BASE_URL}/questions/${question.id}`, {
          method: "PATCH",
          body: JSON.stringify({
            question: toPollServer(question)
          }),
          headers: {
            "Content-Type": "application/json"
          }
        });
        const responseQuestion = await response.json();
        return fromPollServer(responseQuestion.question);
      } catch (e) {
        if (e?.response?.status === 404) {
          throw new Error("POLL_NOT_FOUND");
        }
        throw e;
      }
    };
    this.vote = async (pollId, optionId) => {
      const wixClient = await this.getWixClientPromise();
      const response = await wixClient.fetchWithAuth(`${BASE_URL}/questions/${pollId}/vote`, {
        method: "POST",
        body: JSON.stringify({
          optionId
        }),
        headers: {
          "Content-Type": "application/json"
        }
      });
      const responseQuestion = await response.json();
      return fromPollServer(responseQuestion.question);
    };
    this.unvote = async (pollId, optionId) => {
      const wixClient = await this.getWixClientPromise();
      const response = await wixClient.fetchWithAuth(`${BASE_URL}/questions/${pollId}/unvote`, {
        method: "POST",
        body: JSON.stringify({
          optionId
        }),
        headers: {
          "Content-Type": "application/json"
        }
      });
      const responseQuestion = await response.json();
      return fromPollServer(responseQuestion.question);
    };
    this.getVoters = async (pollId, optionId, params) => {
      const url = `${BASE_URL}/questions/${pollId}/options/${optionId}/voters`;
      const searchParams = new URLSearchParams();
      Object.entries(params).forEach(([key, value]) => {
        if (typeof value !== "undefined") {
          searchParams.set(key, value.toString());
        }
      });
      const wixClient = await this.getWixClientPromise();
      return await wixClient.fetchWithAuth(`${url}?${searchParams.toString()}`, {
        method: "GET",
        headers: {
          "Content-Type": "application/json"
        }
      }).then((response) => response.json());
    };
    this.requestOptions = { baseURL: BASE_URL };
  }
}
export {
  PollsService
};
//# sourceMappingURL=polls-service.js.map
