"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../../../../_virtual/jsx-runtime.js"),S=require("../../../Field/styles.js"),l=require("react"),B=require("../../../../icons/Close.js"),v=require("../../../../utils/index.js"),k=require("../../../IconButton/index.js"),L=require("../../../Modal/index.js"),f=require("../../utils.js"),F=require("../actionButtons/index.js"),P=require("../calendar/index.js"),V=require("../dayNameRow/index.js"),H=require("../input/index.js"),y=require("../selectors/index.js"),u=require("./styles.js");function N(e){const[m,d]=l.useState(!1),[s,o]=l.useState([{month:e.month,year:e.year}]),[n,E]=l.useState(),[h,C]=l.useState(),[c,b]=l.useState(),[x,w]=l.useState(),[D,q]=l.useState(0),M=v.getId(e["data-testid"]);l.useEffect(()=>{o(f.getInitialMonths(e.month,e.year))},[e.month,e.year]),l.useEffect(()=>{c&&c.scrollIntoView()},[c]);function g(i){e.onSelectorClick(i)}l.useEffect(()=>{n&&h&&(c?n.scrollTop=c.getBoundingClientRect().y-c.getBoundingClientRect().height/2:x&&(n.scrollTop=x.getBoundingClientRect().y-x.getBoundingClientRect().height/2-10))},[n]),l.useEffect(()=>{n&&q(n.clientWidth-(n.offsetWidth-n.clientWidth))},[n==null?void 0:n.offsetWidth,window.innerWidth]);function T(){e.onConfirm(),d(!1)}function W(){e.onDismiss(),d(!1)}function I(i){const r=i.currentTarget;if(r.scrollTop<=20)n&&h&&(n.scrollTop=h.getClientRects()[0].height+20),s[0].month===0?o([{month:11,year:s[0].year-1},...s]):o([{month:s[0].month-1,year:s[0].year},...s]);else if(r.scrollHeight-r.scrollTop<=r.clientHeight+500){const a=s.slice(-1)[0];a.month>=11?o([...s,{month:0,year:a.year+1}]):o([...s,{month:a.month+1,year:a.year}])}}function $(i,r,a,R){r===1&&C(i),e.selectedRange&&R===e.selectedRange.start.getFullYear()&&a===e.selectedRange.start.getMonth()&&b(i);const j=new Date;R===j.getFullYear()&&a===j.getMonth()&&w(i)}return t.jsxRuntimeExports.jsxs(t.jsxRuntimeExports.Fragment,{children:[e.label&&t.jsxRuntimeExports.jsx(S.LabelWrapper,{disabled:e.disabled,hasLabel:!!e.label,children:t.jsxRuntimeExports.jsx(S.Label,{"data-testid":M("label"),disabled:e.disabled,children:e.label})}),t.jsxRuntimeExports.jsx(H.Input,{value:e.selectedRange,variant:e.variant,placeholder:e.placeholder??"",visible:m,disabled:e.disabled,locale:e.locale,handleClick:()=>d(!0),getInputLabel:e.getInputLabel}),t.jsxRuntimeExports.jsx(L.Modal,{isOpen:m,onClose:v.noop,size:"large",isLoading:!1,children:t.jsxRuntimeExports.jsxs(u.ModalContent,{children:[t.jsxRuntimeExports.jsxs(u.Header,{width:D,children:[t.jsxRuntimeExports.jsxs(u.Title,{children:[e.selectedRange&&f.formatInputValue(e.selectedRange,e.locale,e.variant)||"",t.jsxRuntimeExports.jsx(k.IconButton,{icon:t.jsxRuntimeExports.jsx(B.default,{}),variant:"ghost",size:"x-small",onClick:()=>d(!1)})]}),t.jsxRuntimeExports.jsx(V.DayNameRow,{startingDay:e.startingDay,narrow:!0,locale:e.locale,scrollBarWidth:n?n.offsetWidth-n.clientWidth:0})]}),t.jsxRuntimeExports.jsx(u.CalendarsWrapper,{ref:i=>i&&E(i),onScroll:I,children:s.map((i,r)=>t.jsxRuntimeExports.jsx("div",{ref:a=>a&&$(a,r,i.month,i.year),children:t.jsxRuntimeExports.jsx(P.Calendar,{selection:e.selection,selectedRange:e.selectedRange,month:i.month,year:i.year,startingDay:e.startingDay,locale:e.locale,handleChange:e.handleChange,eventDates:e.eventDates,minDate:e.minDate,maxDate:e.maxDate,showSelectors:!!e.showSelectors,narrow:!0,visible:!0})},`${e.month}-${r}`))}),e.showSelectors&&e.selectedSelector&&t.jsxRuntimeExports.jsx("div",{children:t.jsxRuntimeExports.jsx(y.SelectorsOld,{handleChange:g,defaultSelectorValues:e.defaultSelectorValues,customSelectors:e.customSelectors,selectedSelector:e.selectedSelector,narrow:!0})}),f.shouldUseSelectorsProp(e.selectors)&&e.selectedSelector&&t.jsxRuntimeExports.jsx("div",{children:t.jsxRuntimeExports.jsx(y.Selectors,{handleChange:g,selectors:e.selectors,selectedSelector:e.selectedSelector,narrow:!0})}),t.jsxRuntimeExports.jsx(F.ActionButtons,{confirmText:e.confirmText,dismissText:e.dismissText,onDismiss:W,onConfirm:T,narrow:!0})]})})]})}exports.DatePickerNarrow=N;
