"use client";
import { calendarVariants } from '@heroui/styles';
import { DateFormatter, createCalendar, CalendarDate } from '@internationalized/date';
import { useControlledState } from '@react-stately/utils';
import React__default, { createContext, useContext } from 'react';
import { useLocale, Calendar, CalendarCell as CalendarCell$1, CalendarHeaderCell as CalendarHeaderCell$1, CalendarGridBody as CalendarGridBody$1, CalendarGridHeader as CalendarGridHeader$1, CalendarGrid as CalendarGrid$1, Button, Heading, CalendarStateContext } from 'react-aria-components';
import { getGregorianYearOffset } from '../../utils/calendar.js';
import { composeTwRenderProps, composeSlotClassName } from '../../utils/compose.js';
import { YearPickerContext, YearPickerStateContext } from '../calendar-year-picker/year-picker-context.js';
import { IconChevronLeft, IconChevronRight } from '../icons.js';
import { jsx } from 'react/jsx-runtime';

const CalendarContext = /*#__PURE__*/createContext({});
const CalendarYearPickerStateBridge = ({
  children
}) => {
  const state = React__default.useContext(CalendarStateContext);
  if (!state) {
    throw new Error("Calendar year picker state must be used within <Calendar>.");
  }
  const yearPickerStateValue = {
    focusedDate: state.focusedDate,
    maxValue: state.maxValue,
    minValue: state.minValue,
    setFocusedDate: value => state.setFocusedDate(value),
    timeZone: state.timeZone
  };
  return /*#__PURE__*/jsx(YearPickerStateContext, {
    value: yearPickerStateValue,
    children: children
  });
};

/* -------------------------------------------------------------------------------------------------
| * Calendar Root
| * -----------------------------------------------------------------------------------------------*/

function CalendarRoot({
  children,
  className,
  defaultYearPickerOpen: defaultYearPickerOpenProp = false,
  isYearPickerOpen: isYearPickerOpenProp,
  maxValue: maxValueProp,
  minValue: minValueProp,
  onYearPickerOpenChange: onYearPickerOpenChangeProp,
  ...rest
}) {
  const {
    locale
  } = useLocale();
  const slots = React__default.useMemo(() => calendarVariants(), []);
  const calendarRef = React__default.useRef(null);
  const [isYearPickerOpen, setIsYearPickerOpen] = useControlledState(isYearPickerOpenProp, defaultYearPickerOpenProp, onYearPickerOpenChangeProp);
  const calendarProp = React__default.useMemo(() => {
    const calendarIdentifier = new DateFormatter(locale).resolvedOptions().calendar;
    return createCalendar(calendarIdentifier);
  }, [locale]);
  const gregorianYearOffset = React__default.useMemo(() => getGregorianYearOffset(calendarProp.identifier), [calendarProp.identifier]);
  const minValue = minValueProp ?? new CalendarDate(calendarProp, 1900 + gregorianYearOffset, 1, 1);
  const maxValue = maxValueProp ?? new CalendarDate(calendarProp, 2099 + gregorianYearOffset, 12, 31);
  return /*#__PURE__*/jsx(YearPickerContext, {
    value: {
      calendarGridSlot: "calendar-grid",
      isYearPickerOpen,
      setIsYearPickerOpen,
      calendarRef
    },
    children: /*#__PURE__*/jsx(CalendarContext, {
      value: {
        slots
      },
      children: /*#__PURE__*/jsx(Calendar, {
        ref: calendarRef,
        "data-slot": "calendar",
        maxValue: maxValue,
        minValue: minValue,
        ...rest,
        className: composeTwRenderProps(className, slots.base()),
        children: values => /*#__PURE__*/jsx(CalendarYearPickerStateBridge, {
          children: typeof children === "function" ? children(values) : children
        })
      })
    })
  });
}
CalendarRoot.displayName = "HeroUI.Calendar";

/* -------------------------------------------------------------------------------------------------
| * Calendar Header
| * -----------------------------------------------------------------------------------------------*/

