import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useUpdateUser,
  useUser
} from "./chunk-GKGLVDDV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/users/user-edit/user-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/users/user-edit/components/edit-user-form/edit-user-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditUserFormSchema = zod.object({
  first_name: zod.string().optional(),
  last_name: zod.string().optional()
});
var EditUserForm = ({ user }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      first_name: user.first_name || "",
      last_name: user.last_name || ""
    },
    resolver: zodResolver(EditUserFormSchema)
  });
  const { mutateAsync, isPending } = useUpdateUser(user.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(values, {
      onSuccess: () => {
        handleSuccess();
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "user-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex max-w-full flex-1 flex-col gap-y-8 overflow-y-auto", "data-testid": "user-edit-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "first_name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "user-edit-form-first-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "user-edit-form-first-name-label", children: t("fields.firstName") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "user-edit-form-first-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "user-edit-form-first-name-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "user-edit-form-first-name-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "last_name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "user-edit-form-last-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "user-edit-form-last-name-label", children: t("fields.lastName") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "user-edit-form-last-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "user-edit-form-last-name-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "user-edit-form-last-name-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "user-edit-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "user-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "user-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/users/user-edit/user-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var UserEdit = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { user, isPending: isLoading, isError, error } = useUser(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "user-edit", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "user-edit-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "user-edit-heading", children: t("users.editUser") }) }),
    !isLoading && user && /* @__PURE__ */ jsx2(EditUserForm, { user })
  ] });
};
export {
  UserEdit as Component
};
