import f from "./index-extends.js";
import * as s from "react";
import { useIsDayDisabled as R } from "./index-useDateValidation.js";
import { useNow as k, useUtils as F } from "./index-useUtils.js";
const O = (o, l, r) => (e, n) => {
  switch (n.type) {
    case "changeMonth":
      return f({}, e, {
        slideDirection: n.direction,
        currentMonth: n.newMonth,
        isMonthSwitchingAnimating: !o
      });
    case "finishMonthSwitchingAnimation":
      return f({}, e, {
        isMonthSwitchingAnimating: !1
      });
    case "changeFocusedDay": {
      if (e.focusedDay != null && n.focusedDay != null && r.isSameDay(n.focusedDay, e.focusedDay))
        return e;
      const u = n.focusedDay != null && !l && !r.isSameMonth(e.currentMonth, n.focusedDay);
      return f({}, e, {
        focusedDay: n.focusedDay,
        isMonthSwitchingAnimating: u && !o && !n.withoutMonthSwitchingAnimation,
        currentMonth: u ? r.startOfMonth(n.focusedDay) : e.currentMonth,
        slideDirection: n.focusedDay != null && r.isAfterDay(n.focusedDay, e.currentMonth) ? "left" : "right"
      });
    }
    default:
      throw new Error("missing support");
  }
}, q = ({
  date: o,
  defaultCalendarMonth: l,
  disableFuture: r,
  disablePast: e,
  disableSwitchToMonthOnDayFocus: n = !1,
  maxDate: u,
  minDate: m,
  onMonthChange: d,
  reduceAnimations: w,
  shouldDisableDate: S
}) => {
  var g;
  const h = k(), i = F(), p = s.useRef(O(!!w, n, i)).current, [a, M] = s.useReducer(p, {
    isMonthSwitchingAnimating: !1,
    focusedDay: o || h,
    currentMonth: i.startOfMonth((g = o ?? l) != null ? g : h),
    slideDirection: "left"
  }), D = s.useCallback((t) => {
    M(f({
      type: "changeMonth"
    }, t)), d && d(t.newMonth);
  }, [d]), A = s.useCallback((t) => {
    const c = t ?? h;
    i.isSameMonth(c, a.currentMonth) || D({
      newMonth: i.startOfMonth(c),
      direction: i.isAfterDay(c, a.currentMonth) ? "left" : "right"
    });
  }, [a.currentMonth, D, h, i]), y = R({
    shouldDisableDate: S,
    minDate: m,
    maxDate: u,
    disableFuture: r,
    disablePast: e
  }), b = s.useCallback(() => {
    M({
      type: "finishMonthSwitchingAnimation"
    });
  }, []), C = s.useCallback((t, c) => {
    y(t) || M({
      type: "changeFocusedDay",
      focusedDay: t,
      withoutMonthSwitchingAnimation: c
    });
  }, [y]);
  return {
    calendarState: a,
    changeMonth: A,
    changeFocusedDay: C,
    isDateDisabled: y,
    onMonthSwitchingAnimationEnd: b,
    handleChangeMonth: D
  };
};
export {
  O as createCalendarStateReducer,
  q as useCalendarState
};
