import {
  DEFAULT_FIELDS
} from "./chunk-Y2M536WR.js";
import "./chunk-NXDFDEKP.js";
import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import "./chunk-JRTZWK77.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-shipping-address/order-edit-shipping-address.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/orders/order-edit-shipping-address/components/edit-order-shipping-address-form/edit-order-shipping-address-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, toast } from "@medusajs/ui";
import i18n from "i18next";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditOrderShippingAddressSchema = zod.object({
  address_1: zod.string(),
  address_2: zod.string().optional(),
  country_code: zod.string(),
  city: zod.string().optional(),
  postal_code: zod.string().optional(),
  province: zod.string().optional(),
  company: zod.string().optional(),
  phone: zod.string().optional()
}).superRefine(({ address_1, country_code }, ctx) => {
  if (!address_1 || address_1.trim().length === 0) {
    ctx.addIssue({
      code: zod.ZodIssueCode.custom,
      message: i18n.t("orders.edit.shippingAddress.validation.addressRequired"),
      path: ["address_1"]
    });
  }
  if (!country_code || country_code.length !== 2) {
    ctx.addIssue({
      code: zod.ZodIssueCode.custom,
      message: i18n.t("orders.edit.shippingAddress.validation.countryRequired"),
      path: ["country_code"]
    });
  }
});
function EditOrderShippingAddressForm({ order }) {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      address_1: order.shipping_address?.address_1 || "",
      address_2: order.shipping_address?.address_2 || "",
      city: order.shipping_address?.city || "",
      company: order.shipping_address?.company || "",
      country_code: order.shipping_address?.country_code || "",
      phone: order.shipping_address?.phone || "",
      postal_code: order.shipping_address?.postal_code || "",
      province: order.shipping_address?.province || ""
    },
    resolver: zodResolver(EditOrderShippingAddressSchema)
  });
  const { mutateAsync, isPending } = useUpdateOrder(order.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    try {
      await mutateAsync({
        shipping_address: data
      });
      toast.success(t("orders.edit.shippingAddress.requestSuccess"));
      handleSuccess();
    } catch (error) {
      toast.error(error.message);
    }
  });
  return /* @__PURE__ */ jsx(
    RouteDrawer.Form,
    {
      form,
      "data-testid": "order-edit-shipping-address-form",
      children: /* @__PURE__ */ jsxs(
        KeyboundForm,
        {
          onSubmit: handleSubmit,
          className: "flex size-full flex-col overflow-hidden",
          children: [
            /* @__PURE__ */ jsx(
              RouteDrawer.Body,
              {
                className: "flex-1 overflow-auto",
                "data-testid": "order-edit-shipping-address-body",
                children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "address_1",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-address1-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "order-edit-shipping-address-address1-label", children: t("fields.address") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-address1-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-address1-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-address1-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "address_2",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-address2-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-address2-label",
                              children: t("fields.address2")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-address2-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-address2-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-address2-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "postal_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-postal-code-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-postal-code-label",
                              children: t("fields.postalCode")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-postal-code-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-postal-code-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-postal-code-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "city",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-city-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-city-label",
                              children: t("fields.city")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-city-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-city-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-city-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "country_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-country-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "order-edit-shipping-address-country-label", children: t("fields.country") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-country-control", children: /* @__PURE__ */ jsx(
                            CountrySelect,
                            {
                              ...field,
                              disabled: true,
                              "data-testid": "order-edit-shipping-address-country-select"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-country-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "province",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-province-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-province-label",
                              children: t("fields.state")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-province-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-province-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-province-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "company",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-company-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-company-label",
                              children: t("fields.company")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-company-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-company-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-company-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "phone",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-shipping-address-phone-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-shipping-address-phone-label",
                              children: t("fields.phone")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-shipping-address-phone-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-shipping-address-phone-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-shipping-address-phone-error" })
                        ] });
                      }
                    }
                  )
                ] })
              }
            ),
            /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "order-edit-shipping-address-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",
                  "data-testid": "order-edit-shipping-address-cancel-button",
                  children: t("actions.cancel")
                }
              ) }),
              /* @__PURE__ */ jsx(
                Button,
                {
                  isLoading: isPending,
                  type: "submit",
                  variant: "primary",
                  size: "small",
                  disabled: !!Object.keys(form.formState.errors || {}).length,
                  "data-testid": "order-edit-shipping-address-save-button",
                  children: t("actions.save")
                }
              )
            ] }) })
          ]
        }
      )
    }
  );
}

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