import {
  DEFAULT_FIELDS
} from "./chunk-Y2M536WR.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./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 {
  useOrder,
  useUpdateOrder
} from "./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/orders/order-edit-email/order-edit-email.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/orders/order-edit-email/components/edit-order-email-form/edit-order-email-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 zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditOrderEmailSchema = zod.object({
  email: zod.string().email()
});
function EditOrderEmailForm({ order }) {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      email: order.email || ""
    },
    resolver: zodResolver(EditOrderEmailSchema)
  });
  const { mutateAsync, isPending } = useUpdateOrder(order.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    try {
      await mutateAsync({
        email: data.email
      });
      toast.success(t("orders.edit.email.requestSuccess"));
      handleSuccess();
    } catch (error) {
      toast.error(error.message);
    }
  });
  return /* @__PURE__ */ jsx(
    RouteDrawer.Form,
    {
      form,
      "data-testid": "order-edit-email-form",
      children: /* @__PURE__ */ jsxs(
        KeyboundForm,
        {
          onSubmit: handleSubmit,
          className: "flex size-full flex-col overflow-hidden",
          "data-testid": "order-edit-email-form",
          children: [
            /* @__PURE__ */ jsx(
              RouteDrawer.Body,
              {
                className: "flex-1 overflow-auto",
                "data-testid": "order-edit-email-body",
                children: /* @__PURE__ */ jsx(
                  Form.Field,
                  {
                    control: form.control,
                    name: "email",
                    render: ({ field }) => {
                      return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-email-item", children: [
                        /* @__PURE__ */ jsx(Form.Label, { "data-testid": "order-edit-email-label", children: t("fields.email") }),
                        /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-email-control", children: /* @__PURE__ */ jsx(
                          Input,
                          {
                            type: "email",
                            ...field,
                            "data-testid": "order-edit-email-input"
                          }
                        ) }),
                        /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-email-error" })
                      ] });
                    }
                  }
                )
              }
            ),
            /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "order-edit-email-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "order-edit-email-form-footer-actions", children: [
              /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(
                Button,
                {
                  variant: "secondary",
                  size: "small",
                  "data-testid": "order-edit-email-cancel-button",
                  children: t("actions.cancel")
                }
              ) }),
              /* @__PURE__ */ jsx(
                Button,
                {
                  isLoading: isPending,
                  type: "submit",
                  variant: "primary",
                  size: "small",
                  "data-testid": "order-edit-email-save-button",
                  children: t("actions.save")
                }
              )
            ] }) })
          ]
        }
      )
    }
  );
}

// src/pages/orders/order-edit-email/order-edit-email.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var OrderEditEmail = () => {
  const { t } = useTranslation2();
  const params = useParams();
  const { order, isPending, isError, error } = useOrder(params.id, {
    fields: DEFAULT_FIELDS
  });
  if (!isPending && isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "order-edit-email-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "order-edit-email-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "order-edit-email-heading", children: t("orders.edit.email.title") }) }),
    order && /* @__PURE__ */ jsx2(EditOrderEmailForm, { order })
  ] });
};
export {
  OrderEditEmail as Component
};
