import { j as i } from "../../../../_virtual/jsx-runtime.js";
import { useState as u, useRef as o, useEffect as a } from "react";
import { useKeyPress as s } from "../../../../hooks/useKeyPress.js";
import { getDays as U, getIsSameDay as I, getIsToday as W, getMonthName as G, isInDateRange as L } from "../../dateUtils.js";
import { Day as T } from "../day/index.js";
import { DayNameRow as $ } from "../dayNameRow/index.js";
import { CalendarWrapper as K, NarrowMonth as k, Dates as q } from "./styles.js";
import { getStartColumn as z, getAllDays as B, getIndexFromGridPosition as j, getGridPosition as H, isDisabled as J, hasEndRange as O, getDayWidth as Q } from "./utils.js";
function te(e) {
  const [l, g] = u([]), [A, E] = u(0), F = o(null), S = z(
    e.startingDay,
    A,
    e.selection
  ), c = o(null), D = s("ArrowDown", c), y = s("ArrowUp", c), r = s("ArrowLeft", c), w = s("ArrowRight", c), v = s("Enter", c), M = s("Space", c), [R, f] = u(!0), P = o(null), [m, b] = u({
    col: 0,
    row: 0
  }), [C, h] = u(!1), [N, x] = u(!1);
  return a(() => {
    e.visible && f(!0);
  }, [e.visible]), a(() => {
    y && e.focusElementIndex !== null && e.focusElementIndex && (e.focusElementIndex - 7 < 0 ? e.selection === "range" ? (e.value === "primary" && e.handlePrevMonth && e.handlePrevMonth(), e.setCalenderWithFocus && e.value && e.setCalenderWithFocus("primary", e.value)) : (h(!0), e.handlePrevMonth && e.handlePrevMonth()) : e.setFocusElementIndex && e.setFocusElementIndex(e.focusElementIndex - 7));
  }, [y]), a(() => {
    D && e.focusElementIndex !== null && e.focusElementIndex && (e.focusElementIndex + 7 >= l.length ? e.selection === "range" ? (e.value === "secondary" && e.handleNextMonth && e.handleNextMonth(), e.setCalenderWithFocus && e.value && e.setCalenderWithFocus("secondary", e.value)) : (e.handleNextMonth && e.handleNextMonth(), x(!0)) : e.setFocusElementIndex && e.setFocusElementIndex(e.focusElementIndex + 7));
  }, [D]), a(() => {
    r && e.focusElementIndex !== null && e.focusElementIndex && (e.focusElementIndex === 0 ? e.selection === "range" ? (e.value === "primary" && e.handlePrevMonth && e.handlePrevMonth(), e.setCalenderWithFocus && e.value && e.setCalenderWithFocus("primary", e.value)) : (e.handlePrevMonth && e.handlePrevMonth(), h(!0)) : e.setFocusElementIndex && e.setFocusElementIndex(e.focusElementIndex - 1));
  }, [r]), a(() => {
    w && e.focusElementIndex !== null && e.focusElementIndex && (e.focusElementIndex === l.length - 1 ? e.selection === "range" ? (e.value === "secondary" && e.handleNextMonth && e.handleNextMonth(), e.setCalenderWithFocus && e.value && e.setCalenderWithFocus("secondary", e.value)) : (e.handleNextMonth && e.handleNextMonth(), x(!0)) : e.setFocusElementIndex && e.setFocusElementIndex(e.focusElementIndex + 1));
  }, [w]), a(() => {
    if (v || M) {
      const n = e.focusElementIndex ? l[e.focusElementIndex] : l[0];
      e.handleChange(n), n.getMonth() !== e.month && f(!0);
    }
  }, [v, M]), a(() => {
    const n = U(e.month, e.year), t = new Date(e.year, e.month).getDay();
    if (e.selection === "single-date") {
      const d = B(
        t,
        e.month,
        e.year,
        e.startingDay
      );
      g(d), E(0);
    } else
      g(n), E(t || 0);
  }, [e.month, e.year, e.startingDay, e.selection]), a(() => {
    c.current && c.current.focus();
  }, [e.focusElementIndex]), a(() => {
    var n;
    (n = e.setLength) == null || n.call(e, l.length);
  }, [l]), a(() => {
    if (R && l.length > 0 && e.hasFocus) {
      const n = l.findIndex(
        (t) => e.selectedRange && I(t, e.selectedRange.start)
      );
      if (n !== -1)
        e.setFocusElementIndex && e.setFocusElementIndex(n);
      else if (l.findIndex((t) => W(t)) !== -1) {
        const t = l.findIndex((d) => W(d));
        e.setFocusElementIndex && e.setFocusElementIndex(t);
      } else {
        const t = l.findIndex((d) => d.getDate() === 1);
        e.setFocusElementIndex && e.setFocusElementIndex(t);
      }
      f(!1);
    }
  }, [l.length, e.hasFocus, R]), a(() => {
    C ? (e.setFocusElementIndex && e.setFocusElementIndex(
      j({
        row: m.row,
        col: m.col
      })
    ), h(!1)) : N && (e.setFocusElementIndex && e.setFocusElementIndex(
      j({ row: 0, col: m.col })
    ), x(!1));
  }, [N, C]), a(() => {
    if (e.focusElementIndex) {
      const t = H(e.focusElementIndex ? e.focusElementIndex : 0, P);
      t && b(t);
    }
  }, [e.focusElementIndex]), /* @__PURE__ */ i.jsxs(K, { ref: F, children: [
    e.startingDay !== "none" && (e.narrow ? /* @__PURE__ */ i.jsx(k, { children: `${G(e.month, e.locale)} ${e.year}` }) : /* @__PURE__ */ i.jsx(
      $,
      {
        startingDay: e.startingDay,
        narrow: e.narrow,
        locale: e.locale
      }
    )),
    /* @__PURE__ */ i.jsx("div", { children: /* @__PURE__ */ i.jsx(q, { startColumn: S, role: "grid", ref: P, children: l.map((n, t) => /* @__PURE__ */ i.jsx(
      T,
      {
        ref: t === e.focusElementIndex ? c : null,
        selectedRange: e.selectedRange,
        day: n,
        width: Q(F),
        currentMonth: e.month,
        selection: e.selection,
        hasFocus: t === e.focusElementIndex,
        isStartDay: e.selectedRange ? I(n, e.selectedRange.start) : !1,
        isEndDay: O(n, e.selectedRange),
        isInRange: !!(e.selectedRange && L(e.selectedRange, n)),
        hasEvent: e.eventDates ? e.eventDates.some((d) => I(d.date, n)) : !1,
        disabled: J(n, e.minDate, e.maxDate),
        onChange: e.handleChange
      },
      n.getDay() + t
    )) }) })
  ] });
}
export {
  te as Calendar
};
