import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCreateRefundReason
} from "./chunk-PBQ5M44A.js";
import "./chunk-OH4AABUT.js";
import "./chunk-YKDEE7ZL.js";
import "./chunk-AEMDNTW5.js";
import "./chunk-LYD3C73Z.js";
import "./chunk-7O5NDICV.js";
import "./chunk-46M463BR.js";
import "./chunk-YFRC6GEN.js";
import "./chunk-N6SI4IIT.js";
import "./chunk-JPNPTILP.js";
import "./chunk-DAXXLQKB.js";
import "./chunk-OHOWEMHO.js";
import "./chunk-GKGLVDDV.js";
import "./chunk-WJJXUXSO.js";
import "./chunk-LAPEZATU.js";
import "./chunk-DBNPVU3L.js";
import "./chunk-U7WZYWGA.js";
import "./chunk-ZN7LWXVC.js";
import "./chunk-VYMODH6P.js";
import "./chunk-LEG67A2R.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5TRY5IMZ.js";
import "./chunk-TAKEQAZZ.js";
import "./chunk-B6FG7A6W.js";
import "./chunk-EPHVGD5Z.js";
import "./chunk-ESPLVOFC.js";
import "./chunk-FXWKGNRV.js";
import "./chunk-VDKOJ5WD.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-Z2QQZOG3.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/refund-reasons/refund-reason-create/components/refund-reason-create-form/refund-reason-create-form.tsx
import { Button, Heading, Input, Text, Textarea, toast } from "@medusajs/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var RefundReasonCreateSchema = z.object({
  label: z.string().min(1),
  code: z.string().min(1),
  description: z.string().optional()
});
var RefundReasonCreateForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      label: "",
      code: "",
      description: ""
    },
    resolver: zodResolver(RefundReasonCreateSchema)
  });
  const generateCodeFromLabel = (label) => {
    return label.toLowerCase().replace(/[^a-z0-9]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
  };
  const { mutateAsync, isPending } = useCreateRefundReason();
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(data, {
      onSuccess: ({ refund_reason }) => {
        toast.success(
          t("refundReasons.create.successToast", {
            label: refund_reason.label
          })
        );
        handleSuccess(`../`);
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "refund-reason-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      className: "flex size-full flex-col overflow-hidden",
      onSubmit: handleSubmit,
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "refund-reason-create-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 justify-center overflow-auto px-6 py-16", "data-testid": "refund-reason-create-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", "data-testid": "refund-reason-create-form-header-section", children: [
            /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { "data-testid": "refund-reason-create-form-heading", children: t("refundReasons.create.header") }) }),
            /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "refund-reason-create-form-subtitle", children: t("refundReasons.create.subtitle") }) })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "grid gap-4 md:grid-cols-2", children: /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "label",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "refund-reason-create-form-label-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "refund-reason-create-form-label-label", children: t("refundReasons.fields.label.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "refund-reason-create-form-label-control", children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      placeholder: t(
                        "refundReasons.fields.label.placeholder"
                      ),
                      "data-testid": "refund-reason-create-form-label-input",
                      onChange: (e) => {
                        if (!form.getFieldState("code").isTouched || !form.getValues("code")) {
                          form.setValue(
                            "code",
                            generateCodeFromLabel(e.target.value)
                          );
                        }
                        field.onChange(e);
                      }
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "refund-reason-create-form-label-error" })
                ] });
              }
            }
          ) }),
          /* @__PURE__ */ jsx("div", { className: "grid gap-4 md:grid-cols-2", children: /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "code",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("refundReasons.fields.code.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      placeholder: t(
                        "refundReasons.fields.code.placeholder"
                      )
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ) }),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "description",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "refund-reason-create-form-description-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "refund-reason-create-form-description-label", children: t("refundReasons.fields.description.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "refund-reason-create-form-description-control", children: /* @__PURE__ */ jsx(
                    Textarea,
                    {
                      ...field,
                      placeholder: t(
                        "refundReasons.fields.description.placeholder"
                      ),
                      "data-testid": "refund-reason-create-form-description-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "refund-reason-create-form-description-error" })
                ] });
              }
            }
          )
        ] }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "refund-reason-create-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-2", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", "data-testid": "refund-reason-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "refund-reason-create-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/refund-reasons/refund-reason-create/refund-reason-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var RefundReasonCreate = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { "data-testid": "refund-reason-create-modal", children: /* @__PURE__ */ jsx2(RefundReasonCreateForm, {}) });
};
export {
  RefundReasonCreate as Component
};