const CalendarHeader = ({
  children,
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx("header", {
    className: composeSlotClassName(slots?.header, className),
    "data-slot": "calendar-header",
    ...props,
    children: children
  });
};
CalendarHeader.displayName = "HeroUI.Calendar.Header";

/* -------------------------------------------------------------------------------------------------
| * Calendar Heading
| * -----------------------------------------------------------------------------------------------*/

const CalendarHeading = ({
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(Heading, {
    "data-slot": "calendar-heading",
    ...props,
    className: composeSlotClassName(slots?.heading, className)
  });
};
CalendarHeading.displayName = "HeroUI.Calendar.Heading";

/* -------------------------------------------------------------------------------------------------
| * Calendar Nav Button
| * -----------------------------------------------------------------------------------------------*/

const CalendarNavButton = ({
  children,
  className,
  slot,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(Button, {
    "data-slot": "calendar-nav-button",
    slot: slot,
    ...props,
    className: composeTwRenderProps(className, slots?.navButton()),
    children: children || (slot === "previous" ? /*#__PURE__*/jsx(IconChevronLeft, {
      className: slots?.navButtonIcon(),
      "data-slot": "calendar-nav-button-icon"
    }) : /*#__PURE__*/jsx(IconChevronRight, {
      className: slots?.navButtonIcon(),
      "data-slot": "calendar-nav-button-icon"
    }))
  });
};
CalendarNavButton.displayName = "HeroUI.Calendar.NavButton";

/* -------------------------------------------------------------------------------------------------
| * Calendar Grid
| * -----------------------------------------------------------------------------------------------*/

const CalendarGrid = ({
  children,
  className,
  weekdayStyle = "short",
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(CalendarGrid$1, {
    "data-slot": "calendar-grid",
    weekdayStyle: weekdayStyle,
    ...props,
    className: composeSlotClassName(slots?.grid, className),
    children: children
  });
};
CalendarGrid.displayName = "HeroUI.Calendar.Grid";

/* -------------------------------------------------------------------------------------------------
| * Calendar Grid Header
| * -----------------------------------------------------------------------------------------------*/

const CalendarGridHeader = ({
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(CalendarGridHeader$1, {
    "data-slot": "calendar-grid-header",
    ...props,
    className: composeSlotClassName(slots?.gridHeader, className)
  });
};
CalendarGridHeader.displayName = "HeroUI.Calendar.GridHeader";

/* -------------------------------------------------------------------------------------------------
| * Calendar Grid Body
| * -----------------------------------------------------------------------------------------------*/

const CalendarGridBody = ({
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(CalendarGridBody$1, {
    "data-slot": "calendar-grid-body",
    ...props,
    className: composeSlotClassName(slots?.gridBody, className)
  });
};
CalendarGridBody.displayName = "HeroUI.Calendar.GridBody";

/* -------------------------------------------------------------------------------------------------
| * Calendar Header Cell
| * -----------------------------------------------------------------------------------------------*/

const CalendarHeaderCell = ({
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(CalendarHeaderCell$1, {
    "data-slot": "calendar-header-cell",
    ...props,
    className: composeSlotClassName(slots?.headerCell, className)
  });
};
CalendarHeaderCell.displayName = "HeroUI.Calendar.HeaderCell";

/* -------------------------------------------------------------------------------------------------
| * Calendar Cell
| * -----------------------------------------------------------------------------------------------*/

const CalendarCell = ({
  children,
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx(CalendarCell$1, {
    "data-slot": "calendar-cell",
    ...props,
    className: composeTwRenderProps(className, slots?.cell()),
    children: values => {
      const {
        formattedDate
      } = values;
      return typeof children === "function" ? children(values) : children || formattedDate;
    }
  });
};
CalendarCell.displayName = "HeroUI.Calendar.Cell";

/* -------------------------------------------------------------------------------------------------
| * Calendar Cell Indicator
| * -----------------------------------------------------------------------------------------------*/

const CalendarCellIndicator = ({
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(CalendarContext);
  return /*#__PURE__*/jsx("span", {
    "aria-hidden": "true",
    className: composeSlotClassName(slots?.cellIndicator, className),
    "data-slot": "calendar-cell-indicator",
    ...props
  });
};
CalendarCellIndicator.displayName = "HeroUI.Calendar.CellIndicator";

export { CalendarCell, CalendarCellIndicator, CalendarGrid, CalendarGridBody, CalendarGridHeader, CalendarHeader, CalendarHeaderCell, CalendarHeading, CalendarNavButton, CalendarRoot };
