import {
  DEFAULT_FIELDS
} from "./chunk-Y2M536WR.js";
import "./chunk-NXDFDEKP.js";
import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import "./chunk-FMFVTERD.js";
import "./chunk-JRTZWK77.js";
import {
  i18n
} from "./chunk-ZMDE5HEV.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-billing-address/order-edit-billing-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-billing-address/components/edit-order-billing-address-form/edit-order-billing-address-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 EditOrderBillingAddressSchema = 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.billingAddress.validation.addressRequired"),
      path: ["address_1"]
    });
  }
  if (!country_code || country_code.length !== 2) {
    ctx.addIssue({
      code: zod.ZodIssueCode.custom,
      message: i18n.t("orders.edit.billingAddress.validation.countryRequired"),
      path: ["country_code"]
    });
  }
});
function EditOrderBillingAddressForm({ order }) {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      address_1: order.billing_address?.address_1 || "",
      address_2: order.billing_address?.address_2 || "",
      city: order.billing_address?.city || "",
      company: order.billing_address?.company || "",
      country_code: order.billing_address?.country_code || "",
      phone: order.billing_address?.phone || "",
      postal_code: order.billing_address?.postal_code || "",
      province: order.billing_address?.province || ""
    },
    resolver: zodResolver(EditOrderBillingAddressSchema)
  });
  const { mutateAsync, isPending } = useUpdateOrder(order.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    try {
      await mutateAsync({
        billing_address: data
      });
      toast.success(t("orders.edit.billingAddress.requestSuccess"));
      handleSuccess();
    } catch (error) {
      toast.error(error.message);
    }
  });
  return /* @__PURE__ */ jsx(
    RouteDrawer.Form,
    {
      form,
      "data-testid": "order-edit-billing-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-billing-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-billing-address-address1-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "order-edit-billing-address-address1-label", children: t("fields.address") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-address1-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-address1-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-address1-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "address_2",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-address2-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-address2-label",
                              children: t("fields.address2")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-address2-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-address2-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-address2-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "postal_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-postal-code-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-postal-code-label",
                              children: t("fields.postalCode")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-postal-code-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-postal-code-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-postal-code-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "city",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-city-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-city-label",
                              children: t("fields.city")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-city-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-city-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-city-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "country_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-country-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "order-edit-billing-address-country-label", children: t("fields.country") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-country-control", children: /* @__PURE__ */ jsx(
                            CountrySelect,
                            {
                              ...field,
                              disabled: true,
                              "data-testid": "order-edit-billing-address-country-select"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-country-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "province",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-province-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-province-label",
                              children: t("fields.state")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-province-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-province-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-province-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "company",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-company-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-company-label",
                              children: t("fields.company")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-company-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-company-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-company-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "phone",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "order-edit-billing-address-phone-item", children: [
                          /* @__PURE__ */ jsx(
                            Form.Label,
                            {
                              optional: true,
                              "data-testid": "order-edit-billing-address-phone-label",
                              children: t("fields.phone")
                            }
                          ),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "order-edit-billing-address-phone-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              size: "small",
                              ...field,
                              "data-testid": "order-edit-billing-address-phone-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "order-edit-billing-address-phone-error" })
                        ] });
                      }
                    }
                  )
                ] })
              }
            ),
            /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "order-edit-billing-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-billing-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-billing-address-save-button",
                  children: t("actions.save")
                }
              )
            ] }) })
          ]
        }
      )
    }
  );
}

// src/pages/orders/order-edit-billing-address/order-edit-billing-address.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var OrderEditBillingAddress = () => {
  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-billing-address-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "order-edit-billing-address-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "order-edit-billing-address-heading", children: t("orders.edit.billingAddress.title") }) }),
    order && /* @__PURE__ */ jsx2(EditOrderBillingAddressForm, { order })
  ] });
};
export {
  OrderEditBillingAddress as Component
};
