"use client";
import { cn } from "./index3.js";
import { createSafeContext } from "./index21.js";
import { CalendarContent } from "./index71.js";
import { CalendarHeader } from "./index72.js";
import { CalendarNavigation } from "./index73.js";
import { CalendarTitle } from "./index74.js";
import { CalendarViewSwitcher } from "./index75.js";
import * as React from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { addDays, addMonths, addWeeks, format, subMonths, subWeeks } from "date-fns";
//#region src/components/molecules/data/Calendar/Calendar.tsx
var [CalendarContext, useCalendarContext] = createSafeContext("Calendar");
function Calendar({ events = [], selectedDate: controlledDate, onDateSelect, view: controlledView, onViewChange, weekStartsOn = 1, className, children, onEventClick }) {
	const [internalView, setInternalView] = React.useState("month");
	const [internalDate, setInternalDate] = React.useState(/* @__PURE__ */ new Date());
	const [selectedEventId, setSelectedEventId] = React.useState(null);
	const view = controlledView ?? internalView;
	const selectedDate = controlledDate ?? internalDate;
	const handleDateSelect = React.useCallback((date) => {
		setInternalDate(date);
		onDateSelect?.(date);
	}, [onDateSelect]);
	const handleViewChange = React.useCallback((newView) => {
		setInternalView(newView);
		onViewChange?.(newView);
	}, [onViewChange]);
	const idPrefix = React.useId();
	const selectEvent = React.useCallback((id) => {
		setSelectedEventId(id);
	}, []);
	const clearSelectedEvent = React.useCallback(() => {
		setSelectedEventId(null);
	}, []);
	const navigateNext = React.useCallback(() => {
		switch (view) {
			case "month":
				handleDateSelect(addMonths(selectedDate, 1));
				break;
			case "week":
				handleDateSelect(addWeeks(selectedDate, 1));
				break;
			case "day":
				handleDateSelect(addDays(selectedDate, 1));
				break;
		}
	}, [
		view,
		selectedDate,
		handleDateSelect
	]);
	const navigatePrev = React.useCallback(() => {
		switch (view) {
			case "month":
				handleDateSelect(subMonths(selectedDate, 1));
				break;
			case "week":
				handleDateSelect(subWeeks(selectedDate, 1));
				break;
			case "day":
				handleDateSelect(addDays(selectedDate, -1));
				break;
		}
	}, [
		view,
		selectedDate,
		handleDateSelect
	]);
	const goToToday = React.useCallback(() => {
		handleDateSelect(/* @__PURE__ */ new Date());
	}, [handleDateSelect]);
	const contextValue = React.useMemo(() => ({
		events,
		selectedDate,
		view,
		weekStartsOn,
		idPrefix,
		selectedEventId,
		selectEvent,
		clearSelectedEvent,
		onDateSelect: handleDateSelect,
		onViewChange: handleViewChange,
		navigateNext,
		navigatePrev,
		goToToday
	}), [
		events,
		selectedDate,
		view,
		weekStartsOn,
		idPrefix,
		selectedEventId,
		selectEvent,
		clearSelectedEvent,
		handleDateSelect,
		handleViewChange,
		navigateNext,
		navigatePrev,
		goToToday
	]);
	const navLabel = React.useMemo(() => {
		switch (view) {
			case "month": return format(selectedDate, "MMMM yyyy");
			case "week": return `Week of ${format(selectedDate, "MMMM d, yyyy")}`;
			case "day": return format(selectedDate, "EEEE, MMMM d, yyyy");
		}
	}, [view, selectedDate]);
	const hasChildren = React.Children.count(children) > 0;
	return /* @__PURE__ */ jsx(CalendarContext.Provider, {
		value: contextValue,
		children: /* @__PURE__ */ jsxs("div", {
			"data-slot": "calendar",
			role: "region",
			"aria-label": "Calendar",
			className: cn("@container w-full", className),
			children: [/* @__PURE__ */ jsx("div", {
				className: "sr-only",
				"aria-live": "polite",
				role: "status",
				children: navLabel
			}), hasChildren ? children : /* @__PURE__ */ jsx(DefaultCalendarLayout, { onEventClick })]
		})
	});
}
Calendar.displayName = "Calendar";
function DefaultCalendarLayout({ onEventClick }) {
	return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs(CalendarHeader, { children: [
		/* @__PURE__ */ jsx(CalendarTitle, {}),
		/* @__PURE__ */ jsx(CalendarViewSwitcher, {}),
		/* @__PURE__ */ jsx(CalendarNavigation, {})
	] }), /* @__PURE__ */ jsx(CalendarContent, { onEventClick })] });
}
//#endregion
export { Calendar, useCalendarContext };

//# sourceMappingURL=index76.js.map