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 "./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 {
  useInventoryItem,
  useUpdateInventoryItem
} from "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/inventory/inventory-detail/components/edit-inventory-item-attributes/edit-item-attributes-drawer.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/inventory/inventory-detail/components/edit-inventory-item-attributes/components/edit-item-attributes-form.tsx
import { Button, Input, toast } from "@medusajs/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditInventoryItemAttributesSchema = z.object({
  height: z.number().positive().optional(),
  width: z.number().positive().optional(),
  length: z.number().positive().optional(),
  weight: z.number().positive().optional(),
  mid_code: z.string().optional(),
  material: z.string().optional(),
  hs_code: z.string().optional(),
  origin_country: z.string().optional()
});
var getDefaultValues = (item) => {
  return {
    height: item.height ?? void 0,
    width: item.width ?? void 0,
    length: item.length ?? void 0,
    weight: item.weight ?? void 0,
    mid_code: item.mid_code ?? void 0,
    material: item.material ?? void 0,
    hs_code: item.hs_code ?? void 0,
    origin_country: item.origin_country ?? void 0
  };
};
var EditInventoryItemAttributesForm = ({
  item
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: getDefaultValues(item),
    resolver: zodResolver(EditInventoryItemAttributesSchema)
  });
  const { mutateAsync, isPending: isLoading } = useUpdateInventoryItem(item.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(values, {
      onSuccess: () => {
        toast.success(t("inventory.toast.updateItem"));
        handleSuccess();
      },
      onError: (error) => toast.error(error.message)
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "inventory-edit-item-attributes-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "inventory-edit-item-attributes-keybound-form",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-4 overflow-auto", "data-testid": "inventory-edit-item-attributes-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "height",
              render: ({ field: { onChange, value, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-height-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-height-label", children: t("fields.height") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-height-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-height-input-wrapper", 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": "inventory-edit-item-attributes-form-height-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-height-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "width",
              render: ({ field: { onChange, value, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-width-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-width-label", children: t("fields.width") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-width-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-width-input-wrapper", 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": "inventory-edit-item-attributes-form-width-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-width-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "length",
              render: ({ field: { onChange, value, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-length-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-length-label", children: t("fields.length") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-length-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-length-input-wrapper", 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": "inventory-edit-item-attributes-form-length-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-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": "inventory-edit-item-attributes-form-weight-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-weight-label", children: t("fields.weight") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-weight-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-weight-input-wrapper", 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": "inventory-edit-item-attributes-form-weight-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-weight-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "mid_code",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-mid-code-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-mid-code-label", children: t("fields.midCode") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-mid-code-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-mid-code-input-wrapper", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "inventory-edit-item-attributes-form-mid-code-input" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-mid-code-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "hs_code",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-hs-code-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-hs-code-label", children: t("fields.hsCode") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-hs-code-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-hs-code-input-wrapper", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "inventory-edit-item-attributes-form-hs-code-input" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-hs-code-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "material",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-material-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-material-label", children: t("fields.material") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-material-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-material-input-wrapper", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "inventory-edit-item-attributes-form-material-input" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-material-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "origin_country",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-attributes-form-origin-country-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "inventory-edit-item-attributes-form-origin-country-label", children: t("fields.countryOfOrigin") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-attributes-form-origin-country-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-attributes-form-origin-country-select-wrapper", children: /* @__PURE__ */ jsx(CountrySelect, { ...field, "data-testid": "inventory-edit-item-attributes-form-origin-country-select" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-attributes-form-origin-country-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "inventory-edit-item-attributes-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "inventory-edit-item-attributes-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, "data-testid": "inventory-edit-item-attributes-form-cancel-button-wrapper", children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", "data-testid": "inventory-edit-item-attributes-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading, "data-testid": "inventory-edit-item-attributes-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/inventory/inventory-detail/components/edit-inventory-item-attributes/edit-item-attributes-drawer.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var InventoryItemAttributesEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const {
    inventory_item: inventoryItem,
    isPending: isLoading,
    isError,
    error
  } = useInventoryItem(id);
  const ready = !isLoading && inventoryItem;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "inventory-edit-item-attributes-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "inventory-edit-item-attributes-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "inventory-edit-item-attributes-drawer-title", children: t("products.editAttributes") }) }),
    ready && /* @__PURE__ */ jsx2(EditInventoryItemAttributesForm, { item: inventoryItem })
  ] });
};
export {
  InventoryItemAttributesEdit as Component
};
