import {
  PriceListStatus,
  PriceListType
} from "./chunk-6IJMSVC6.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  usePriceList,
  useUpdatePriceList
} from "./chunk-P7UBW2EI.js";
import "./chunk-II3CLUXA.js";
import "./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/price-lists/[id]/edit/index.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/price-lists/[id]/edit/price-list-edit-form/edit-price-list-form.tsx
import {
  Button,
  Input,
  RadioGroup,
  Select,
  Textarea,
  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 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 form = useExtendableForm({
    schema: PriceListEditSchema,
    model: "price_list",
    zone: "edit",
    data: priceList,
    defaultValues: {
      type: priceList.type,
      title: priceList.title,
      description: priceList.description,
      status: priceList.status
    }
  });
  const { mutateAsync, isPending } = useUpdatePriceList(priceList.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    const { status, type, title, description } = values;
    await mutateAsync({ status, type, title, description }, {
      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, 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", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "type",
              render: ({ field: { onChange, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsxs("div", { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("priceLists.fields.type.label") }),
                    /* @__PURE__ */ jsx(Form.Hint, { children: t("priceLists.fields.type.hint") })
                  ] }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(RadioGroup, { ...field, onValueChange: onChange, children: [
                    /* @__PURE__ */ jsx(
                      RadioGroup.ChoiceBox,
                      {
                        value: "sale" /* SALE */,
                        label: t("priceLists.fields.type.options.sale.label"),
                        description: t(
                          "priceLists.fields.type.options.sale.description"
                        )
                      }
                    ),
                    /* @__PURE__ */ jsx(
                      RadioGroup.ChoiceBox,
                      {
                        value: "override" /* OVERRIDE */,
                        label: t(
                          "priceLists.fields.type.options.override.label"
                        ),
                        description: t(
                          "priceLists.fields.type.options.override.description"
                        )
                      }
                    )
                  ] }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__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, { 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: "status",
                render: ({ field: { onChange, ref, ...field } }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("priceLists.fields.status.label") }),
                    /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(Select, { ...field, onValueChange: onChange, children: [
                      /* @__PURE__ */ jsx(Select.Trigger, { ref, children: /* @__PURE__ */ jsx(Select.Value, {}) }),
                      /* @__PURE__ */ jsxs(Select.Content, { children: [
                        /* @__PURE__ */ jsx(Select.Item, { value: "active" /* ACTIVE */, children: t("priceLists.fields.status.options.active") }),
                        /* @__PURE__ */ jsx(Select.Item, { value: "draft" /* DRAFT */, children: t("priceLists.fields.status.options.draft") })
                      ] })
                    ] }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "description",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("fields.description") }),
                    /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Textarea, { ...field }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] });
                }
              }
            )
          ] }),
          /* @__PURE__ */ jsx(
            FormExtensionZone,
            {
              model: "price_list",
              zone: "edit",
              control: form.control,
              data: priceList
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { className: "shrink-0", 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", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/price-lists/[id]/edit/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Component = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const linkQuery = useLinkQuery("price_list");
  const { price_list, isLoading, isError, error } = usePriceList(id, linkQuery);
  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 {
  Component
};
