"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { cn } from "@open-mercato/shared/lib/utils";
import { Badge } from "../../primitives/badge.js";
import { Button } from "../../primitives/button.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { expandRecurringItems } from "./recurrence.js";
const DAY_MS = 24 * 60 * 60 * 1e3;
function startOfDay(value) {
  return new Date(value.getFullYear(), value.getMonth(), value.getDate());
}
function endOfDay(value) {
  return new Date(value.getFullYear(), value.getMonth(), value.getDate(), 23, 59, 59, 999);
}
function eachDay(start, end) {
  const days = [];
  let cursor = startOfDay(start);
  const last = startOfDay(end);
  while (cursor <= last) {
    days.push(new Date(cursor));
    cursor = new Date(cursor.getTime() + DAY_MS);
  }
  return days;
}
function overlapsDay(item, day) {
  const dayStart = startOfDay(day);
  const dayEnd = endOfDay(day);
  return item.startsAt <= dayEnd && item.endsAt >= dayStart;
}
function formatDayLabel(day) {
  return day.toLocaleDateString(void 0, { weekday: "short", month: "short", day: "numeric" });
}
function formatTimeRange(item, timezone) {
  const options = { hour: "2-digit", minute: "2-digit" };
  if (timezone) options.timeZone = timezone;
  const startLabel = item.startsAt.toLocaleTimeString(void 0, options);
  const endLabel = item.endsAt.toLocaleTimeString(void 0, options);
  return `${startLabel}-${endLabel}`;
}
function getStatusLabel(status, t) {
  if (!status) return null;
  if (status === "draft") return t("schedule.item.status.draft", "Draft");
  if (status === "negotiation") return t("schedule.item.status.negotiation", "Negotiation");
  if (status === "confirmed") return t("schedule.item.status.confirmed", "Confirmed");
  if (status === "cancelled") return t("schedule.item.status.cancelled", "Cancelled");
  return null;
}
function getKindStyles(kind) {
  if (kind === "event") return "border-blue-500/40 bg-blue-500/10 text-blue-950";
  if (kind === "exception") return "border-amber-500/40 bg-amber-500/10 text-amber-950";
  return "border-emerald-500/40 bg-emerald-500/10 text-emerald-950";
}
function ScheduleGrid({ items, range, timezone, onItemClick, onSlotClick, className }) {
  const t = useT();
  const days = React.useMemo(() => eachDay(range.start, range.end), [range]);
  const expandedItems = React.useMemo(() => expandRecurringItems(items, range), [items, range]);
  return /* @__PURE__ */ jsx("div", { className: cn("grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3", className), children: days.map((day) => {
    const dayItems = expandedItems.filter((item) => overlapsDay(item, day));
    const slotStart = new Date(day.getFullYear(), day.getMonth(), day.getDate(), 9, 0, 0);
    const slotEnd = new Date(day.getFullYear(), day.getMonth(), day.getDate(), 10, 0, 0);
    return /* @__PURE__ */ jsxs("div", { className: "rounded-xl border bg-card p-4 shadow-sm", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
        /* @__PURE__ */ jsx("div", { className: "text-sm font-semibold text-foreground", children: formatDayLabel(day) }),
        onSlotClick ? /* @__PURE__ */ jsx(
          Button,
          {
            type: "button",
            variant: "outline",
            size: "sm",
            onClick: () => onSlotClick({ start: slotStart, end: slotEnd }),
            children: t("schedule.actions.add", "Add")
          }
        ) : null
      ] }),
      /* @__PURE__ */ jsx("div", { className: "mt-3 space-y-2", children: dayItems.length === 0 ? /* @__PURE__ */ jsx("div", { className: "rounded-lg border border-dashed p-3 text-xs text-muted-foreground", children: t("schedule.emptyDay", "No scheduled items") }) : dayItems.map((item) => {
        const statusLabel = getStatusLabel(item.status, t);
        return /* @__PURE__ */ jsxs(
          "button",
          {
            type: "button",
            className: cn(
              "flex w-full cursor-pointer flex-col gap-2 rounded-lg border px-3 py-2 text-left text-xs transition hover:shadow-sm",
              getKindStyles(item.kind)
            ),
            onClick: () => onItemClick?.(item),
            children: [
              /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
                /* @__PURE__ */ jsx("span", { className: "font-semibold", children: item.title }),
                statusLabel ? /* @__PURE__ */ jsx(Badge, { variant: "secondary", children: statusLabel }) : null
              ] }),
              /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between text-[11px] text-muted-foreground", children: [
                /* @__PURE__ */ jsx("span", { children: formatTimeRange(item, timezone) }),
                /* @__PURE__ */ jsx("span", { className: "capitalize", children: item.kind })
              ] })
            ]
          },
          item.id
        );
      }) })
    ] }, day.toISOString());
  }) });
}
export {
  ScheduleGrid
};
//# sourceMappingURL=ScheduleGrid.js.map
