import { makeRequest } from "@frontend/kitui";
import { parseAxiosError } from "@root/utils/parse-error";

import { SendTicketMessageRequest } from "@frontend/kitui";

const apiUrl = process.env.REACT_APP_DOMAIN + "/heruvym";

export async function sendTicketMessage(
  ticketId: string,
  message: string
): Promise<[null, string?]> {
  try {
    const sendTicketMessageResponse = await makeRequest<
      SendTicketMessageRequest,
      null
    >({
      url: `${apiUrl}/send`,
      method: "POST",
      useToken: true,
      body: { ticket: ticketId, message: message, lang: "ru", files: [] },
    });

    return [sendTicketMessageResponse];
  } catch (nativeError) {
    const [error] = parseAxiosError(nativeError);

    return [null, `Не удалось отправить сообщение. ${error}`];
  }
}

export async function shownMessage(id: string): Promise<[null, string?]> {
  try {
    const shownMessageResponse = await makeRequest<{ id: string }, null>({
      url: apiUrl + "/shown",
      method: "POST",
      useToken: true,
      body: { id },
    });

    return [shownMessageResponse];
  } catch (nativeError) {
    const [error] = parseAxiosError(nativeError);

    return [null, `Не удалось прочесть сообщение. ${error}`];
  }
}
