import {
  useReturnReason,
  useUpdateReturnReason
} from "./chunk-2FGRDEAD.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/return-reasons/return-reason-edit/components/return-reason-edit-form/return-reason-edit-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, Textarea, 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 ReturnReasonEditSchema = z.object({
  value: z.string().min(1),
  label: z.string().min(1),
  description: z.string().optional()
});
var ReturnReasonEditForm = ({
  returnReason
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      value: returnReason.value,
      label: returnReason.label,
      description: returnReason.description ?? void 0
    },
    resolver: zodResolver(ReturnReasonEditSchema)
  });
  const { mutateAsync, isPending } = useUpdateReturnReason(returnReason.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(data, {
      onSuccess: ({ return_reason }) => {
        toast.success(
          t("returnReasons.edit.successToast", {
            label: return_reason.label
          })
        );
        handleSuccess();
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "return-reason-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      className: "flex size-full flex-col overflow-hidden",
      onSubmit: handleSubmit,
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-4 overflow-auto", "data-testid": "return-reason-edit-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "value",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "return-reason-edit-form-value-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { tooltip: t("returnReasons.fields.value.tooltip"), "data-testid": "return-reason-edit-form-value-label", children: t("returnReasons.fields.value.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "return-reason-edit-form-value-control", children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      placeholder: t("returnReasons.fields.value.placeholder"),
                      "data-testid": "return-reason-edit-form-value-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "return-reason-edit-form-value-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "label",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "return-reason-edit-form-label-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "return-reason-edit-form-label-label", children: t("returnReasons.fields.label.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "return-reason-edit-form-label-control", children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      placeholder: t("returnReasons.fields.label.placeholder"),
                      "data-testid": "return-reason-edit-form-label-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "return-reason-edit-form-label-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "description",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "return-reason-edit-form-description-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "return-reason-edit-form-description-label", children: t("returnReasons.fields.description.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "return-reason-edit-form-description-control", children: /* @__PURE__ */ jsx(
                    Textarea,
                    {
                      ...field,
                      placeholder: t(
                        "returnReasons.fields.description.placeholder"
                      ),
                      "data-testid": "return-reason-edit-form-description-input"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "return-reason-edit-form-description-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "return-reason-edit-form-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, { variant: "secondary", size: "small", type: "button", "data-testid": "return-reason-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "return-reason-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/return-reasons/return-reason-edit/return-reason-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ReturnReasonEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { return_reason, isPending, isError, error } = useReturnReason(id);
  const ready = !isPending && !!return_reason;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "return-reason-edit-drawer", children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { "data-testid": "return-reason-edit-drawer-header", children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "return-reason-edit-drawer-heading", children: t("returnReasons.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { className: "sr-only", children: t("returnReasons.edit.subtitle") })
    ] }),
    ready && /* @__PURE__ */ jsx2(ReturnReasonEditForm, { returnReason: return_reason })
  ] });
};
export {
  ReturnReasonEdit as Component
};
