import {
  PriceListStatus,
  PriceListType
} from "./chunk-6IJMSVC6.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  useDocumentDirection
} from "./chunk-Y7QKP6QU.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  usePriceList,
  useUpdatePriceList
} from "./chunk-DBNPVU3L.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/price-lists/price-list-edit/components/price-list-edit-form/edit-price-list-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import {
  Button,
  Input,
  RadioGroup,
  Select,
  Textarea,
  toast
} 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 PriceListEditSchema = z.object({
  status: z.nativeEnum(PriceListStatus),
  type: z.nativeEnum(PriceListType),
  title: z.string().min(1),
  description: z.string().min(1)
});
var PriceListEditForm = ({ priceList }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const direction = useDocumentDirection();
  const form = useForm({
    defaultValues: {
      type: priceList.type,
      title: priceList.title,
      description: priceList.description,
      status: priceList.status
    },
    resolver: zodResolver(PriceListEditSchema)
  });
  const { mutateAsync, isPending } = useUpdatePriceList(priceList.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(values, {
      onSuccess: ({ price_list }) => {
        toast.success(
          t("priceLists.edit.successToast", {
            title: price_list.title
          })
        );
        handleSuccess();
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "price-list-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      className: "flex flex-1 flex-col overflow-hidden",
      onSubmit: handleSubmit,
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-6 overflow-auto", "data-testid": "price-list-edit-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "type",
              render: ({ field: { onChange, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "price-list-edit-form-type-item", children: [
                  /* @__PURE__ */ jsxs("div", { children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "price-list-edit-form-type-label", children: t("priceLists.fields.type.label") }),
                    /* @__PURE__ */ jsx(Form.Hint, { "data-testid": "price-list-edit-form-type-hint", children: t("priceLists.fields.type.hint") })
                  ] }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "price-list-edit-form-type-control", children: /* @__PURE__ */ jsxs(
                    RadioGroup,
                    {
                      dir: direction,
                      ...field,
                      onValueChange: onChange,
                      "data-testid": "price-list-edit-form-type-radio-group",
                      children: [
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "sale" /* SALE */,
                            label: t("priceLists.fields.type.options.sale.label"),
                            description: t(
                              "priceLists.fields.type.options.sale.description"
                            ),
                            "data-testid": "price-list-edit-form-type-option-sale"
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "override" /* OVERRIDE */,
                            label: t(
                              "priceLists.fields.type.options.override.label"
                            ),
                            description: t(
                              "priceLists.fields.type.options.override.description"
                            ),
                            "data-testid": "price-list-edit-form-type-option-override"
                          }
                        )
                      ]
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "price-list-edit-form-type-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "title",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "price-list-edit-form-title-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "price-list-edit-form-title-label", children: t("fields.title") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "price-list-edit-form-title-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "price-list-edit-form-title-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "price-list-edit-form-title-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "status",
                render: ({ field: { onChange, ref, ...field } }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "price-list-edit-form-status-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "price-list-edit-form-status-label", children: t("priceLists.fields.status.label") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "price-list-edit-form-status-control", children: /* @__PURE__ */ jsxs(
                      Select,
                      {
                        dir: direction,
                        ...field,
                        onValueChange: onChange,
                        "data-testid": "price-list-edit-form-status-select",
                        children: [
                          /* @__PURE__ */ jsx(Select.Trigger, { ref, children: /* @__PURE__ */ jsx(Select.Value, {}) }),
                          /* @__PURE__ */ jsxs(Select.Content, { children: [
                            /* @__PURE__ */ jsx(Select.Item, { value: "active" /* ACTIVE */, "data-testid": "price-list-edit-form-status-option-active", children: t("priceLists.fields.status.options.active") }),
                            /* @__PURE__ */ jsx(Select.Item, { value: "draft" /* DRAFT */, "data-testid": "price-list-edit-form-status-option-draft", children: t("priceLists.fields.status.options.draft") })
                          ] })
                        ]
                      }
                    ) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "price-list-edit-form-status-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "description",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "price-list-edit-form-description-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "price-list-edit-form-description-label", children: t("fields.description") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "price-list-edit-form-description-control", children: /* @__PURE__ */ jsx(Textarea, { ...field, "data-testid": "price-list-edit-form-description-input" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "price-list-edit-form-description-error" })
                  ] });
                }
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { className: "shrink-0", "data-testid": "price-list-edit-form-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, { size: "small", variant: "secondary", "data-testid": "price-list-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "price-list-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/price-lists/price-list-edit/price-list-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var PriceListEdit = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { price_list, isLoading, isError, error } = usePriceList(id);
  const ready = !isLoading && price_list;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(Heading, { children: t("priceLists.edit.header") }) }),
    ready && /* @__PURE__ */ jsx2(PriceListEditForm, { priceList: price_list })
  ] });
};
export {
  PriceListEdit as Component
};
