import {
  DEFAULT_FIELDS
} from "./chunk-Y2M536WR.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-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 {
  useOrder,
  useUpdateOrder
} from "./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/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
};
