import {
  AvatarBox
} from "./chunk-CP5IL4P6.js";
import {
  isFetchError
} from "./chunk-C6SUTRP2.js";
import "./chunk-U2EI2ZLM.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-3MDRW5MA.js";
import {
  useSignInWithEmailPass
} from "./chunk-MFL2TZ3W.js";
import "./chunk-YNIIYGL4.js";
import "./chunk-CBHNHLUZ.js";
import "./chunk-VME3CJER.js";
import "./chunk-XMVTX2EN.js";
import "./chunk-5PUHROK4.js";
import "./chunk-TPPONKYW.js";
import "./chunk-DF3OGJAK.js";
import "./chunk-XRDP2SYY.js";
import "./chunk-FNBKEF54.js";
import "./chunk-EHQESDH2.js";
import "./chunk-XKVXLDSJ.js";
import "./chunk-XDZGOUCR.js";
import "./chunk-K4BGK3CO.js";
import "./chunk-YYTI5JIC.js";
import "./chunk-HAFZQK53.js";
import "./chunk-IJWG755C.js";
import "./chunk-M3YP3NX4.js";
import "./chunk-UU2RQMXS.js";
import "./chunk-O4STPJR2.js";
import "./chunk-R74OHYTB.js";
import "./chunk-22XJUSMM.js";
import "./chunk-OFKUNYQJ.js";
import "./chunk-W5UPPL3W.js";
import "./chunk-LC6TM5OT.js";
import "./chunk-7SJC6BWZ.js";
import "./chunk-N4JU7GWK.js";
import "./chunk-52BRBQMY.js";
import "./chunk-BA2HXHTW.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/login/login.tsx
import { Alert, Button, Heading, Hint, Input, Text } from "@medusajs/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Trans, useTranslation } from "react-i18next";
import {
  Link,
  useLocation,
  useNavigate,
  useSearchParams
} from "react-router-dom";
import * as z from "zod";
import config from "virtual:mercur/config";
import { jsx, jsxs } from "react/jsx-runtime";
var LoginSchema = z.object({
  email: z.string().email(),
  password: z.string()
});
var Login = () => {
  const { t } = useTranslation();
  const location = useLocation();
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const reason = searchParams.get("reason") || "";
  const reasonMessage = reason && reason.toLowerCase() === "unauthorized" ? "Session expired" : reason;
  const from = location.state?.from?.pathname || "/orders";
  const form = useForm({
    resolver: zodResolver(LoginSchema),
    defaultValues: {
      email: "",
      password: ""
    }
  });
  const { mutateAsync, isPending } = useSignInWithEmailPass();
  const handleSubmit = form.handleSubmit(async ({ email, password }) => {
    await mutateAsync(
      {
        email,
        password
      },
      {
        onError: (error) => {
          if (isFetchError(error)) {
            if (error.status === 401) {
              form.setError("email", {
                type: "manual",
                message: error.message
              });
              return;
            }
          }
          form.setError("root.serverError", {
            type: "manual",
            message: error.message
          });
        },
        onSuccess: () => {
          navigate(from, { replace: true });
        }
      }
    );
  });
  const serverError = form.formState.errors?.root?.serverError?.message || reasonMessage;
  const validationError = form.formState.errors.email?.message || form.formState.errors.password?.message;
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: "flex min-h-dvh w-dvw items-center justify-center bg-ui-bg-subtle",
      "data-testid": "login-page",
      children: /* @__PURE__ */ jsxs(
        "div",
        {
          className: "m-4 flex w-full max-w-[280px] flex-col items-center",
          "data-testid": "login-container",
          children: [
            /* @__PURE__ */ jsx(AvatarBox, {}),
            /* @__PURE__ */ jsxs(
              "div",
              {
                className: "mb-4 flex flex-col items-center",
                "data-testid": "login-header",
                children: [
                  /* @__PURE__ */ jsx(Heading, { "data-testid": "login-title", children: t("login.title", { name: config.name ?? "Mercur" }) }),
                  /* @__PURE__ */ jsx(
                    Text,
                    {
                      size: "small",
                      className: "text-center text-ui-fg-subtle",
                      "data-testid": "login-hint",
                      children: t("login.hint")
                    }
                  )
                ]
              }
            ),
            /* @__PURE__ */ jsx("div", { className: "flex w-full flex-col gap-y-3", children: /* @__PURE__ */ jsx(Form, { ...form, children: /* @__PURE__ */ jsxs(
              "form",
              {
                onSubmit: handleSubmit,
                className: "flex w-full flex-col gap-y-6",
                "data-testid": "login-form",
                children: [
                  /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", children: [
                    /* @__PURE__ */ jsx(
                      Form.Field,
                      {
                        control: form.control,
                        name: "email",
                        render: ({ field }) => {
                          return /* @__PURE__ */ jsx(Form.Item, { children: /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              autoComplete: "email",
                              ...field,
                              className: "bg-ui-bg-field-component",
                              placeholder: t("fields.email"),
                              "data-testid": "login-email-input"
                            }
                          ) }) });
                        }
                      }
                    ),
                    /* @__PURE__ */ jsx(
                      Form.Field,
                      {
                        control: form.control,
                        name: "password",
                        render: ({ field }) => {
                          return /* @__PURE__ */ jsxs(Form.Item, { children: [
                            /* @__PURE__ */ jsx(Form.Label, {}),
                            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                              Input,
                              {
                                type: "password",
                                autoComplete: "current-password",
                                ...field,
                                className: "bg-ui-bg-field-component",
                                placeholder: t("fields.password"),
                                "data-testid": "login-password-input"
                              }
                            ) })
                          ] });
                        }
                      }
                    )
                  ] }),
                  validationError && /* @__PURE__ */ jsx(
                    "div",
                    {
                      className: "text-center",
                      "data-testid": "login-validation-error",
                      children: /* @__PURE__ */ jsx(
                        Hint,
                        {
                          className: "inline-flex",
                          variant: "error",
                          "data-testid": "login-validation-error-message",
                          children: validationError
                        }
                      )
                    }
                  ),
                  serverError && /* @__PURE__ */ jsx(
                    Alert,
                    {
                      className: "items-center bg-ui-bg-base p-2",
                      dismissible: true,
                      variant: "error",
                      "data-testid": "login-server-error",
                      children: serverError
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Button,
                    {
                      className: "w-full",
                      type: "submit",
                      isLoading: isPending,
                      "data-testid": "login-submit-button",
                      children: t("actions.continueWithEmail")
                    }
                  )
                ]
              }
            ) }) }),
            /* @__PURE__ */ jsx(
              "span",
              {
                className: "txt-small my-6 text-ui-fg-muted",
                "data-testid": "login-forgot-password-section",
                children: /* @__PURE__ */ jsx(
                  Trans,
                  {
                    i18nKey: "login.forgotPassword",
                    components: [
                      /* @__PURE__ */ jsx(
                        Link,
                        {
                          to: "/reset-password",
                          className: "font-medium text-ui-fg-interactive outline-none transition-fg hover:text-ui-fg-interactive-hover focus-visible:text-ui-fg-interactive-hover",
                          "data-testid": "login-reset-password-link"
                        },
                        "reset-password-link"
                      )
                    ]
                  }
                )
              }
            )
          ]
        }
      )
    }
  );
};
export {
  Login as Component
};
