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]/configuration/index.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/price-lists/[id]/configuration/price-list-configuration-form/price-list-configuration-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import {
  Button,
  DatePicker,
  Divider,
  toast
} from "@medusajs/ui";
import { useFieldArray, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var PriceListConfigurationSchema = z.object({
  ends_at: z.date().nullable(),
  starts_at: z.date().nullable(),
  customer_group_id: z.array(
    z.object({
      id: z.string(),
      name: z.string()
    })
  )
});
var PriceListConfigurationForm = ({
  priceList,
  customerGroups
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      ends_at: priceList.ends_at ? new Date(priceList.ends_at) : null,
      starts_at: priceList.starts_at ? new Date(priceList.starts_at) : null,
      customer_group_id: customerGroups
    },
    resolver: zodResolver(PriceListConfigurationSchema)
  });
  const { remove: _remove } = useFieldArray({
    control: form.control,
    name: "customer_group_id",
    keyName: "cg_id"
  });
  const { mutateAsync } = useUpdatePriceList(priceList.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    const rules = { ...priceList.rules };
    await mutateAsync(
      {
        starts_at: values.starts_at?.toISOString() || null,
        ends_at: values.ends_at?.toISOString() || null,
        rules
      },
      {
        onSuccess: () => {
          toast.success(t("priceLists.configuration.edit.successToast"));
          handleSuccess();
        },
        onError: (error) => toast.error(error.message)
      }
    );
  });
  return /* @__PURE__ */ jsxs(RouteDrawer.Form, { form, children: [
    /* @__PURE__ */ jsx(RouteDrawer.Description, { className: "sr-only", children: t("priceLists.configuration.edit.description") }),
    /* @__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-8 overflow-auto", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "starts_at",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-3", children: [
                      /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                        /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("priceLists.fields.startsAt.label") }),
                        /* @__PURE__ */ jsx(Form.Hint, { children: t("priceLists.fields.startsAt.hint") })
                      ] }),
                      /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                        DatePicker,
                        {
                          granularity: "minute",
                          shouldCloseOnSelect: false,
                          ...field
                        }
                      ) })
                    ] }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(Divider, {}),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "ends_at",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { children: [
                    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-3", children: [
                      /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
                        /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("priceLists.fields.endsAt.label") }),
                        /* @__PURE__ */ jsx(Form.Hint, { children: t("priceLists.fields.endsAt.hint") })
                      ] }),
                      /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                        DatePicker,
                        {
                          granularity: "minute",
                          shouldCloseOnSelect: false,
                          ...field
                        }
                      ) })
                    ] }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                  ] });
                }
              }
            )
          ] }),
          /* @__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", children: t("actions.save") })
          ] }) })
        ]
      }
    )
  ] });
};

// src/pages/price-lists/[id]/configuration/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Component = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { price_list, isPending, isError, error } = usePriceList(id);
  const ready = !isPending && !!price_list;
  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("priceLists.configuration.edit.header") }) }) }),
    ready && /* @__PURE__ */ jsx2(
      PriceListConfigurationForm,
      {
        priceList: price_list,
        customerGroups: []
      }
    )
  ] });
};
export {
  Component
};
