import {
  getCurrencySymbol
} from "./chunk-IQPN4PZJ.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 {
  useCampaign,
  useUpdateCampaign
} from "./chunk-MR6REQKT.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/campaigns/[id]/edit-budget/edit-campaign-budget-form/edit-campaign-budget-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, CurrencyInput, Input, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditCampaignSchema = zod.object({
  limit: zod.number().min(0).optional().nullable()
});
var EditCampaignBudgetForm = ({
  campaign
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      limit: campaign?.budget?.limit || void 0
    },
    resolver: zodResolver(EditCampaignSchema)
  });
  const { mutateAsync, isPending } = useUpdateCampaign(campaign.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        budget: {
          limit: data.limit ? data.limit : null
        }
      },
      {
        onSuccess: ({ campaign: campaign2 }) => {
          toast.success(
            t("campaigns.edit.successToast", {
              name: campaign2.name
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex flex-1 flex-col", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsx(
      Form.Field,
      {
        control: form.control,
        name: "limit",
        render: ({ field: { onChange, value, ...field } }) => {
          return /* @__PURE__ */ jsxs(Form.Item, { className: "basis-1/2", children: [
            /* @__PURE__ */ jsx(Form.Label, { children: t("campaigns.budget.fields.limit") }),
            /* @__PURE__ */ jsx(Form.Control, { children: campaign.budget?.type === "spend" ? /* @__PURE__ */ jsx(
              CurrencyInput,
              {
                min: 0,
                onValueChange: (value2) => onChange(value2 ? parseInt(value2) : null),
                code: campaign.budget?.currency_code,
                symbol: campaign.budget?.currency_code ? getCurrencySymbol(
                  campaign.budget?.currency_code
                ) : "",
                ...field,
                value: value || void 0
              }
            ) : /* @__PURE__ */ jsx(
              Input,
              {
                min: 0,
                ...field,
                value: value || void 0,
                onChange: (e) => {
                  onChange(
                    e.target.value === "" ? null : parseInt(e.target.value)
                  );
                }
              },
              "usage"
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] });
        }
      }
    ) }) }),
    /* @__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, { variant: "secondary", size: "small", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(
        Button,
        {
          isLoading: isPending,
          type: "submit",
          variant: "primary",
          size: "small",
          children: t("actions.save")
        }
      )
    ] }) })
  ] }) });
};

// src/pages/campaigns/[id]/edit-budget/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Component = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { campaign, isLoading, isError, error } = useCampaign(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { children: t("campaigns.budget.edit.header") }) }) }),
    !isLoading && campaign && /* @__PURE__ */ jsx2(EditCampaignBudgetForm, { campaign })
  ] });
};
export {
  Component
};
