import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useInventoryItem,
  useUpdateInventoryItem
} 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/inventory/[id]/_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";
import { useLinkQuery } from "@mercurjs/dashboard-shared";

// src/pages/inventory/[id]/_components/edit-inventory-item/components/edit-item-form.tsx
import { Button, Input, toast } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import {
  FormExtensionZone,
  useExtendableForm
} from "@mercurjs/dashboard-shared";
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 = useExtendableForm({
    schema: EditInventoryItemSchema,
    model: "inventory_item",
    zone: "edit",
    data: item,
    defaultValues: getDefaultValues(item)
  });
  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, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-auto", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "title",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("fields.title") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "sku",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("fields.sku") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            FormExtensionZone,
            {
              model: "inventory_item",
              zone: "edit",
              control: form.control,
              data: item
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.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", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading, children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/inventory/[id]/_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, useLinkQuery("inventory_item"));
  const ready = !isLoading && inventoryItem;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(Heading, { children: t("inventory.editItemDetails") }) }),
    ready && /* @__PURE__ */ jsx2(EditInventoryItemForm, { item: inventoryItem })
  ] });
};
export {
  InventoryItemEdit as Component
};
