import axios from "../../../config/axios";
import { getMailboxId } from "../../../config/func";
import {
  MessageListQueryDto,
  InboxResponse,
  MessageDetail,
  MessageDetailResponse,
} from "../types/SentTypes";

export const getSentMessages = async (
  query: MessageListQueryDto
): Promise<InboxResponse> => {
  const isSearch =
    !!query.search || !!query.datestart || query.dateend || query.from;
  const url = isSearch ? "search" : "messages/sent";

  // اضافه کردن mailbox id از localStorage
  const mailboxId = getMailboxId("Sent Mail");
  const queryWithMailbox = {
    ...query,
    mailbox: mailboxId,
  };

  const { data } = await axios.get(`/email/${url}`, {
    params: queryWithMailbox,
  });
  return data;
};

export const getSentMessageDetail = async (
  messageId: number
): Promise<MessageDetail> => {
  const { data }: { data: MessageDetailResponse } = await axios.get(
    `/email/messages/sent/${messageId}`
  );
  return data.data.message;
};

export const deleteSentMessage = async (messageId: number): Promise<void> => {
  const { data } = await axios.delete(`/email/messages/sent/${messageId}`);
  return data;
};
