import { n as newMessage, g as getOneMessage, r as replyToMessage } from '../../../chunks/message_DAReJ7u_.mjs';
export { renderers } from '../../../renderers.mjs';

const prerender = false;
const POST = async ({ request, params, locals }) => {
  const data = await request.formData();
  const { type } = params;
  const name = data.get("name");
  const email = data.get("email");
  const subject = data.get("subject");
  const message = data.get("message");
  const chat_id = data.get("id");
  if (type?.toLowerCase() === "message") {
    if (!name || !email || !message || !subject) {
      return new Response(
        JSON.stringify({
          message: "Missing required fields"
        }),
        { status: 400 }
      );
    }
    if (typeof name !== "string" || typeof email !== "string" || typeof subject !== "string" || typeof message !== "string") {
      return new Response(
        JSON.stringify({
          message: "Text Inputs Only"
        }),
        { status: 400 }
      );
    }
    try {
      await newMessage({
        name,
        email,
        subject,
        message
      });
    } catch (error) {
      console.error(error);
      return new Response(JSON.stringify({ error }), { status: 500 });
    }
  }
  if (type?.toLowerCase() === "reply") {
    console.log(typeof chat_id);
    if (!message || !chat_id) {
      return new Response(
        JSON.stringify({
          message: "Missing required fields"
        }),
        { status: 400 }
      );
    }
    if (typeof chat_id !== "string" || Number(chat_id).toString().toLowerCase() === "nan" || typeof message !== "string") {
      return new Response(
        JSON.stringify({
          message: "Text Inputs Only"
        }),
        { status: 400 }
      );
    }
    try {
      const dbChat = await getOneMessage(Number(chat_id));
      if (!dbChat)
        return new Response(
          JSON.stringify({ error: "Internal Server Error" }),
          { status: 500 }
        );
      await replyToMessage({
        message,
        chatId: Number(chat_id),
        // @ts-ignore
        repliedBy: locals.user.id
      });
    } catch (error) {
      return new Response(JSON.stringify({ error }), { status: 500 });
    }
  }
  return new Response(
    JSON.stringify({
      message: "Success!"
    }),
    { status: 200 }
  );
};

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  POST,
  prerender
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
