import {
  PRODUCT_DETAIL_QUERY
} from "./chunk-35NDMIYZ.js";
import {
  useComboboxData
} from "./chunk-AEFDAU3Y.js";
import {
  Combobox
} from "./chunk-AW77UWCZ.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 {
  useProduct,
  useUpdateProduct
} from "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/products/product-shipping-profile/product-shipping-profile.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/products/product-shipping-profile/components/product-organization-form/product-shipping-profile-form.tsx
import { Button, toast } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useEffect } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var ProductShippingProfileSchema = zod.object({
  shipping_profile_id: zod.string()
});
var ProductShippingProfileForm = ({
  product
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const shippingProfiles = useComboboxData({
    queryKey: ["shipping_profiles"],
    queryFn: (params) => sdk.admin.shippingProfiles.query(params),
    getOptions: (data) => data.shipping_profiles.map((shippingProfile) => ({
      label: shippingProfile.name,
      value: shippingProfile.id
    }))
  });
  const form = useForm({
    defaultValues: {
      shipping_profile_id: product.shipping_profile?.id ?? ""
    },
    resolver: zodResolver(ProductShippingProfileSchema)
  });
  const selectedShippingProfile = form.watch("shipping_profile_id");
  const { mutateAsync, isPending } = useUpdateProduct(product.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        shipping_profile_id: data.shipping_profile_id === "" ? null : data.shipping_profile_id
      },
      {
        onSuccess: ({ product: product2 }) => {
          toast.success(
            t("products.shippingProfile.edit.toasts.success", {
              title: product2.title
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  useEffect(() => {
    if (typeof selectedShippingProfile === "undefined") {
      form.setValue("shipping_profile_id", "");
    }
  }, [selectedShippingProfile, form]);
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-shipping-profile-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex h-full flex-col", "data-testid": "product-shipping-profile-keybound-form", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": "product-shipping-profile-form-body", children: /* @__PURE__ */ jsx("div", { className: "flex h-full flex-col gap-y-4", "data-testid": "product-shipping-profile-form-fields", children: /* @__PURE__ */ jsx(
      Form.Field,
      {
        control: form.control,
        name: "shipping_profile_id",
        render: ({ field }) => {
          return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-shipping-profile-form-shipping-profile-item", children: [
            /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-shipping-profile-form-shipping-profile-label", children: t("products.fields.shipping_profile.label") }),
            /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-shipping-profile-form-shipping-profile-control", children: /* @__PURE__ */ jsx(
              Combobox,
              {
                ...field,
                allowClear: true,
                options: shippingProfiles.options,
                searchValue: shippingProfiles.searchValue,
                onSearchValueChange: shippingProfiles.onSearchValueChange,
                fetchNextPage: shippingProfiles.fetchNextPage,
                "data-testid": "product-shipping-profile-form-shipping-profile-combobox"
              }
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-shipping-profile-form-shipping-profile-error" })
          ] });
        }
      }
    ) }) }),
    /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-shipping-profile-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "product-shipping-profile-form-footer-actions", children: [
      /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, "data-testid": "product-shipping-profile-form-cancel-button-wrapper", children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "product-shipping-profile-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "product-shipping-profile-form-save-button", children: t("actions.save") })
    ] }) })
  ] }) });
};

// src/pages/products/product-shipping-profile/product-shipping-profile.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ProductShippingProfile = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { product, isLoading, isError, error } = useProduct(id, PRODUCT_DETAIL_QUERY);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "product-shipping-profile-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "product-shipping-profile-drawer-header", children: /* @__PURE__ */ jsx2(
      RouteDrawer.Title,
      {
        asChild: true,
        "data-testid": "product-shipping-profile-drawer-title",
        children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "product-shipping-profile-drawer-title-text", children: t("products.shippingProfile.edit.header") })
      }
    ) }),
    !isLoading && product && /* @__PURE__ */ jsx2(ProductShippingProfileForm, { product })
  ] });
};
export {
  ProductShippingProfile as Component
};
