import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useCampaign,
  useUpdateCampaign
} from "./chunk-ZN7LWXVC.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/campaigns/campaign-configuration/campaign-configuration.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/campaigns/campaign-configuration/components/campaign-configuration-form/campaign-configuration-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, DatePicker, 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 CampaignConfigurationSchema = z.object({
  starts_at: z.date().nullable(),
  ends_at: z.date().nullable()
});
var CampaignConfigurationForm = ({
  campaign
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      starts_at: campaign.starts_at ? new Date(campaign.starts_at) : void 0,
      ends_at: campaign.ends_at ? new Date(campaign.ends_at) : void 0
    },
    resolver: zodResolver(CampaignConfigurationSchema)
  });
  const { mutateAsync, isPending } = useUpdateCampaign(campaign.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        starts_at: data.starts_at || null,
        ends_at: data.ends_at || null
      },
      {
        onSuccess: ({ campaign: campaign2 }) => {
          toast.success(
            t("campaigns.configuration.edit.successToast", {
              name: campaign2.name
            })
          );
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "campaign-configuration-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex flex-1 flex-col", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": "campaign-configuration-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "starts_at",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "campaign-configuration-form-starts-at-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "campaign-configuration-form-starts-at-label", children: t("campaigns.fields.start_date") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "campaign-configuration-form-starts-at-control", children: /* @__PURE__ */ jsx(
                DatePicker,
                {
                  granularity: "minute",
                  hourCycle: 12,
                  shouldCloseOnSelect: false,
                  ...field,
                  "data-testid": "campaign-configuration-form-starts-at-input"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "campaign-configuration-form-starts-at-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "ends_at",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "campaign-configuration-form-ends-at-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "campaign-configuration-form-ends-at-label", children: t("campaigns.fields.end_date") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "campaign-configuration-form-ends-at-control", children: /* @__PURE__ */ jsx(
                DatePicker,
                {
                  granularity: "minute",
                  shouldCloseOnSelect: false,
                  ...field,
                  "data-testid": "campaign-configuration-form-ends-at-input"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "campaign-configuration-form-ends-at-error" })
            ] });
          }
        }
      )
    ] }) }),
    /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "campaign-configuration-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, { variant: "secondary", size: "small", "data-testid": "campaign-configuration-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(
        Button,
        {
          isLoading: isPending,
          type: "submit",
          variant: "primary",
          size: "small",
          "data-testid": "campaign-configuration-form-save-button",
          children: t("actions.save")
        }
      )
    ] }) })
  ] }) });
};

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