import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCreateShippingOptionType
} from "./chunk-3MDRW5MA.js";
import "./chunk-MFL2TZ3W.js";
import "./chunk-YNIIYGL4.js";
import "./chunk-CBHNHLUZ.js";
import "./chunk-VME3CJER.js";
import "./chunk-XMVTX2EN.js";
import "./chunk-5PUHROK4.js";
import "./chunk-TPPONKYW.js";
import "./chunk-DF3OGJAK.js";
import "./chunk-XRDP2SYY.js";
import "./chunk-FNBKEF54.js";
import "./chunk-EHQESDH2.js";
import "./chunk-XKVXLDSJ.js";
import "./chunk-XDZGOUCR.js";
import "./chunk-K4BGK3CO.js";
import "./chunk-YYTI5JIC.js";
import "./chunk-HAFZQK53.js";
import "./chunk-IJWG755C.js";
import "./chunk-M3YP3NX4.js";
import "./chunk-UU2RQMXS.js";
import "./chunk-O4STPJR2.js";
import "./chunk-R74OHYTB.js";
import "./chunk-22XJUSMM.js";
import "./chunk-OFKUNYQJ.js";
import "./chunk-W5UPPL3W.js";
import "./chunk-LC6TM5OT.js";
import "./chunk-7SJC6BWZ.js";
import "./chunk-N4JU7GWK.js";
import "./chunk-52BRBQMY.js";
import "./chunk-BA2HXHTW.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/shipping-option-types/shipping-option-type-create/components/create-shipping-option-type-form/create-shipping-option-type-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Heading, Input, Text, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var CreateShippingOptionTypeSchema = z.object({
  label: z.string().min(1),
  code: z.string().min(1),
  description: z.string().optional()
});
var CreateShippingOptionTypeForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      label: "",
      code: "",
      description: void 0
    },
    resolver: zodResolver(CreateShippingOptionTypeSchema)
  });
  const generateCodeFromLabel = (label) => {
    return label.toLowerCase().replace(/[^a-z0-9]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
  };
  const { mutateAsync, isPending } = useCreateShippingOptionType();
  const handleSubmit = form.handleSubmit(
    async (values) => {
      await mutateAsync(values, {
        onSuccess: ({ shipping_option_type }) => {
          toast.success(
            t("shippingOptionTypes.create.successToast", {
              label: shipping_option_type.label.trim()
            })
          );
          handleSuccess(
            `/settings/locations/shipping-option-types/${shipping_option_type.id}`
          );
        },
        onError: (e) => {
          toast.error(e.message);
        }
      });
    }
  );
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "shipping-option-type-create-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex h-full flex-col", children: [
    /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-col items-center overflow-auto p-16", "data-testid": "shipping-option-type-create-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8", children: [
      /* @__PURE__ */ jsxs("div", { "data-testid": "shipping-option-type-create-form-header-section", children: [
        /* @__PURE__ */ jsx(Heading, { "data-testid": "shipping-option-type-create-form-heading", children: t("shippingOptionTypes.create.header") }),
        /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "shipping-option-type-create-form-hint", children: t("shippingOptionTypes.create.hint") })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4", children: [
        /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "label",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-create-form-label-item", children: [
                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "shipping-option-type-create-form-label-label", children: t("shippingOptionTypes.fields.label") }),
                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-create-form-label-control", children: /* @__PURE__ */ jsx(
                  Input,
                  {
                    ...field,
                    onChange: (e) => {
                      if (!form.getFieldState("code").isTouched || !form.getValues("code")) {
                        form.setValue(
                          "code",
                          generateCodeFromLabel(e.target.value)
                        );
                      }
                      field.onChange(e);
                    },
                    "data-testid": "shipping-option-type-create-form-label-input"
                  }
                ) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-create-form-label-error" })
              ] });
            }
          }
        ),
        /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "code",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-create-form-code-item", children: [
                /* @__PURE__ */ jsx(Form.Label, { "data-testid": "shipping-option-type-create-form-code-label", children: t("shippingOptionTypes.fields.code") }),
                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-create-form-code-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "shipping-option-type-create-form-code-input" }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-create-form-code-error" })
              ] });
            }
          }
        ),
        /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "description",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "shipping-option-type-create-form-description-item", children: [
                /* @__PURE__ */ jsxs(Form.Label, { "data-testid": "shipping-option-type-create-form-description-label", children: [
                  t("shippingOptionTypes.fields.description"),
                  /* @__PURE__ */ jsxs(
                    Text,
                    {
                      size: "small",
                      leading: "compact",
                      className: "text-ui-fg-muted ml-1 inline",
                      children: [
                        "(",
                        t("fields.optional"),
                        ")"
                      ]
                    }
                  )
                ] }),
                /* @__PURE__ */ jsx(Form.Control, { "data-testid": "shipping-option-type-create-form-description-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "shipping-option-type-create-form-description-input" }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "shipping-option-type-create-form-description-error" })
              ] });
            }
          }
        )
      ] })
    ] }) }),
    /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "shipping-option-type-create-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
      /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "shipping-option-type-create-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(
        Button,
        {
          size: "small",
          variant: "primary",
          type: "submit",
          isLoading: isPending,
          "data-testid": "shipping-option-type-create-form-create-button",
          children: t("actions.create")
        }
      )
    ] }) })
  ] }) });
};

// src/pages/shipping-option-types/shipping-option-type-create/shipping-option-type-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var ShippingOptionTypeCreate = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { "data-testid": "shipping-option-type-create-modal", children: /* @__PURE__ */ jsx2(CreateShippingOptionTypeForm, {}) });
};
export {
  ShippingOptionTypeCreate as Component
};
