import {
  PRODUCT_DETAIL_QUERY
} from "./chunk-35NDMIYZ.js";
import {
  ChipInput
} from "./chunk-NCYTM57J.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useProduct,
  useUpdateProductOption
} from "./chunk-BA2HXHTW.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/products/product-edit-option/components/edit-product-option-form/edit-product-option-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var CreateProductOptionSchema = z.object({
  title: z.string().min(1),
  values: z.array(z.string()).optional()
});
var CreateProductOptionForm = ({
  option
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      title: option.title,
      values: option.values.map((v) => v.value)
    },
    resolver: zodResolver(CreateProductOptionSchema)
  });
  const { mutateAsync, isPending } = useUpdateProductOption(
    option.product_id,
    option.id
  );
  const handleSubmit = form.handleSubmit(async (values) => {
    mutateAsync(
      {
        id: option.id,
        ...values
      },
      {
        onSuccess: () => {
          handleSuccess();
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "product-edit-option-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      "data-testid": "product-edit-option-keybound-form",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-4 overflow-auto", "data-testid": "product-edit-option-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "title",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-option-form-title-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-edit-option-form-title-label", children: t("products.fields.options.optionTitle") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-option-form-title-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-option-form-title-input-wrapper", children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...field,
                      placeholder: t(
                        "products.fields.options.optionTitlePlaceholder"
                      ),
                      "data-testid": "product-edit-option-form-title-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-option-form-title-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "values",
              render: ({ field: { ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "product-edit-option-form-values-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "product-edit-option-form-values-label", children: t("products.fields.options.variations") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "product-edit-option-form-values-control", children: /* @__PURE__ */ jsx("div", { "data-testid": "product-edit-option-form-values-chip-input-wrapper", children: /* @__PURE__ */ jsx(
                    ChipInput,
                    {
                      ...field,
                      placeholder: t(
                        "products.fields.options.variantionsPlaceholder"
                      ),
                      "data-testid": "product-edit-option-form-values-chip-input"
                    }
                  ) }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "product-edit-option-form-values-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "product-edit-option-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", "data-testid": "product-edit-option-form-footer-actions", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, "data-testid": "product-edit-option-form-cancel-button-wrapper", children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", "data-testid": "product-edit-option-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading: isPending, "data-testid": "product-edit-option-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/products/product-edit-option/product-edit-option.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ProductEditOption = () => {
  const { id, option_id } = useParams();
  const { t } = useTranslation2();
  const { product, isPending, isFetching, isError, error } = useProduct(id, PRODUCT_DETAIL_QUERY);
  const option = product?.options?.find((o) => o.id === option_id);
  if (!isPending && !isFetching && !option) {
    throw new Response(JSON.stringify({ message: `An option with ID ${option_id} was not found` }), { status: 404, headers: { "Content-Type": "application/json" } });
  }
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { prev: "../../..", "data-testid": "product-edit-option-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "product-edit-option-drawer-header", children: /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, "data-testid": "product-edit-option-drawer-title", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "product-edit-option-drawer-title-text", children: t("products.options.edit.header") }) }) }),
    option && /* @__PURE__ */ jsx2(CreateProductOptionForm, { option })
  ] });
};
export {
  ProductEditOption as Component
};
