import { __awaiter } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
import { Box, Button, FormControl, FormLabel, Input } from "@chakra-ui/react";
import { css } from "@emotion/react";
import { useForm } from "react-hook-form";
import { useRouter } from "next/dist/client/router";
import { QoreClient } from "@qorebase/client";
import Cookies from "js-cookie";
import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();
const BASE_URL = process.env.NODE_ENV === "development"
    ? "/api/backend"
    : publicRuntimeConfig.qoreDataConsoleEndpoint;
const Login = () => {
    const form = useForm({
        mode: "onChange",
        defaultValues: { email: "", password: "" },
    });
    const router = useRouter();
    const onSubmit = (values) => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c;
        const client = new QoreClient({
            endpoint: BASE_URL,
            getToken: () => Cookies.get("token"),
        });
        const token = yield client.authenticate(values.email, values.password);
        Cookies.set("token", token, { expires: 30 });
        const user = yield client.currentUser();
        const users = yield client
            .table("users")
            .readRows({
            condition: {
                $and: [
                    {
                        id: {
                            $eq: user.id,
                        },
                    },
                ],
            },
            populate: ["project-user_items"],
        })
            .toPromise();
        const currentUser = (_a = users.data) === null || _a === void 0 ? void 0 : _a.nodes[0];
        const projectId = ((_c = (_b = currentUser === null || currentUser === void 0 ? void 0 : currentUser["project-user_items"]) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c["project-id"]) ||
            publicRuntimeConfig.sandboxProjectID;
        router.push("/apps/project/[projectID]", `/apps/project/${projectId}`);
    });
    return (_jsx(Box, Object.assign({ css: css `
        width: 100vw;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      ` }, { children: _jsxs(Box, Object.assign({ as: "form", onSubmit: form.handleSubmit(onSubmit), width: "420px", css: css `
          border-radius: 8px;
          border: 1px solid #cecece;
          padding: 24px;
        ` }, { children: [_jsxs(FormControl, Object.assign({ id: "email" }, { children: [_jsx(FormLabel, { children: "Email" }), _jsx(Input, { name: "email", ref: form.register({ required: true }), placeholder: "Email here" })] })), _jsx("br", {}), _jsxs(FormControl, Object.assign({ id: "password" }, { children: [_jsx(FormLabel, { children: "Password" }), _jsx(Input, { name: "password", type: "password", ref: form.register({ required: true }), placeholder: "Password here" })] })), _jsx("br", {}), _jsx(Button, Object.assign({ type: "submit", disabled: form.formState.isSubmitting || !form.formState.isValid }, { children: form.formState.isSubmitting ? "Logging in" : "Login" }))] })) })));
};
// enable SSR to expose env
Login.getInitialProps = (ctx) => __awaiter(void 0, void 0, void 0, function* () { return ({}); });
export default Login;
//# sourceMappingURL=login.js.map