import {
  AvatarBox
} from "./chunk-CP5IL4P6.js";
import {
  isFetchError
} from "./chunk-C6SUTRP2.js";
import "./chunk-U2EI2ZLM.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-PBQ5M44A.js";
import {
  useSignInWithEmailPass
} from "./chunk-OH4AABUT.js";
import "./chunk-YKDEE7ZL.js";
import "./chunk-AEMDNTW5.js";
import "./chunk-LYD3C73Z.js";
import "./chunk-7O5NDICV.js";
import "./chunk-46M463BR.js";
import "./chunk-YFRC6GEN.js";
import "./chunk-N6SI4IIT.js";
import "./chunk-JPNPTILP.js";
import "./chunk-DAXXLQKB.js";
import "./chunk-OHOWEMHO.js";
import "./chunk-GKGLVDDV.js";
import "./chunk-WJJXUXSO.js";
import "./chunk-LAPEZATU.js";
import "./chunk-DBNPVU3L.js";
import "./chunk-U7WZYWGA.js";
import "./chunk-ZN7LWXVC.js";
import "./chunk-VYMODH6P.js";
import "./chunk-LEG67A2R.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5TRY5IMZ.js";
import "./chunk-TAKEQAZZ.js";
import "./chunk-B6FG7A6W.js";
import "./chunk-EPHVGD5Z.js";
import "./chunk-ESPLVOFC.js";
import "./chunk-FXWKGNRV.js";
import "./chunk-VDKOJ5WD.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-Z2QQZOG3.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.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" ? t("login.sessionExpired") : 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
};
