import {
  VisuallyHidden
} from "./chunk-54TMLQH3.js";
import {
  CreateCampaignFormFields
} from "./chunk-YTPCWCA3.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  useCreateCampaign
} from "./chunk-HAFZQK53.js";

// src/pages/campaigns/campaign-create/components/create-campaign-form/create-campaign-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";

// src/pages/campaigns/common/constants.ts
var DEFAULT_CAMPAIGN_VALUES = {
  name: "",
  description: "",
  campaign_identifier: "",
  starts_at: null,
  ends_at: null,
  budget: {
    type: "usage",
    currency_code: null,
    limit: null,
    attribute: null
  }
};

// src/pages/campaigns/campaign-create/components/create-campaign-form/create-campaign-form.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var CreateCampaignSchema = zod.object({
  name: zod.string().min(1),
  description: zod.string().optional(),
  campaign_identifier: zod.string().min(1),
  starts_at: zod.date().nullable(),
  ends_at: zod.date().nullable(),
  budget: zod.object({
    attribute: zod.string().nullish(),
    limit: zod.number().min(0).nullish(),
    type: zod.enum(["spend", "usage", "use_by_attribute"]),
    currency_code: zod.string().nullish()
  })
});
var CreateCampaignForm = () => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const { mutateAsync, isPending } = useCreateCampaign();
  const form = useForm({
    defaultValues: DEFAULT_CAMPAIGN_VALUES,
    resolver: zodResolver(CreateCampaignSchema)
  });
  const handleSubmit = form.handleSubmit(async (data) => {
    const attribute = data.budget.attribute || null;
    const type = attribute ? "use_by_attribute" : data.budget.type;
    await mutateAsync(
      {
        name: data.name,
        description: data.description,
        campaign_identifier: data.campaign_identifier,
        starts_at: data.starts_at,
        ends_at: data.ends_at,
        budget: {
          type,
          attribute,
          limit: data.budget.limit ? data.budget.limit : void 0,
          currency_code: data.budget.currency_code
        }
      },
      {
        onSuccess: ({ campaign }) => {
          toast.success(
            t("campaigns.create.successToast", {
              name: campaign.name
            })
          );
          handleSuccess(`/campaigns/${campaign.id}`);
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "campaign-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex size-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteFocusModal.Header, { "data-testid": "campaign-create-form-header", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(VisuallyHidden, { children: t("campaigns.create.title") }) }),
          /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(VisuallyHidden, { children: t("campaigns.create.description") }) })
        ] }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex size-full flex-col items-center overflow-auto py-16", "data-testid": "campaign-create-form-body", children: /* @__PURE__ */ jsx(CreateCampaignFormFields, { form }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "campaign-create-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "campaign-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "small",
              variant: "primary",
              type: "submit",
              isLoading: isPending,
              "data-testid": "campaign-create-form-create-button",
              children: t("actions.create")
            }
          )
        ] }) })
      ]
    }
  ) });
};

export {
  DEFAULT_CAMPAIGN_VALUES,
  CreateCampaignSchema,
  CreateCampaignForm
};
