import {
  DeprecatedPercentageInput
} from "./chunk-MAEP2UCK.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 {
  usePromotion,
  useUpdatePromotion
} from "./chunk-MR6REQKT.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/promotions/[id]/edit/index.tsx
import { Heading } from "@medusajs/ui";
import { useLinkQuery } from "@mercurjs/dashboard-shared";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/promotions/[id]/edit/edit-promotion-form/edit-promotion-details-form.tsx
import { Button, Input, RadioGroup, Text } from "@medusajs/ui";
import { Trans, useTranslation } from "react-i18next";
import * as zod from "zod";
import {
  FormExtensionZone,
  useExtendableForm
} from "@mercurjs/dashboard-shared";
import { jsx, jsxs } from "react/jsx-runtime";
var EditPromotionSchema = zod.object({
  is_automatic: zod.string().toLowerCase(),
  code: zod.string().min(1),
  status: zod.enum(["active", "inactive", "draft"]),
  value_type: zod.enum(["fixed", "percentage"]),
  value: zod.number(),
  max_quantity: zod.number().min(1).optional().nullable()
});
var EditPromotionDetailsForm = ({ promotion }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useExtendableForm({
    schema: EditPromotionSchema,
    model: "promotion",
    zone: "edit",
    data: promotion,
    defaultValues: {
      is_automatic: promotion.is_automatic.toString(),
      code: promotion.code,
      status: promotion.status,
      value: promotion.application_method.value,
      value_type: promotion.application_method.type,
      max_quantity: promotion.application_method?.max_quantity ?? 1
    }
  });
  const { mutateAsync, isPending } = useUpdatePromotion(promotion.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        is_automatic: data.is_automatic === "true",
        code: data.code,
        status: data.status,
        application_method: {
          value: data.value,
          max_quantity: data.max_quantity
        }
      },
      {
        onSuccess: () => {
          handleSuccess();
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-8", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "status",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("promotions.form.status.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(
                    RadioGroup,
                    {
                      className: "flex-col gap-y-3",
                      ...field,
                      value: field.value,
                      onValueChange: field.onChange,
                      children: [
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "draft",
                            label: t("promotions.form.status.draft.title"),
                            description: t("promotions.form.status.draft.description")
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "active",
                            label: t("promotions.form.status.active.title"),
                            description: t("promotions.form.status.active.description")
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "inactive",
                            label: t("promotions.form.status.inactive.title"),
                            description: t("promotions.form.status.inactive.description")
                          }
                        )
                      ]
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "is_automatic",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("promotions.form.method.label") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(
                    RadioGroup,
                    {
                      className: "flex-col gap-y-3",
                      ...field,
                      value: field.value,
                      onValueChange: field.onChange,
                      children: [
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "false",
                            label: t("promotions.form.method.code.title"),
                            description: t("promotions.form.method.code.description")
                          }
                        ),
                        /* @__PURE__ */ jsx(
                          RadioGroup.ChoiceBox,
                          {
                            value: "true",
                            label: t("promotions.form.method.automatic.title"),
                            description: t("promotions.form.method.automatic.description")
                          }
                        )
                      ]
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsx(Form.Label, { children: t("promotions.form.code.title") }),
                    /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Text,
              {
                size: "small",
                leading: "compact",
                className: "text-ui-fg-subtle",
                children: /* @__PURE__ */ jsx(
                  Trans,
                  {
                    t,
                    i18nKey: "promotions.form.code.description",
                    components: [/* @__PURE__ */ jsx("br", {}, "break")]
                  }
                )
              }
            )
          ] }),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "value",
              render: ({ field: { onChange, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("fields.percentage") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                    DeprecatedPercentageInput,
                    {
                      min: 0,
                      max: 100,
                      ...field,
                      value: field.value || "",
                      onChange: (e) => {
                        onChange(e.target.value === "" ? null : parseInt(e.target.value));
                      }
                    },
                    "amount"
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          promotion.application_method?.allocation === "each" && /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "max_quantity",
              render: () => {
                return /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { children: t("promotions.form.max_quantity.title") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                    Input,
                    {
                      ...form.register("max_quantity", {
                        valueAsNumber: true
                      }),
                      type: "number",
                      min: 1,
                      placeholder: "999"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(
                    Text,
                    {
                      size: "small",
                      leading: "compact",
                      className: "text-ui-fg-subtle",
                      children: /* @__PURE__ */ jsx(
                        Trans,
                        {
                          t,
                          i18nKey: "promotions.form.max_quantity.description",
                          components: [/* @__PURE__ */ jsx("br", {}, "break")]
                        }
                      )
                    }
                  ),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            FormExtensionZone,
            {
              model: "promotion",
              zone: "edit",
              control: form.control,
              data: promotion
            }
          )
        ] }) }),
        /* @__PURE__ */ jsx(RouteDrawer.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",
              children: t("actions.cancel")
            }
          ) }),
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "small",
              type: "submit",
              isLoading: isPending,
              children: t("actions.save")
            }
          )
        ] }) })
      ]
    }
  ) });
};

// src/pages/promotions/[id]/edit/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Component = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const linkQuery = useLinkQuery("promotion");
  const { promotion, isLoading, isError, error } = usePromotion(id, linkQuery);
  if (isError) throw error;
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(Heading, { children: t("promotions.edit.title") }) }),
    !isLoading && promotion && /* @__PURE__ */ jsx2(EditPromotionDetailsForm, { promotion })
  ] });
};
export {
  Component
};
