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/edit-item-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/components/edit-item-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 EditInventoryItemSchema = z.object({
  title: z.string().optional(),
  sku: z.string().min(1)
});
var getDefaultValues = (item) => {
  return {
    title: item.title ?? void 0,
    sku: item.sku ?? void 0
  };
};
var EditInventoryItemForm = ({ item }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: getDefaultValues(item),
    resolver: zodResolver(EditInventoryItemSchema)
  });
  const { mutateAsync, isPending: isLoading } = useUpdateInventoryItem(item.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    mutateAsync(values, {
      onSuccess: () => {
        toast.success(t("inventory.toast.updateItem"));
        handleSuccess();
      },
      onError: (e) => toast.error(e.message)
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "inventory-edit-item-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "inventory-edit-item-keybound-form",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-auto", "data-testid": "inventory-edit-item-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "title",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-form-title-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "inventory-edit-item-form-title-label", children: t("fields.title") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-form-title-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-form-title-input-wrapper", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "inventory-edit-item-form-title-input" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-form-title-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "sku",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-edit-item-form-sku-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "inventory-edit-item-form-sku-label", children: t("fields.sku") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "inventory-edit-item-form-sku-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "inventory-edit-item-form-sku-input-wrapper", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "inventory-edit-item-form-sku-input" }) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "inventory-edit-item-form-sku-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "inventory-edit-item-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "inventory-edit-item-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, "data-testid": "inventory-edit-item-form-cancel-button-wrapper", children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", "data-testid": "inventory-edit-item-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading, "data-testid": "inventory-edit-item-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/inventory/inventory-detail/components/edit-inventory-item/edit-item-drawer.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var InventoryItemEdit = () => {
  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-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "inventory-edit-item-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "inventory-edit-item-drawer-title", children: t("inventory.editItemDetails") }) }),
    ready && /* @__PURE__ */ jsx2(EditInventoryItemForm, { item: inventoryItem })
  ] });
};
export {
  InventoryItemEdit as Component
};
