import {
  PRODUCT_DETAIL_QUERY
} from "./chunk-35NDMIYZ.js";
import {
  CategoryCombobox
} from "./chunk-3DOCALSU.js";
import {
  useComboboxData
} from "./chunk-AEFDAU3Y.js";
import {
  Combobox
} from "./chunk-AW77UWCZ.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import "./chunk-INNFZYX2.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-Z2QQZOG3.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-organization/product-organization.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/products/product-organization/components/product-organization-form/product-organization-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 { jsx, jsxs } from "react/jsx-runtime";
var ProductOrganizationSchema = zod.object({
  type_id: zod.string().nullable(),
  collection_id: zod.string().nullable(),
  category_ids: zod.array(zod.string()),
  tag_ids: zod.array(zod.string())
});
var ProductOrganizationForm = ({
  product
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const collections = useComboboxData({
    queryKey: ["product_collections"],
    queryFn: (params) => sdk.admin.collections.query(params),
    getOptions: (data) => data.collections.map((collection) => ({
      label: collection.title,
      value: collection.id
    }))
  });
  const types = useComboboxData({
    queryKey: ["product_types"],
    queryFn: (params) => sdk.admin.productTypes.query(params),
    getOptions: (data) => data.product_types.map((type) => ({
      label: type.value,
      value: type.id
    }))
  });
  const tags = useComboboxData({
    queryKey: ["product_tags"],
    queryFn: (params) => sdk.admin.productTags.query(params),
    getOptions: (data) => data.product_tags.map((tag) => ({
      label: tag.value,
      value: tag.id
    }))
  });
  const form = useForm({
    defaultValues: {
      type_id: product.type_id ?? "",
      collection_id: product.collection_id ?? "",
      category_ids: product.categories?.map((c) => c.id) || [],
      tag_ids: product.tags?.map((t2) => t2.id) || []
    },
    resolver: zodResolver(ProductOrganizationSchema)
  });
  const { mutateAsync, isPending } = useUpdateProduct(product.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        type_id: data.type_id || null,
        collection_id: data.collection_id || null,
        categories: data.category_ids.map((c) => ({ id: c })),
        tags: data.tag_ids?.map((t2) => ({ id: t2 }))
      },
      {
        onSuccess: ({ product: product2 }) => {
          toast.success(
            t("products.organization.edit.toasts.success", {
              title: product2.title
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-organization-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col",
      "data-testid": "product-organization-keybound-form",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": "product-organization-form-body", children: /* @__PURE__ */ jsxs(
          "div",
          {
            className: "flex h-full flex-col gap-y-4",
            "data-testid": "product-organization-form-fields",
            children: [
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "type_id",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-organization-form-type-item", children: [
                      /* @__PURE__ */ jsx(
                        Form.Label,
                        {
                          optional: true,
                          "data-testid": "product-organization-form-type-label",
                          children: t("products.fields.type.label")
                        }
                      ),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-organization-form-type-control", children: /* @__PURE__ */ jsx(
                        Combobox,
                        {
                          ...field,
                          options: types.options,
                          searchValue: types.searchValue,
                          onSearchValueChange: types.onSearchValueChange,
                          fetchNextPage: types.fetchNextPage,
                          "data-testid": "product-organization-form-type-combobox"
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-organization-form-type-error" })
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "collection_id",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-organization-form-collection-item", children: [
                      /* @__PURE__ */ jsx(
                        Form.Label,
                        {
                          optional: true,
                          "data-testid": "product-organization-form-collection-label",
                          children: t("products.fields.collection.label")
                        }
                      ),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-organization-form-collection-control", children: /* @__PURE__ */ jsx(
                        Combobox,
                        {
                          ...field,
                          multiple: false,
                          options: collections.options,
                          onSearchValueChange: collections.onSearchValueChange,
                          searchValue: collections.searchValue,
                          "data-testid": "product-organization-form-collection-combobox"
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-organization-form-collection-error" })
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "category_ids",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-organization-form-categories-item", children: [
                      /* @__PURE__ */ jsx(
                        Form.Label,
                        {
                          optional: true,
                          "data-testid": "product-organization-form-categories-label",
                          children: t("products.fields.categories.label")
                        }
                      ),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-organization-form-categories-control", children: /* @__PURE__ */ jsx(
                        CategoryCombobox,
                        {
                          ...field,
                          "data-testid": "product-organization-form-categories-combobox"
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-organization-form-categories-error" })
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "tag_ids",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-organization-form-tags-item", children: [
                      /* @__PURE__ */ jsx(
                        Form.Label,
                        {
                          optional: true,
                          "data-testid": "product-organization-form-tags-label",
                          children: t("products.fields.tags.label")
                        }
                      ),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-organization-form-tags-control", children: /* @__PURE__ */ jsx(
                        Combobox,
                        {
                          ...field,
                          multiple: true,
                          options: tags.options,
                          onSearchValueChange: tags.onSearchValueChange,
                          searchValue: tags.searchValue,
                          "data-testid": "product-organization-form-tags-combobox"
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-organization-form-tags-error" })
                    ] });
                  }
                }
              )
            ]
          }
        ) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-organization-form-footer", children: /* @__PURE__ */ jsxs(
          "div",
          {
            className: "flex items-center justify-end gap-x-2",
            "data-testid": "product-organization-form-footer-actions",
            children: [
              /* @__PURE__ */ jsx(
                RouteDrawer.Close,
                {
                  asChild: true,
                  "data-testid": "product-organization-form-cancel-button-wrapper",
                  children: /* @__PURE__ */ jsx(
                    Button,
                    {
                      size: "small",
                      variant: "secondary",
                      "data-testid": "product-organization-form-cancel-button",
                      children: t("actions.cancel")
                    }
                  )
                }
              ),
              /* @__PURE__ */ jsx(
                Button,
                {
                  size: "small",
                  type: "submit",
                  isLoading: isPending,
                  "data-testid": "product-organization-form-save-button",
                  children: t("actions.save")
                }
              )
            ]
          }
        ) })
      ]
    }
  ) });
};

// src/pages/products/product-organization/product-organization.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ProductOrganization = () => {
  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-organization-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "product-organization-drawer-header", children: /* @__PURE__ */ jsx2(
      RouteDrawer.Title,
      {
        asChild: true,
        "data-testid": "product-organization-drawer-title",
        children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "product-organization-drawer-title-text", children: t("products.organization.edit.header") })
      }
    ) }),
    !isLoading && product && /* @__PURE__ */ jsx2(ProductOrganizationForm, { product })
  ] });
};
export {
  ProductOrganization as Component
};
