"use client";
import { rangeCalendarVariants } 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, RangeCalendar, CalendarCell, CalendarHeaderCell, CalendarGridBody, CalendarGridHeader, CalendarGrid, Button, Heading, RangeCalendarStateContext } from 'react-aria-components';
import { dataAttr } from '../../utils/assertion.js';
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 RangeCalendarContext = /*#__PURE__*/createContext({});
const RangeCalendarYearPickerStateBridge = ({
  children
}) => {
  const state = React__default.useContext(RangeCalendarStateContext);
  if (!state) {
    throw new Error("RangeCalendar year picker state must be used within <RangeCalendar>.");
  }
  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
  });
};

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Root
| * -----------------------------------------------------------------------------------------------*/

function RangeCalendarRoot({
  children,
  className,
  defaultYearPickerOpen: defaultYearPickerOpenProp = false,
  isYearPickerOpen: isYearPickerOpenProp,
  maxValue: maxValueProp,
  minValue: minValueProp,
  onYearPickerOpenChange: onYearPickerOpenChangeProp,
  ...rest
}) {
  const {
    locale
  } = useLocale();
  const slots = React__default.useMemo(() => rangeCalendarVariants(), []);
  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: "range-calendar-grid",
      isYearPickerOpen,
      setIsYearPickerOpen,
      calendarRef
    },
    children: /*#__PURE__*/jsx(RangeCalendarContext, {
      value: {
        slots
      },
      children: /*#__PURE__*/jsx(RangeCalendar, {
        ref: calendarRef,
        "data-slot": "range-calendar",
        maxValue: maxValue,
        minValue: minValue,
        ...rest,
        className: composeTwRenderProps(className, slots.base()),
        children: values => /*#__PURE__*/jsx(RangeCalendarYearPickerStateBridge, {
          children: typeof children === "function" ? children(values) : children
        })
      })
    })
  });
}
RangeCalendarRoot.displayName = "HeroUI.RangeCalendar";

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Header
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Heading
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Nav Button
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Grid
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Grid Header
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Grid Body
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Header Cell
| * -----------------------------------------------------------------------------------------------*/

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

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Cell
| * -----------------------------------------------------------------------------------------------*/

const RangeCalendarCell = ({
  children,
  className,
  ...props
}) => {
  const {
    slots
  } = useContext(RangeCalendarContext);
  return /*#__PURE__*/jsx(CalendarCell, {
    "data-slot": "range-calendar-cell",
    ...props,
    className: composeTwRenderProps(className, slots?.cell()),
    children: values => {
      const {
        formattedDate,
        isDisabled,
        isHovered,
        isPressed,
        isSelectionEnd,
        isSelectionStart
      } = values;
      const content = typeof children === "function" ? children(values) : children || formattedDate;
      return /*#__PURE__*/jsx("span", {
        className: "range-calendar__cell-button",
        "data-disabled": dataAttr(isDisabled),
        "data-hovered": dataAttr(isHovered),
        "data-pressed": dataAttr(isPressed),
        "data-selected": dataAttr(isSelectionStart || isSelectionEnd),
        "data-slot": "range-calendar-cell-button",
        children: content
      });
    }
  });
};
RangeCalendarCell.displayName = "HeroUI.RangeCalendar.Cell";

/* -------------------------------------------------------------------------------------------------
| * RangeCalendar Cell Indicator
| * -----------------------------------------------------------------------------------------------*/

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

export { RangeCalendarCell, RangeCalendarCellIndicator, RangeCalendarGrid, RangeCalendarGridBody, RangeCalendarGridHeader, RangeCalendarHeader, RangeCalendarHeaderCell, RangeCalendarHeading, RangeCalendarNavButton, RangeCalendarRoot };
