import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCustomerGroup,
  useUpdateCustomerGroup
} from "./chunk-UU2RQMXS.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/customer-groups/customer-group-edit/components/edit-customer-group-form/edit-customer-group-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 z from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditCustomerGroupSchema = z.object({
  name: z.string().min(1)
});
var EditCustomerGroupForm = ({
  group
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: group.name || ""
    },
    resolver: zodResolver(EditCustomerGroupSchema)
  });
  const { mutateAsync, isPending } = useUpdateCustomerGroup(group.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(data, {
      onSuccess: ({ customer_group }) => {
        toast.success(
          t("customerGroups.edit.successToast", {
            name: customer_group.name
          })
        );
        handleSuccess();
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "edit-customer-group-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "edit-customer-group-form-keybound",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex max-w-full flex-1 flex-col gap-y-8 overflow-y-auto", "data-testid": "edit-customer-group-form-body", children: /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "name",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "edit-customer-group-form-name-item", children: [
                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "edit-customer-group-form-name-label", children: t("fields.name") }),
                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "edit-customer-group-form-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, size: "small", "data-testid": "edit-customer-group-form-name-input" }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "edit-customer-group-form-name-error" })
              ] });
            }
          }
        ) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "edit-customer-group-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "edit-customer-group-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "edit-customer-group-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "edit-customer-group-form-submit-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

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