import {
  AuthLayout
} from "./chunk-QEFPTNPC.js";
import {
  AvatarBox
} from "./chunk-CP5IL4P6.js";
import {
  isFetchError
} from "./chunk-C6SUTRP2.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  require_dist
} from "./chunk-52KJSIHS.js";
import {
  useFeatureFlags
} from "./chunk-RLRWDHME.js";
import {
  useSignInWithEmailPass
} from "./chunk-K5324KIX.js";
import {
  __toESM
} from "./chunk-NBMM2TZK.js";

// src/pages/login/login.tsx
var import_types = __toESM(require_dist(), 1);
import { Children } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import i18n from "i18next";
import { Alert, Button, Heading, Input, Text } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { Trans, useTranslation } from "react-i18next";
import { Link, useNavigate, useSearchParams } from "react-router-dom";
import * as z from "zod";
import { WidgetZone } from "@mercurjs/dashboard-shared";
import config from "virtual:mercur/config";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var LoginSchema = z.object({
  email: z.string().min(1, { message: i18n.t("login.validation.emailRequired") }).email({ message: i18n.t("login.validation.emailInvalid") }),
  password: z.string().min(1, { message: i18n.t("login.validation.passwordRequired") })
});
var LoginLogo = () => {
  return /* @__PURE__ */ jsx(WidgetZone, { id: "login.logo", children: /* @__PURE__ */ jsx(AvatarBox, {}) });
};
var LoginHeader = () => {
  const { t } = useTranslation();
  return /* @__PURE__ */ jsxs("div", { className: "mb-6 flex flex-col", children: [
    /* @__PURE__ */ jsx(Heading, { children: t("login.title", { name: config.name ?? "Mercur" }) }),
    /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: t("login.hint") })
  ] });
};
var LoginForm = () => {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const reason = searchParams.get("reason") || "";
  const reasonMessage = reason && reason.toLowerCase() === "unauthorized" ? t("login.sessionExpired") : reason;
  const form = useForm({
    resolver: zodResolver(LoginSchema),
    mode: "onSubmit",
    reValidateMode: "onSubmit",
    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: () => {
          const email2 = form.getValues("email");
          setTimeout(() => {
            navigate("/store-select", {
              replace: true,
              state: { email: email2 }
            });
          }, 1e3);
        }
      }
    );
  });
  const serverError = form.formState.errors?.root?.serverError?.message || reasonMessage;
  return /* @__PURE__ */ jsx(Form, { ...form, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: "flex w-full flex-col gap-y-6", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "email",
          render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
            /* @__PURE__ */ jsx(Form.Label, { children: t("fields.email") }),
            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { autoComplete: "email", ...field }) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] })
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "password",
          render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
            /* @__PURE__ */ jsx(Form.Label, { children: t("fields.password") }),
            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
              Input,
              {
                type: "password",
                autoComplete: "current-password",
                ...field
              }
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] })
        }
      ),
      serverError && /* @__PURE__ */ jsx(
        Alert,
        {
          className: "bg-ui-bg-base items-center p-2",
          dismissible: true,
          variant: "error",
          children: serverError
        }
      )
    ] }),
    /* @__PURE__ */ jsx(Button, { className: "w-full", type: "submit", isLoading: isPending, children: t("login.submit") })
  ] }) });
};
var LoginFooter = () => {
  const { t } = useTranslation();
  const { feature_flags } = useFeatureFlags();
  return /* @__PURE__ */ jsxs("div", { className: "mt-auto flex flex-col gap-y-2", children: [
    /* @__PURE__ */ jsx("span", { className: "text-ui-fg-muted txt-small", children: /* @__PURE__ */ jsx(
      Trans,
      {
        t,
        i18nKey: "login.forgotPassword",
        components: [
          /* @__PURE__ */ jsx(
            Link,
            {
              to: "/reset-password",
              className: "text-ui-fg-interactive transition-fg hover:text-ui-fg-interactive-hover focus-visible:text-ui-fg-interactive-hover font-medium outline-none"
            },
            "reset-password-link"
          )
        ]
      }
    ) }),
    feature_flags?.[import_types.MercurFeatureFlags.SELLER_REGISTRATION] && /* @__PURE__ */ jsx("span", { className: "text-ui-fg-muted txt-small", children: /* @__PURE__ */ jsx(
      Trans,
      {
        t,
        i18nKey: "login.notSellerYet",
        components: [
          /* @__PURE__ */ jsx(
            Link,
            {
              to: "/register",
              className: "text-ui-fg-interactive transition-fg hover:text-ui-fg-interactive-hover focus-visible:text-ui-fg-interactive-hover font-medium outline-none"
            },
            "register-link"
          )
        ]
      }
    ) })
  ] });
};
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx(AuthLayout, { children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(LoginLogo, {}),
    /* @__PURE__ */ jsx(WidgetZone, { id: "login.before" }),
    /* @__PURE__ */ jsxs("div", { className: "mt-6", children: [
      /* @__PURE__ */ jsx(LoginHeader, {}),
      /* @__PURE__ */ jsx(LoginForm, {})
    ] }),
    /* @__PURE__ */ jsx(WidgetZone, { id: "login.after" }),
    /* @__PURE__ */ jsx(LoginFooter, {})
  ] }) });
};
var LoginPage = Object.assign(Root, {
  Logo: LoginLogo,
  Header: LoginHeader,
  Form: LoginForm,
  Footer: LoginFooter
});

export {
  LoginPage
};
