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