import L from "./index-extends.js";
import * as m from "react";
import { createElement as le } from "react";
import { clsx as ce } from "./index-clsx.m.js";
import { PickersDay as de } from "./index-PickersDay.js";
import { useNow as me, useUtils as ue } from "./index-useUtils.js";
import { DAY_MARGIN as _, DAY_SIZE as De } from "./index-dimensions.js";
import { PickersSlideTransition as fe } from "./index-PickersSlideTransition.js";
import { useIsDayDisabled as he } from "./index-useDateValidation.js";
import { findClosestEnabledDate as M } from "./index-date-utils.js";
import { getDayPickerUtilityClass as ye } from "./index-dayPickerClasses.js";
import { jsx as u, jsxs as ke } from "react/jsx-runtime";
import pe from "./index-useThemeProps.js";
import ge from "./index-useTheme.js";
import Ce from "./index-composeClasses.js";
import h from "./index-styled.js";
import be from "./index-Typography.js";
const Me = (r) => {
  const {
    classes: a
  } = r;
  return Ce({
    header: ["header"],
    weekDayLabel: ["weekDayLabel"],
    loadingContainer: ["loadingContainer"],
    slideTransition: ["slideTransition"],
    monthContainer: ["monthContainer"],
    weekContainer: ["weekContainer"]
  }, ye, a);
}, we = (r) => r.charAt(0).toUpperCase(), I = (De + _ * 2) * 6, ve = h("div", {
  name: "MuiDayPicker",
  slot: "Header",
  overridesResolver: (r, a) => a.header
})({
  display: "flex",
  justifyContent: "center",
  alignItems: "center"
}), Pe = h(be, {
  name: "MuiDayPicker",
  slot: "WeekDayLabel",
  overridesResolver: (r, a) => a.weekDayLabel
})(({
  theme: r
}) => ({
  width: 36,
  height: 40,
  margin: "0 2px",
  textAlign: "center",
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
  color: r.palette.text.secondary
})), xe = h("div", {
  name: "MuiDayPicker",
  slot: "LoadingContainer",
  overridesResolver: (r, a) => a.loadingContainer
})({
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
  minHeight: I
}), Ae = h(fe, {
  name: "MuiDayPicker",
  slot: "SlideTransition",
  overridesResolver: (r, a) => a.slideTransition
})({
  minHeight: I
}), Fe = h("div", {
  name: "MuiDayPicker",
  slot: "MonthContainer",
  overridesResolver: (r, a) => a.monthContainer
})({
  overflow: "hidden"
}), Oe = h("div", {
  name: "MuiDayPicker",
  slot: "WeekContainer",
  overridesResolver: (r, a) => a.weekContainer
})({
  margin: `${_}px 0`,
  display: "flex",
  justifyContent: "center"
});
function Ge(r) {
  const a = me(), e = ue(), w = pe({
    props: r,
    name: "MuiDayPicker"
  }), D = Me(w), {
    onFocusedDayChange: v,
    className: H,
    currentMonth: y,
    selectedDays: j,
    disabled: E,
    disableHighlightToday: U,
    focusedDay: K,
    isMonthSwitchingAnimating: B,
    loading: Y,
    onSelectedDaysChange: P,
    onMonthSwitchingAnimationEnd: $,
    readOnly: x,
    reduceAnimations: G,
    renderDay: A,
    renderLoading: V = () => /* @__PURE__ */ u("span", {
      children: "..."
    }),
    showDaysOutsideCurrentMonth: Z,
    slideDirection: q,
    TransitionProps: z,
    disablePast: p,
    disableFuture: g,
    minDate: J,
    maxDate: Q,
    shouldDisableDate: X,
    dayOfWeekFormatter: F = we,
    hasFocus: O,
    onFocusedViewChange: C,
    gridLabelId: ee
  } = w, i = he({
    shouldDisableDate: X,
    minDate: J,
    maxDate: Q,
    disablePast: p,
    disableFuture: g
  }), [d, te] = m.useState(() => K || a), b = m.useCallback((n) => {
    C && C(n);
  }, [C]), ne = m.useCallback((n, t = "finish") => {
    x || P(n, t);
  }, [P, x]), s = m.useCallback((n) => {
    i(n) || (v(n), te(n), b(!0));
  }, [i, v, b]), l = ge();
  function ae(n, t) {
    switch (n.key) {
      case "ArrowUp":
        s(e.addDays(t, -7)), n.preventDefault();
        break;
      case "ArrowDown":
        s(e.addDays(t, 7)), n.preventDefault();
        break;
      case "ArrowLeft": {
        const o = e.addDays(t, l.direction === "ltr" ? -1 : 1), c = l.direction === "ltr" ? e.getPreviousMonth(t) : e.getNextMonth(t), f = M({
          utils: e,
          date: o,
          minDate: l.direction === "ltr" ? e.startOfMonth(c) : o,
          maxDate: l.direction === "ltr" ? o : e.endOfMonth(c),
          isDateDisabled: i
        });
        s(f || o), n.preventDefault();
        break;
      }
      case "ArrowRight": {
        const o = e.addDays(t, l.direction === "ltr" ? 1 : -1), c = l.direction === "ltr" ? e.getNextMonth(t) : e.getPreviousMonth(t), f = M({
          utils: e,
          date: o,
          minDate: l.direction === "ltr" ? o : e.startOfMonth(c),
          maxDate: l.direction === "ltr" ? e.endOfMonth(c) : o,
          isDateDisabled: i
        });
        s(f || o), n.preventDefault();
        break;
      }
      case "Home":
        s(e.startOfWeek(t)), n.preventDefault();
        break;
      case "End":
        s(e.endOfWeek(t)), n.preventDefault();
        break;
      case "PageUp":
        s(e.getNextMonth(t)), n.preventDefault();
        break;
      case "PageDown":
        s(e.getPreviousMonth(t)), n.preventDefault();
        break;
    }
  }
  function oe(n, t) {
    s(t);
  }
  function re(n, t) {
    O && e.isSameDay(d, t) && b(!1);
  }
  const S = e.getMonth(y), T = j.filter((n) => !!n).map((n) => e.startOfDay(n)), N = S, W = m.useMemo(() => /* @__PURE__ */ m.createRef(), [N]), se = e.startOfWeek(a), R = m.useMemo(() => {
    const n = e.startOfMonth(y), t = e.endOfMonth(y);
    return i(d) || e.isAfterDay(d, t) || e.isBeforeDay(d, n) ? M({
      utils: e,
      date: d,
      minDate: n,
      maxDate: t,
      disablePast: p,
      disableFuture: g,
      isDateDisabled: i
    }) : d;
  }, [y, g, p, d, i, e]);
  return /* @__PURE__ */ ke("div", {
    role: "grid",
    "aria-labelledby": ee,
    children: [/* @__PURE__ */ u(ve, {
      role: "row",
      className: D.header,
      children: e.getWeekdays().map((n, t) => {
        var o;
        return /* @__PURE__ */ u(Pe, {
          variant: "caption",
          role: "columnheader",
          "aria-label": e.format(e.addDays(se, t), "weekday"),
          className: D.weekDayLabel,
          children: (o = F == null ? void 0 : F(n)) != null ? o : n
        }, n + t.toString());
      })
    }), Y ? /* @__PURE__ */ u(xe, {
      className: D.loadingContainer,
      children: V()
    }) : /* @__PURE__ */ u(Ae, L({
      transKey: N,
      onExited: $,
      reduceAnimations: G,
      slideDirection: q,
      className: ce(H, D.slideTransition)
    }, z, {
      nodeRef: W,
      children: /* @__PURE__ */ u(Fe, {
        ref: W,
        role: "rowgroup",
        className: D.monthContainer,
        children: e.getWeekArray(y).map((n) => /* @__PURE__ */ u(Oe, {
          role: "row",
          className: D.weekContainer,
          children: n.map((t) => {
            const o = R !== null && e.isSameDay(t, R), c = T.some((ie) => e.isSameDay(ie, t)), f = e.isSameDay(t, a), k = {
              key: t == null ? void 0 : t.toString(),
              day: t,
              isAnimating: B,
              disabled: E || i(t),
              autoFocus: O && o,
              today: f,
              outsideCurrentMonth: e.getMonth(t) !== S,
              selected: c,
              disableHighlightToday: U,
              showDaysOutsideCurrentMonth: Z,
              onKeyDown: ae,
              onFocus: oe,
              onBlur: re,
              onDaySelect: ne,
              tabIndex: o ? 0 : -1,
              role: "gridcell",
              "aria-selected": c
            };
            return f && (k["aria-current"] = "date"), A ? A(t, T, k) : /* @__PURE__ */ le(de, L({}, k, {
              key: k.key
            }));
          })
        }, `week-${n[0]}`))
      })
    }))]
  });
}
export {
  Ge as DayPicker
};
