"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { Globe, Repeat } from "lucide-react";
import { cn } from "@open-mercato/shared/lib/utils";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Button } from "@open-mercato/ui/primitives/button";
import { DatePicker } from "@open-mercato/ui/primitives/date-picker";
import { TimePicker } from "@open-mercato/ui/backend/inputs/TimePicker";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { isVisible, getFieldLabel } from "./fieldConfig.js";
function parseIsoDate(value) {
  if (!value) return null;
  const parts = value.split("-");
  if (parts.length !== 3) return null;
  const [y, m, d] = parts.map((p) => parseInt(p, 10));
  if (!Number.isFinite(y) || !Number.isFinite(m) || !Number.isFinite(d)) return null;
  const date = new Date(y, m - 1, d);
  return Number.isNaN(date.getTime()) ? null : date;
}
function formatIsoDate(date) {
  if (!date) return "";
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, "0");
  const d = String(date.getDate()).padStart(2, "0");
  return `${y}-${m}-${d}`;
}
const DURATION_OPTIONS = [
  { value: 15, key: "customers.schedule.duration.option.15min", fallback: "15 min" },
  { value: 30, key: "customers.schedule.duration.option.30min", fallback: "30 min" },
  { value: 45, key: "customers.schedule.duration.option.45min", fallback: "45 min" },
  { value: 60, key: "customers.schedule.duration.option.1hour", fallback: "1 hour" },
  { value: 90, key: "customers.schedule.duration.option.1h30m", fallback: "1h 30m" },
  { value: 120, key: "customers.schedule.duration.option.2hours", fallback: "2 hours" }
];
const DAYS_OF_WEEK = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
function DateTimeFields({
  visible,
  activityType,
  date,
  setDate,
  startTime,
  setStartTime,
  duration,
  setDuration,
  allDay,
  setAllDay,
  recurrenceEnabled,
  setRecurrenceEnabled,
  recurrenceDays,
  toggleRecurrenceDay,
  recurrenceEndType,
  setRecurrenceEndType,
  recurrenceCount,
  setRecurrenceCount,
  recurrenceEndDate,
  setRecurrenceEndDate
}) {
  const t = useT();
  if (!visible.has("date")) return null;
  const showStartTime = isVisible(activityType, "startTime");
  const showDuration = isVisible(activityType, "duration");
  const showAllDay = isVisible(activityType, "allDay");
  const showRecurrence = isVisible(activityType, "recurrence");
  const dateMissing = !date.trim();
  const timeMissing = showStartTime && !allDay && !startTime.trim();
  const dateErrorId = "schedule-date-error";
  const timeErrorId = "schedule-time-error";
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap gap-3", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-[1.5] flex-col gap-1.5", children: [
        /* @__PURE__ */ jsxs("label", { className: "text-overline font-semibold text-muted-foreground tracking-wider", children: [
          getFieldLabel(activityType, "date", t, "customers.schedule.date", "Date"),
          /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "ml-1 text-status-error-foreground", children: "*" })
        ] }),
        /* @__PURE__ */ jsx(
          DatePicker,
          {
            value: parseIsoDate(date),
            onChange: (next) => setDate(formatIsoDate(next)),
            placeholder: t("customers.schedule.date.placeholder", "Pick a date"),
            required: true,
            "aria-describedby": dateMissing ? dateErrorId : void 0,
            className: cn(
              "h-10",
              dateMissing && "border-status-error-border"
            )
          }
        ),
        dateMissing ? /* @__PURE__ */ jsx("p", { id: dateErrorId, className: "text-xs text-status-error-foreground", children: t("customers.activities.errors.dateRequired", "Date is required") }) : null
      ] }),
      showStartTime && /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1.5", children: [
        /* @__PURE__ */ jsxs("label", { className: "text-overline font-semibold text-muted-foreground tracking-wider", children: [
          getFieldLabel(activityType, "startTime", t, "customers.schedule.start", "Start"),
          /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "ml-1 text-status-error-foreground", children: "*" })
        ] }),
        /* @__PURE__ */ jsx(
          TimePicker,
          {
            value: startTime || null,
            onChange: (next) => setStartTime(next ?? ""),
            disabled: allDay,
            placeholder: t("customers.schedule.start.placeholder", "Pick a time"),
            className: cn(
              "py-2.5",
              timeMissing ? "border-status-error-border" : void 0
            ),
            showNowButton: true,
            showClearButton: false
          }
        ),
        timeMissing ? /* @__PURE__ */ jsx("p", { id: timeErrorId, className: "text-xs text-status-error-foreground", children: t("customers.activities.errors.timeRequired", "Time is required") }) : null
      ] }),
      showDuration && /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1.5", children: [
        /* @__PURE__ */ jsx("label", { className: "text-overline font-semibold text-muted-foreground tracking-wider", children: getFieldLabel(activityType, "duration", t, "customers.schedule.duration", "Duration") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            value: String(duration),
            onValueChange: (next) => {
              const parsed = Number(next);
              if (Number.isFinite(parsed)) setDuration(parsed);
            },
            disabled: allDay,
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { className: "h-10", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("customers.schedule.duration.placeholder", "Pick duration") }) }),
              /* @__PURE__ */ jsx(SelectContent, { children: DURATION_OPTIONS.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: String(option.value), children: t(option.key, option.fallback) }, option.value)) })
            ]
          }
        )
      ] })
    ] }),
    showAllDay && /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-3.5 text-xs text-muted-foreground", children: [
      /* @__PURE__ */ jsxs("label", { className: "flex items-center gap-2 cursor-pointer", children: [
        /* @__PURE__ */ jsx("input", { type: "checkbox", checked: allDay, onChange: (e) => setAllDay(e.target.checked), className: "rounded" }),
        t("customers.schedule.allDay", "All day")
      ] }),
      /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "\xB7" }),
      /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1.5", children: [
        /* @__PURE__ */ jsx(Globe, { className: "size-3.5" }),
        Intl.DateTimeFormat().resolvedOptions().timeZone,
        " (GMT",
        (/* @__PURE__ */ new Date()).getTimezoneOffset() <= 0 ? "+" : "-",
        String(Math.abs(Math.floor((/* @__PURE__ */ new Date()).getTimezoneOffset() / 60))).padStart(1, "0"),
        ")"
      ] }),
      showRecurrence && /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "\xB7" }),
        /* @__PURE__ */ jsxs(
          Button,
          {
            type: "button",
            variant: "ghost",
            size: "sm",
            onClick: () => setRecurrenceEnabled(!recurrenceEnabled),
            className: cn("h-auto flex items-center gap-1.5", recurrenceEnabled && "font-medium text-foreground"),
            children: [
              /* @__PURE__ */ jsx(Repeat, { className: "size-3.5" }),
              recurrenceEnabled ? t("customers.schedule.recurrence.active", "Repeats") : t("customers.schedule.recurrence.none", "No repeat")
            ]
          }
        )
      ] })
    ] }),
    showRecurrence && recurrenceEnabled && /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-status-warning-border bg-status-warning-bg p-4 space-y-3", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
        /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-2 text-sm font-semibold text-foreground", children: [
          /* @__PURE__ */ jsx(Repeat, { className: "size-3.5" }),
          t("customers.schedule.recurrence.title", "Recurrence")
        ] }),
        /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", size: "sm", className: "h-auto text-xs font-medium text-foreground", children: t("customers.schedule.recurrence.edit", "Edit") })
      ] }),
      /* @__PURE__ */ jsx("div", { className: "flex gap-2", children: DAYS_OF_WEEK.map((day, i) => /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          variant: "ghost",
          size: "sm",
          onClick: () => toggleRecurrenceDay(i),
          className: cn(
            "h-auto flex size-8 items-center justify-center rounded-full text-xs font-medium transition-colors p-0",
            recurrenceDays[i] ? "bg-primary text-primary-foreground" : "border border-border bg-background text-muted-foreground hover:bg-muted"
          ),
          children: day.slice(0, 2)
        },
        day
      )) }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-xs text-muted-foreground", children: [
        /* @__PURE__ */ jsxs("span", { children: [
          t("customers.schedule.recurrence.ends", "Ends"),
          ":"
        ] }),
        /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: () => setRecurrenceEndType("never"), className: cn("h-auto rounded-full px-3 py-1 text-xs font-medium", recurrenceEndType === "never" ? "bg-background border border-border text-foreground" : "text-muted-foreground"), children: t("customers.schedule.recurrence.never", "Never") }),
        /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: () => setRecurrenceEndType("count"), className: cn("h-auto rounded-full px-3 py-1 text-xs font-medium", recurrenceEndType === "count" ? "bg-primary text-primary-foreground" : "text-muted-foreground"), children: t("customers.schedule.recurrence.afterCount", "After {{count}} occurrences", { count: recurrenceCount }) }),
        /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: () => setRecurrenceEndType("date"), className: cn("h-auto rounded-full px-3 py-1 text-xs font-medium", recurrenceEndType === "date" ? "bg-background border border-border text-foreground" : "text-muted-foreground"), children: recurrenceEndDate || t("customers.schedule.recurrence.onDate", "On date") })
      ] })
    ] })
  ] });
}
export {
  DateTimeFields
};
//# sourceMappingURL=DateTimeFields.js.map
