import "./chunk-NXDFDEKP.js";
import {
  CountrySelect
} from "./chunk-7CAFYKKA.js";
import "./chunk-JRTZWK77.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useProduct,
  useUpdateProduct
} from "./chunk-YAUGB4LD.js";
import "./chunk-A5DJIOHN.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/products/[id]/attributes/index.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/products/[id]/attributes/product-attributes-form/product-attributes-form.tsx
import { Button, Input } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { jsx, jsxs } from "react/jsx-runtime";
var dimension = zod.union([zod.string(), zod.number()]).transform((value) => {
  if (value === "") {
    return null;
  }
  return Number(value);
}).optional().nullable();
var ProductAttributesSchema = zod.object({
  weight: dimension,
  length: dimension,
  width: dimension,
  height: dimension,
  mid_code: zod.string().optional(),
  hs_code: zod.string().optional(),
  origin_country: zod.string().optional()
});
var ProductAttributesForm = ({
  product
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      height: product.height ? product.height : null,
      width: product.width ? product.width : null,
      length: product.length ? product.length : null,
      weight: product.weight ? product.weight : null,
      mid_code: product.mid_code || "",
      hs_code: product.hs_code || "",
      origin_country: product.origin_country || ""
    },
    resolver: zodResolver(ProductAttributesSchema)
  });
  const { mutateAsync, isPending } = useUpdateProduct(product.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        weight: data.weight ? data.weight : null,
        length: data.length ? data.length : null,
        width: data.width ? data.width : null,
        height: data.height ? data.height : null,
        mid_code: data.mid_code,
        hs_code: data.hs_code,
        origin_country: data.origin_country
      },
      {
        onSuccess: () => {
          handleSuccess();
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-attributes-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col",
      "data-testid": "product-attributes-keybound-form",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": "product-attributes-form-body", children: /* @__PURE__ */ jsx(
          "div",
          {
            className: "flex h-full flex-col gap-y-8",
            "data-testid": "product-attributes-form-fields",
            children: /* @__PURE__ */ jsxs(
              "div",
              {
                className: "flex flex-col gap-y-4",
                "data-testid": "product-attributes-form-main-fields",
                children: [
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "width",
                      render: ({ field: { onChange, value, ...field } }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-width-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-width-label", children: t("fields.width") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-width-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              type: "number",
                              min: 0,
                              value: value || "",
                              onChange: (e) => {
                                const value2 = e.target.value;
                                if (value2 === "") {
                                  onChange(null);
                                } else {
                                  onChange(parseFloat(value2));
                                }
                              },
                              ...field,
                              "data-testid": "product-attributes-form-width-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-width-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "height",
                      render: ({ field: { onChange, value, ...field } }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-height-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-height-label", children: t("fields.height") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-height-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              type: "number",
                              min: 0,
                              value: value || "",
                              onChange: (e) => {
                                const value2 = e.target.value;
                                if (value2 === "") {
                                  onChange(null);
                                } else {
                                  onChange(Number(value2));
                                }
                              },
                              ...field,
                              "data-testid": "product-attributes-form-height-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-height-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "length",
                      render: ({ field: { onChange, value, ...field } }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-length-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-length-label", children: t("fields.length") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-length-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              type: "number",
                              min: 0,
                              value: value || "",
                              onChange: (e) => {
                                const value2 = e.target.value;
                                if (value2 === "") {
                                  onChange(null);
                                } else {
                                  onChange(Number(value2));
                                }
                              },
                              ...field,
                              "data-testid": "product-attributes-form-length-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-length-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "weight",
                      render: ({ field: { onChange, value, ...field } }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-weight-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-weight-label", children: t("fields.weight") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-weight-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              type: "number",
                              min: 0,
                              value: value || "",
                              onChange: (e) => {
                                const value2 = e.target.value;
                                if (value2 === "") {
                                  onChange(null);
                                } else {
                                  onChange(Number(value2));
                                }
                              },
                              ...field,
                              "data-testid": "product-attributes-form-weight-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-weight-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "mid_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-mid-code-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-mid-code-label", children: t("fields.midCode") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-mid-code-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              ...field,
                              "data-testid": "product-attributes-form-mid-code-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-mid-code-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "hs_code",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-hs-code-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-hs-code-label", children: t("fields.hsCode") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-hs-code-control", children: /* @__PURE__ */ jsx(
                            Input,
                            {
                              ...field,
                              "data-testid": "product-attributes-form-hs-code-input"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-hs-code-error" })
                        ] });
                      }
                    }
                  ),
                  /* @__PURE__ */ jsx(
                    Form.Field,
                    {
                      control: form.control,
                      name: "origin_country",
                      render: ({ field }) => {
                        return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-attributes-form-origin-country-item", children: [
                          /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-attributes-form-origin-country-label", children: t("fields.countryOfOrigin") }),
                          /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-attributes-form-origin-country-control", children: /* @__PURE__ */ jsx(
                            CountrySelect,
                            {
                              ...field,
                              "data-testid": "product-attributes-form-origin-country-select"
                            }
                          ) }),
                          /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-attributes-form-origin-country-error" })
                        ] });
                      }
                    }
                  )
                ]
              }
            )
          }
        ) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-attributes-form-footer", children: /* @__PURE__ */ jsxs(
          "div",
          {
            className: "flex items-center justify-end gap-x-2",
            "data-testid": "product-attributes-form-footer-actions",
            children: [
              /* @__PURE__ */ jsx(
                RouteDrawer.Close,
                {
                  asChild: true,
                  "data-testid": "product-attributes-form-cancel-button-wrapper",
                  children: /* @__PURE__ */ jsx(
                    Button,
                    {
                      size: "small",
                      variant: "secondary",
                      "data-testid": "product-attributes-form-cancel-button",
                      children: t("actions.cancel")
                    }
                  )
                }
              ),
              /* @__PURE__ */ jsx(
                Button,
                {
                  size: "small",
                  type: "submit",
                  isLoading: isPending,
                  "data-testid": "product-attributes-form-save-button",
                  children: t("actions.save")
                }
              )
            ]
          }
        ) })
      ]
    }
  ) });
};

// src/pages/products/[id]/attributes/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Component = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { product, isLoading, isError, error } = useProduct(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { children: t("products.editAttributes") }) }) }),
    !isLoading && product && /* @__PURE__ */ jsx2(ProductAttributesForm, { product })
  ] });
};
export {
  Component
};
