import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCustomerGroup,
  useUpdateCustomerGroup
} from "./chunk-II3CLUXA.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/customer-groups/customer-group-edit/customer-group-edit.tsx
import { Heading } from "@medusajs/ui";
import { useLinkQuery } from "@mercurjs/dashboard-shared";
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 { Button, Input, toast } from "@medusajs/ui";
import i18n from "i18next";
import { useTranslation } from "react-i18next";
import * as z from "zod";
import {
  FormExtensionZone,
  useExtendableForm
} from "@mercurjs/dashboard-shared";
import { jsx, jsxs } from "react/jsx-runtime";
var EditCustomerGroupSchema = z.object({
  name: z.string().min(1, { message: i18n.t("customerGroups.validation.nameRequired") })
});
var EditCustomerGroupForm = ({
  group
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useExtendableForm({
    schema: EditCustomerGroupSchema,
    model: "customer_group",
    zone: "edit",
    data: group,
    defaultValues: {
      name: group.name || ""
    }
  });
  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, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "edit-customer-group-form",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex max-w-full flex-1 flex-col gap-y-8 overflow-y-auto", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("fields.name") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      size: "small",
                      "data-testid": "edit-customer-group-form-name-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            FormExtensionZone,
            {
              model: "customer_group",
              zone: "edit",
              control: form.control,
              data: group
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.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": "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 query = useLinkQuery("customer_group");
  const { customer_group, isLoading, isError, error } = useCustomerGroup(
    id,
    query
  );
  const { t } = useTranslation2();
  if (isError) {
    throw error;
  }
  const ready = !isLoading && !!customer_group;
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { children: t("customerGroups.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { className: "sr-only", children: t("customerGroups.edit.header") })
    ] }),
    ready && /* @__PURE__ */ jsx2(EditCustomerGroupForm, { group: customer_group })
  ] });
};
export {
  CustomerGroupEdit as Component
};
