"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Calendar, dateFnsLocalizer } from "react-big-calendar";
import { addDays, differenceInCalendarDays, endOfDay, endOfMonth, endOfWeek, format, getDay, parse, startOfDay, startOfMonth, startOfWeek } from "date-fns";
import { enUS } from "date-fns/locale/en-US";
import { Button } from "../../primitives/button.js";
import { expandRecurringItems } from "./recurrence.js";
const localizer = dateFnsLocalizer({
  format,
  parse,
  startOfWeek,
  getDay,
  locales: { "en-US": enUS }
});
const VIEW_MAP = {
  day: "day",
  week: "week",
  month: "month",
  agenda: "agenda"
};
function deriveRange(date, view, agendaLength) {
  if (view === "day") {
    return { start: startOfDay(date), end: endOfDay(date) };
  }
  if (view === "week") {
    return { start: startOfWeek(date, { locale: enUS }), end: endOfWeek(date, { locale: enUS }) };
  }
  if (view === "month") {
    return { start: startOfMonth(date), end: endOfMonth(date) };
  }
  const length = Math.max(1, agendaLength);
  return { start: startOfDay(date), end: endOfDay(addDays(date, length - 1)) };
}
function normalizeRange(nextRange, view, agendaLength) {
  if (!nextRange) return null;
  if (Array.isArray(nextRange)) {
    if (nextRange.length === 0) return null;
    if (view === "agenda") {
      return { start: nextRange[0], end: nextRange[nextRange.length - 1] };
    }
    return deriveRange(nextRange[0], view, agendaLength);
  }
  if (nextRange.start && nextRange.end) return { start: nextRange.start, end: nextRange.end };
  return deriveRange(/* @__PURE__ */ new Date(), view, agendaLength);
}
function getEventStyles(item) {
  if (item.kind === "event") {
    return { backgroundColor: "rgba(59, 130, 246, 0.15)", border: "1px solid rgba(59, 130, 246, 0.5)", color: "#1e3a8a" };
  }
  if (item.kind === "exception") {
    return { backgroundColor: "rgba(148, 163, 184, 0.2)", border: "1px solid rgba(100, 116, 139, 0.6)", color: "#334155" };
  }
  return { backgroundColor: "rgba(16, 185, 129, 0.15)", border: "1px solid rgba(16, 185, 129, 0.5)", color: "#064e3b" };
}
function ScheduleCalendar({
  items,
  view,
  range,
  onRangeChange,
  onViewChange,
  onItemClick,
  onSlotClick
}) {
  const agendaLength = React.useMemo(
    () => Math.max(1, differenceInCalendarDays(range.end, range.start) + 1),
    [range.end, range.start]
  );
  const currentView = VIEW_MAP[view];
  const expandedItems = React.useMemo(() => expandRecurringItems(items, range), [items, range]);
  const events = React.useMemo(
    () => expandedItems.map((item) => ({
      id: item.id,
      title: item.title,
      start: item.startsAt,
      end: item.endsAt,
      resource: item
    })),
    [expandedItems]
  );
  const handleNavigate = React.useCallback((date, nextView) => {
    const resolvedView = nextView ?? currentView;
    onRangeChange(deriveRange(date, resolvedView, agendaLength));
  }, [agendaLength, currentView, onRangeChange]);
  const handleRangeChange = React.useCallback((nextRange, nextView) => {
    const resolvedView = nextView ?? currentView;
    const normalized = normalizeRange(nextRange, resolvedView, agendaLength);
    if (normalized) onRangeChange(normalized);
  }, [agendaLength, currentView, onRangeChange]);
  const handleViewChange = React.useCallback((nextView) => {
    const resolved = nextView;
    if (resolved !== view) {
      onViewChange(resolved);
      onRangeChange(deriveRange(/* @__PURE__ */ new Date(), resolved, agendaLength));
    }
  }, [agendaLength, onRangeChange, onViewChange, view]);
  const handleSelectEvent = React.useCallback(
    (event) => onItemClick?.(event.resource),
    [onItemClick]
  );
  const handleSelectSlot = React.useCallback(
    (slot) => {
      if (!onSlotClick) return;
      onSlotClick({ start: slot.start, end: slot.end });
    },
    [onSlotClick]
  );
  const eventPropGetter = React.useCallback(
    (event) => ({ style: getEventStyles(event.resource) }),
    []
  );
  const calendarStyle = React.useMemo(() => ({ height: 640 }), []);
  const components = React.useMemo(
    () => ({
      event: ({ event }) => {
        const resource = event.resource;
        const hasLink = Boolean(resource.linkLabel) && typeof onItemClick === "function";
        return /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
          /* @__PURE__ */ jsx("span", { className: "truncate text-xs font-medium", children: resource.title }),
          hasLink ? /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: "link",
              size: "sm",
              className: "h-auto p-0 text-[11px]",
              onClick: (clickEvent) => {
                clickEvent.stopPropagation();
                onItemClick?.(resource);
              },
              children: resource.linkLabel
            }
          ) : null
        ] });
      }
    }),
    [onItemClick]
  );
  return /* @__PURE__ */ jsx(
    Calendar,
    {
      localizer,
      culture: "en-US",
      events,
      view: currentView,
      date: range.start,
      toolbar: false,
      selectable: Boolean(onSlotClick),
      popup: true,
      length: agendaLength,
      onView: handleViewChange,
      onNavigate: handleNavigate,
      onRangeChange: handleRangeChange,
      onSelectEvent: handleSelectEvent,
      onSelectSlot: handleSelectSlot,
      eventPropGetter,
      components,
      style: calendarStyle
    }
  );
}
export {
  ScheduleCalendar as default
};
//# sourceMappingURL=ScheduleCalendar.js.map
