import { jsxs as g, jsx as t } from "@emotion/react/jsx-runtime";
import U from "clsx";
import { forwardRef as X, memo as P } from "react";
import { ArrowChevronSmLeftOutlined as ee, ArrowChevronSmRightOutlined as ae } from "@m4l/icons";
import { getPropDataTestId as r } from "../../test/getNameDataTestId.js";
import { DATE_RANGE_CALENDAR_PICKER_CALENDAR_WIDTH as te, DATE_RANGE_CALENDAR_PICKER_KEY_COMPONENT as i, DATE_RANGE_CALENDAR_PICKER_CLASSES as le } from "./constants.js";
import { useDateRangeCalendarPicker as de } from "./hooks/useDateRangeCalendarPicker/useDateRangeCalendarPicker.js";
import { RootStyled as ne, HeadersStyled as re, HeaderStyled as ie, HeaderNavigationButtonStyled as M, HeaderLabelButtonStyled as oe, HeaderTitleStyled as ye, CalendarStyled as se, MonthStyled as ce, WeekdayRowStyled as ue, WeekdayLabelStyled as V, SkeletonStyled as me, WeeksStyled as ge, WeekStyled as he, TimeRangeStyled as Se, TimeFieldGroupStyled as p, TimeFieldSummaryStyled as x, TimePickerStyled as H, HelperErrorStyled as De, YearPopoverStyled as be, YearCalendarStyled as Ce, DayButtonStyled as fe, DayBadgeStyled as Te, DayLabelStyled as Y } from "./slots/DateRangeCalendarPickerSlots.js";
import { DateRangeCalendarPickerSlots as o } from "./slots/DateRangeCalendarPickerEnum.js";
function G(n, a) {
  return n?.getTime() === a?.getTime();
}
function ke({
  dataTestId: n,
  day: a,
  dayModelConfig: w,
  getDayModel: d,
  handleDayFocus: h,
  handleDayKeyDown: S,
  handleDayMouseEnter: D,
  handleSelectDay: b,
  month: C,
  nextDay: f,
  previousDay: T,
  renderToken: L,
  setDayRef: k,
  weekEnd: R,
  weekStart: $
}) {
  const l = d(a, C, T, f);
  return /* @__PURE__ */ t(
    fe,
    {
      ref: (u) => {
        k(l.dayKey, u);
      },
      "aria-current": l.ariaCurrent,
      "aria-label": l.ariaLabel,
      "aria-selected": l.metadata.isSelected,
      className: U(
        le.dayButton,
        l.metadata.isSelected && "Mui-selected"
      ),
      disabled: l.metadata.disabled,
      onClick: () => b(a),
      onFocus: () => h(a),
      onKeyDown: (u) => S(u, a, $, R),
      onMouseEnter: () => D(a),
      ownerState: l.ownerState,
      role: "gridcell",
      tabIndex: l.tabIndex,
      ...r(
        i,
        o.dayButton,
        `${n ?? "default"}-${l.dayKey}`
      ),
      children: l.badge ? /* @__PURE__ */ t(
        Te,
        {
          anchorOrigin: l.badge.anchorOrigin,
          badgeContent: l.badge.badgeContent,
          color: l.badge.color,
          invisible: l.badge.invisible,
          max: l.badge.max,
          ownerState: l.ownerState,
          overlap: "rectangular",
          showZero: l.badge.showZero,
          size: "small",
          variant: l.badge.variant,
          ...r(
            i,
            o.dayBadge,
            `${n ?? "default"}-${l.dayKey}`
          ),
          children: /* @__PURE__ */ t(Y, { ownerState: l.ownerState, children: l.label })
        }
      ) : /* @__PURE__ */ t(Y, { ownerState: l.ownerState, children: l.label })
    }
  );
}
const Re = P(
  ke,
  (n, a) => n.renderToken === a.renderToken && n.dataTestId === a.dataTestId && n.dayModelConfig === a.dayModelConfig && n.getDayModel === a.getDayModel && n.handleDayFocus === a.handleDayFocus && n.handleDayKeyDown === a.handleDayKeyDown && n.handleDayMouseEnter === a.handleDayMouseEnter && n.handleSelectDay === a.handleSelectDay && n.setDayRef === a.setDayRef && n.day.getTime() === a.day.getTime() && n.month.getTime() === a.month.getTime() && G(n.previousDay, a.previousDay) && G(n.nextDay, a.nextDay) && n.weekStart.getTime() === a.weekStart.getTime() && n.weekEnd.getTime() === a.weekEnd.getTime()
), ve = X(function(a, w) {
  const { dataTestId: d } = a, {
    activeYearMonth: h,
    dayModelConfig: S,
    getDayModel: D,
    getDayRenderToken: b,
    handleCalendarMouseLeave: C,
    handleDayFocus: f,
    handleDayKeyDown: T,
    handleDayMouseEnter: L,
    handleOpenYearPopover: k,
    handleSelectDay: R,
    isNextDisabled: $,
    isPreviousDisabled: l,
    labels: u,
    maxDaysError: A,
    monthModels: I,
    openYearPopoverIndex: E,
    ownerState: e,
    rootClassName: W,
    rootSlotProps: N,
    setDayRef: j,
    shiftVisibleMonths: O,
    timeRange: c,
    weekdayHeaders: _,
    yearCalendarProps: v,
    yearPopoverProps: Z
  } = de(a), q = {
    ...N?.style,
    "--M4LDateRangeCalendarPicker-calendar-width": te
  }, z = e.variant === "column";
  return /* @__PURE__ */ g(
    ne,
    {
      ref: w,
      ...N,
      className: W,
      ownerState: e,
      style: q,
      ...r(
        i,
        o.root,
        d
      ),
      children: [
        /* @__PURE__ */ t(re, { ownerState: e, children: I.map((y) => /* @__PURE__ */ g(
          ie,
          {
            ownerState: e,
            children: [
              /* @__PURE__ */ t(
                M,
                {
                  "aria-label": u.previousMonth,
                  dataTestId: r(
                    i,
                    o.headerNavigationButton,
                    `${d ?? "default"}-${y.index}-previous`
                  )["data-testid"],
                  disabled: e.disabled || l,
                  icon: /* @__PURE__ */ t(ee, {}),
                  onClick: () => O(-1),
                  ownerState: e,
                  size: e.size
                }
              ),
              /* @__PURE__ */ t(
                oe,
                {
                  "aria-expanded": y.isYearPopoverOpen,
                  "aria-haspopup": "dialog",
                  "aria-label": y.headerButtonAriaLabel,
                  disabled: e.disabled,
                  disableRipple: !0,
                  onClick: (s) => k(y.index, s.currentTarget),
                  ownerState: e,
                  skeletonHeight: 12,
                  skeletonWidth: "65%",
                  ...r(
                    i,
                    o.headerLabelButton,
                    `${d ?? "default"}-${y.index}`
                  ),
                  children: /* @__PURE__ */ t(ye, { ownerState: e, children: y.headerLabel })
                }
              ),
              /* @__PURE__ */ t(
                M,
                {
                  "aria-label": u.nextMonth,
                  dataTestId: r(
                    i,
                    o.headerNavigationButton,
                    `${d ?? "default"}-${y.index}-next`
                  )["data-testid"],
                  disabled: e.disabled || $,
                  icon: /* @__PURE__ */ t(ae, {}),
                  onClick: () => O(1),
                  ownerState: e,
                  size: e.size
                }
              )
            ]
          },
          `${y.key}-header`
        )) }),
        /* @__PURE__ */ t(
          se,
          {
            ownerState: e,
            onMouseLeave: C,
            ...r(
              i,
              o.calendar,
              d
            ),
            children: I.map((y) => /* @__PURE__ */ g(
              ce,
              {
                ownerState: e,
                "aria-label": y.monthAriaLabel,
                children: [
                  /* @__PURE__ */ t(
                    ue,
                    {
                      ownerState: e,
                      role: "row",
                      ...r(
                        i,
                        o.weekdayRow,
                        `${d ?? "default"}-${y.index}`
                      ),
                      children: e.isSkeleton ? _.map((s, m) => /* @__PURE__ */ t(
                        V,
                        {
                          ownerState: e,
                          "aria-hidden": "true",
                          ...r(
                            i,
                            o.weekdayLabel,
                            `${d ?? "default"}-${y.index}-${m}`
                          ),
                          children: /* @__PURE__ */ t(
                            me,
                            {
                              ownerState: e,
                              variant: "rounded",
                              width: 16,
                              height: 16
                            }
                          )
                        },
                        s.date.toISOString()
                      )) : _.map((s, m) => /* @__PURE__ */ t(
                        V,
                        {
                          ownerState: e,
                          role: "columnheader",
                          "aria-label": s.ariaLabel,
                          ...r(
                            i,
                            o.weekdayLabel,
                            `${d ?? "default"}-${y.index}-${m}`
                          ),
                          children: s.label
                        },
                        s.date.toISOString()
                      ))
                    }
                  ),
                  /* @__PURE__ */ t(
                    ge,
                    {
                      ownerState: e,
                      role: "grid",
                      "aria-label": u.dateTableLabel,
                      children: y.weeks.map((s) => /* @__PURE__ */ t(
                        he,
                        {
                          ownerState: e,
                          role: "row",
                          children: s.map((m, B) => {
                            const K = s[B - 1] ?? null, F = s[B + 1] ?? null, J = s[0], Q = s[s.length - 1];
                            return /* @__PURE__ */ t(
                              Re,
                              {
                                dataTestId: d,
                                day: m,
                                dayModelConfig: S,
                                getDayModel: D,
                                handleDayFocus: f,
                                handleDayKeyDown: T,
                                handleDayMouseEnter: L,
                                handleSelectDay: R,
                                month: y.month,
                                nextDay: F,
                                previousDay: K,
                                renderToken: b(
                                  m,
                                  y.month,
                                  K,
                                  F
                                ),
                                setDayRef: j,
                                weekEnd: Q,
                                weekStart: J
                              },
                              m.toISOString()
                            );
                          })
                        },
                        s[0].toISOString()
                      ))
                    }
                  )
                ]
              },
              y.key
            ))
          }
        ),
        c ? /* @__PURE__ */ g(
          Se,
          {
            ownerState: e,
            ...r(
              i,
              o.timeRange,
              d
            ),
            children: [
              /* @__PURE__ */ g(
                p,
                {
                  ownerState: e,
                  ...r(
                    i,
                    o.timeFieldGroup,
                    `${d ?? "default"}-start`
                  ),
                  children: [
                    z ? /* @__PURE__ */ t(
                      x,
                      {
                        ownerState: e,
                        ...r(
                          i,
                          o.timeFieldSummary,
                          `${d ?? "default"}-start`
                        ),
                        children: c.startTimeLabel
                      }
                    ) : null,
                    /* @__PURE__ */ t(
                      H,
                      {
                        dataTestId: `${d ?? "default"}-start`,
                        disabled: c.isStartDisabled,
                        displayVariant: c.timeDisplayVariant,
                        granularity: "seconds",
                        onChange: c.handleStartTimeChange,
                        ownerState: e,
                        pickerVariant: "inline",
                        readOnly: e.readOnly,
                        size: e.size,
                        value: c.startTimeValue,
                        ...r(
                          i,
                          o.timePicker,
                          `${d ?? "default"}-start`
                        )
                      }
                    )
                  ]
                }
              ),
              /* @__PURE__ */ g(
                p,
                {
                  ownerState: e,
                  ...r(
                    i,
                    o.timeFieldGroup,
                    `${d ?? "default"}-end`
                  ),
                  children: [
                    z ? /* @__PURE__ */ t(
                      x,
                      {
                        ownerState: e,
                        ...r(
                          i,
                          o.timeFieldSummary,
                          `${d ?? "default"}-end`
                        ),
                        children: c.endTimeLabel
                      }
                    ) : null,
                    /* @__PURE__ */ t(
                      H,
                      {
                        dataTestId: `${d ?? "default"}-end`,
                        disabled: c.isEndDisabled,
                        displayVariant: c.timeDisplayVariant,
                        granularity: "seconds",
                        onChange: c.handleEndTimeChange,
                        ownerState: e,
                        pickerVariant: "inline",
                        readOnly: e.readOnly,
                        size: e.size,
                        value: c.endTimeValue,
                        ...r(
                          i,
                          o.timePicker,
                          `${d ?? "default"}-end`
                        )
                      }
                    )
                  ]
                }
              )
            ]
          }
        ) : null,
        A ? /* @__PURE__ */ t(
          De,
          {
            message: A,
            ownerState: e,
            size: e.size,
            ...r(
              i,
              o.helperError,
              d
            )
          }
        ) : null,
        /* @__PURE__ */ t(
          be,
          {
            ...Z,
            ...r(
              i,
              o.yearPopover,
              E === null ? d : `${d ?? "default"}-${E}`
            ),
            children: v && h ? /* @__PURE__ */ t(
              Ce,
              {
                ...v,
                ...r(
                  i,
                  o.yearCalendar,
                  `${d ?? "default"}-${E}`
                )
              }
            ) : null
          }
        )
      ]
    }
  );
});
export {
  ve as DateRangeCalendarPicker
};
