/// <reference lib="dom" />

import { passwordSetupApi } from "./shared.bundle.ts";

const form = document.getElementById("passwordSetupForm") as HTMLFormElement;
const passwordInput = document.getElementById(
    "passwordInput",
) as HTMLInputElement;
const passwordRepeatInput = document.getElementById(
    "passwordRepeatInput",
) as HTMLInputElement;
const errDiv = document.getElementById("errDiv") as HTMLDivElement;

form.addEventListener("submit", async (e) => {
    e.preventDefault();

    const password = passwordInput.value;
    const passwordRepeat = passwordRepeatInput.value;

    const res =
        (await passwordSetupApi.makeRequest({ password, passwordRepeat }, {}))
            .flatten();

    if (res.isErr()) {
        errDiv.innerText = res.error.info;
    } else {
        window.location.href = "/login";
    }
});
