'use client';
import{__rest as e}from"tslib";import l,{useRef as o,useState as a}from"react";import{twMerge as n}from"tailwind-merge";import{startOfToday as t,startOfMonth as r}from"date-fns";import{enUS as d}from"date-fns/locale";import s from"../../../contexts/BaseColorContext.js";import u from"../../../contexts/HoveredValueContext.js";import"../../../contexts/RootStylesContext.js";import m from"../../../contexts/SelectedValueContext.js";import i from"../../../hooks/useInternalState.js";import c from"../../../hooks/useSelectOnKeyDown.js";import{BaseColors as p}from"../../../lib/constants.js";import{parseStartDate as f,parseEndDate as w,makeDateRangePickerClassName as h,defaultOptions as D,getStartDateByDropdownValue as v,getEndDateByDropdownValue as C}from"./dateRangePickerUtils.js";import x from"./Calendar.js";import b from"./DateRangePickerButton.js";import"../Dropdown/Dropdown.js";import j from"../Dropdown/DropdownItem.js";import g from"../../util-elements/Modal/Modal.js";const P=l.forwardRef(((P,S)=>{var E,M,R;const{value:V,defaultValue:k,onValueChange:y,enableDropdown:N=!0,options:B,minDate:K=null,maxDate:O=null,placeholder:Y="Select",dropdownPlaceholder:H="Select",disabled:I=!1,color:U=p.Blue,enableYearPagination:q=!1,locale:z=d,enableClear:A=!0,className:F}=P,G=e(P,["value","defaultValue","onValueChange","enableDropdown","options","minDate","maxDate","placeholder","dropdownPlaceholder","disabled","color","enableYearPagination","locale","enableClear","className"]),J=t(),L=o(null),Q=o(null),[T,W]=i(k,V),[X,Z]=a(null),[$,_]=a(!1),[ee,le]=a(!1),oe=null!=B?B:D,ae=T&&null!==(E=T[2])&&void 0!==E?E:null,ne=T?f(T[0],K,ae,oe):null,te=T?w(T[1],O,ae,oe):null,re=null!==(R=null!==(M=null!=X?X:te)&&void 0!==M?M:ne)&&void 0!==R?R:J,de=e=>{const l=v(e,oe),o=C(e,oe);W([l,o,e]),null==y||y([l,o,e]),Z(r(o)),le(!1)},[se,ue]=c(de,oe.map((e=>e.value)),ee,le,ae);return l.createElement(s.Provider,{value:U},l.createElement("div",Object.assign({ref:S,className:n(h("root"),"relative w-full",F)},G),l.createElement(b,{value:[ne,te,ae],options:oe,placeholder:Y,disabled:I,calendarRef:L,showCalendar:$,setShowCalendar:_,onCalendarKeyDown:e=>{"Escape"===e.key&&(e.preventDefault(),_(!1))},enableDropdown:N,dropdownRef:Q,showDropdown:ee,setShowDropdown:le,onDropdownKeyDown:ue,locale:z,dropdownPlaceholder:H,enableClear:A,onClear:()=>{W([null,null,null]),null==y||y([null,null,null])}}),l.createElement(g,{className:h("calendarModal"),showModal:$,setShowModal:_,parentRef:L,width:288,maxHeight:"max-h-fit"},l.createElement(x,{enableYearPagination:q,anchorDate:re,startDate:ne,endDate:te,minDate:K,maxDate:O,onDateClick:e=>{ne?ne&&!te?e<ne?(null==y||y([e,te,null]),W([e,te,null])):(null==y||y([ne,e,null]),W([ne,e,null]),_(!1)):ne&&te&&(null==y||y([e,null,null]),W([e,null,null])):(null==y||y([e,te,null]),W([e,te,null]))},locale:z,setStartOfCurrMonth:Z})),l.createElement(g,{className:h("dropdownModal"),showModal:ee,setShowModal:le,parentRef:Q},l.createElement(m.Provider,{value:{selectedValue:ae,handleValueChange:de}},l.createElement(u.Provider,{value:{hoveredValue:se}},oe.map((({value:e,text:o})=>l.createElement(j,{key:e,value:e,text:o}))))))))}));P.displayName="DateRangePicker";export{P as default};
