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

// src/pages/stores/store-closure-edit/store-closure-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/stores/store-closure-edit/components/store-closure-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 StoreClosureSchema = z.object({
  closed_from: z.date({ required_error: "Close from date is required" }),
  closed_to: z.date().nullable()
}).refine((data) => data.closed_from >= new Date((/* @__PURE__ */ new Date()).toDateString()), {
  message: "Close from date cannot be in the past",
  path: ["closed_from"]
});
var StoreClosureForm = ({ seller }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      closed_from: seller.closed_from ? new Date(seller.closed_from) : null,
      closed_to: seller.closed_to ? new Date(seller.closed_to) : null
    },
    resolver: zodResolver(StoreClosureSchema)
  });
  const { mutateAsync, isPending } = useUpdateSeller(seller.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(
      {
        closed_from: data.closed_from || null,
        closed_to: data.closed_to || null
      },
      {
        onSuccess: () => {
          toast.success(
            t("store.scheduledClosure.edit.successToast")
          );
          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__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "closed_from",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { children: [
              /* @__PURE__ */ jsx(Form.Label, { children: t("store.scheduledClosure.fields.closedFrom") }),
              /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                DatePicker,
                {
                  granularity: "minute",
                  hourCycle: 12,
                  shouldCloseOnSelect: false,
                  ...field
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, {})
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "closed_to",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { children: [
              /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.scheduledClosure.fields.closedTo") }),
              /* @__PURE__ */ jsx(Form.Hint, { children: t("store.scheduledClosure.fields.closedToHint") }),
              /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                DatePicker,
                {
                  granularity: "minute",
                  shouldCloseOnSelect: false,
                  ...field
                }
              ) }),
              /* @__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/stores/store-closure-edit/store-closure-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var StoreClosureEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { seller, isLoading, isError, error } = useSeller(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("store.scheduledClosure.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { className: "sr-only", children: t("store.scheduledClosure.edit.description") })
    ] }),
    !isLoading && seller && /* @__PURE__ */ jsx2(StoreClosureForm, { seller })
  ] });
};
export {
  StoreClosureEdit as Component
};
