"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
document.addEventListener("DOMContentLoaded", () => {
    const form = document.getElementById("loginForm");
    const message = document.getElementById("message");
    form.addEventListener("submit", (e) => __awaiter(void 0, void 0, void 0, function* () {
        e.preventDefault();
        message.textContent = "";
        const email = document.getElementById("email").value;
        const password = document.getElementById("password").value;
        try {
            const response = yield fetch("/api/auth/login", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({ email, password }),
            });
            const data = yield response.json();
            if (!response.ok) {
                message.textContent = data.message || "Login failed.";
                return;
            }
            document.cookie = `token=${data.token}; path=/; SameSite=Lax; Secure`;
            message.style.color = "green";
            message.textContent = "Login successful!";
            window.location.href = "/index.html";
        }
        catch (error) {
            message.textContent = "Something went wrong.";
            console.error(error);
        }
    }));
});
