import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  ConditionalTooltip
} from "./chunk-LAVHRER2.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCustomer,
  useUpdateCustomer
} from "./chunk-PRBE5CM3.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/customers/customer-edit/components/edit-customer-form/edit-customer-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, toast } 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 EditCustomerSchema = zod.object({
  email: zod.string().email(),
  first_name: zod.string().optional(),
  last_name: zod.string().optional(),
  company_name: zod.string().optional(),
  phone: zod.string().optional()
});
var EditCustomerForm = ({ customer }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      email: customer.email || "",
      first_name: customer.first_name || "",
      last_name: customer.last_name || "",
      company_name: customer.company_name || "",
      phone: customer.phone || ""
    },
    resolver: zodResolver(EditCustomerSchema)
  });
  const { mutateAsync, isPending } = useUpdateCustomer(customer.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        email: customer.has_account ? void 0 : data.email,
        first_name: data.first_name || void 0,
        last_name: data.last_name || void 0,
        phone: data.phone || void 0,
        company_name: data.company_name || void 0
      },
      {
        onSuccess: ({ customer: customer2 }) => {
          toast.success(
            t("customers.edit.successToast", {
              email: customer2.email
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "edit-customer-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex flex-1 flex-col", "data-testid": "edit-customer-form-keybound", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": "edit-customer-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", "data-testid": "edit-customer-form-fields", children: [
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "email",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-form-email-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-form-email-label", children: t("fields.email") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-form-email-control", children: /* @__PURE__ */ jsx(
                ConditionalTooltip,
                {
                  showTooltip: customer.has_account,
                  content: t("customers.edit.emailDisabledTooltip"),
                  children: /* @__PURE__ */ jsx(Input, { ...field, disabled: customer.has_account, "data-testid": "edit-customer-form-email-input" })
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-form-email-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "first_name",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-form-first-name-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-form-first-name-label", children: t("fields.firstName") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-form-first-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "edit-customer-form-first-name-input" }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-form-first-name-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "last_name",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-form-last-name-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-form-last-name-label", children: t("fields.lastName") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-form-last-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "edit-customer-form-last-name-input" }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-form-last-name-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "company_name",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-form-company-name-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-form-company-name-label", children: t("fields.company") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-form-company-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "edit-customer-form-company-name-input" }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-form-company-name-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "phone",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-form-phone-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-form-phone-label", children: t("fields.phone") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-form-phone-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "edit-customer-form-phone-input" }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-form-phone-error" })
            ] });
          }
        }
      )
    ] }) }),
    /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "edit-customer-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "edit-customer-form-footer-actions", children: [
      /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", "data-testid": "edit-customer-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(
        Button,
        {
          isLoading: isPending,
          type: "submit",
          variant: "primary",
          size: "small",
          "data-testid": "edit-customer-form-submit-button",
          children: t("actions.save")
        }
      )
    ] }) })
  ] }) });
};

// src/pages/customers/customer-edit/customer-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var CustomerEdit = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { customer, isLoading, isError, error } = useCustomer(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(Heading, { children: t("customers.edit.header") }) }),
    !isLoading && customer && /* @__PURE__ */ jsx2(EditCustomerForm, { customer })
  ] });
};
export {
  CustomerEdit as Component
};
